podman
78 строк · 1.9 Кб
1// Copyright 2015 go-swagger maintainers
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package errors
16
17import (
18"encoding/json"
19"fmt"
20)
21
22// ParseError represents a parsing error
23type ParseError struct {
24code int32
25Name string
26In string
27Value string
28Reason error
29message string
30}
31
32func (e *ParseError) Error() string {
33return e.message
34}
35
36// Code returns the http status code for this error
37func (e *ParseError) Code() int32 {
38return e.code
39}
40
41// MarshalJSON implements the JSON encoding interface
42func (e ParseError) MarshalJSON() ([]byte, error) {
43var reason string
44if e.Reason != nil {
45reason = e.Reason.Error()
46}
47return json.Marshal(map[string]interface{}{
48"code": e.code,
49"message": e.message,
50"in": e.In,
51"name": e.Name,
52"value": e.Value,
53"reason": reason,
54})
55}
56
57const (
58parseErrorTemplContent = `parsing %s %s from %q failed, because %s`
59parseErrorTemplContentNoIn = `parsing %s from %q failed, because %s`
60)
61
62// NewParseError creates a new parse error
63func NewParseError(name, in, value string, reason error) *ParseError {
64var msg string
65if in == "" {
66msg = fmt.Sprintf(parseErrorTemplContentNoIn, name, value, reason)
67} else {
68msg = fmt.Sprintf(parseErrorTemplContent, name, in, value, reason)
69}
70return &ParseError{
71code: 400,
72Name: name,
73In: in,
74Value: value,
75Reason: reason,
76message: msg,
77}
78}
79