...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package errors
16
17 import (
18 "encoding/json"
19 "fmt"
20 )
21
22
23 type ParseError struct {
24 code int32
25 Name string
26 In string
27 Value string
28 Reason error
29 message string
30 }
31
32 func (e *ParseError) Error() string {
33 return e.message
34 }
35
36
37 func (e *ParseError) Code() int32 {
38 return e.code
39 }
40
41
42 func (e ParseError) MarshalJSON() ([]byte, error) {
43 var reason string
44 if e.Reason != nil {
45 reason = e.Reason.Error()
46 }
47 return 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
57 const (
58 parseErrorTemplContent = `parsing %s %s from %q failed, because %s`
59 parseErrorTemplContentNoIn = `parsing %s from %q failed, because %s`
60 )
61
62
63 func NewParseError(name, in, value string, reason error) *ParseError {
64 var msg string
65 if in == "" {
66 msg = fmt.Sprintf(parseErrorTemplContentNoIn, name, value, reason)
67 } else {
68 msg = fmt.Sprintf(parseErrorTemplContent, name, in, value, reason)
69 }
70 return &ParseError{
71 code: 400,
72 Name: name,
73 In: in,
74 Value: value,
75 Reason: reason,
76 message: msg,
77 }
78 }
79
View as plain text