-
Notifications
You must be signed in to change notification settings - Fork 1
/
error.go
67 lines (55 loc) · 1.34 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package pi
import (
"fmt"
"strconv"
)
// HTTPError represents a HTTP Error.
type HTTPError interface {
error
ContentType() string
StatusCode() int
}
type _JSONError struct {
statusCode int
err string
template string
}
func (error _JSONError) Error() string {
return fmt.Sprintf(error.template, int(error.statusCode), strconv.Quote(error.err))
}
func (error _JSONError) StatusCode() int {
return error.statusCode
}
func (error _JSONError) ContentType() string {
return "application/json; charset=UTF-8"
}
type _XMLError struct {
statusCode int
err string
template string
}
func (error _XMLError) Error() string {
return fmt.Sprintf(error.template, int(error.statusCode), error.err)
}
func (error _XMLError) ContentType() string {
return "application/xml; charset=UTF-8"
}
func (error _XMLError) StatusCode() int {
return error.statusCode
}
// NewError returns a new HTTPError, and outputs it as JSON.
func NewError(statusCode int, err error) HTTPError {
return _JSONError{
statusCode: statusCode,
err: err.Error(),
template: `{"errorCode": %d, "errorMessage": %s}`,
}
}
// NewXMLError returns a new HTTPError, and outputs it as XML.
func NewXMLError(statusCode int, err error) HTTPError {
return _XMLError{
statusCode: statusCode,
err: err.Error(),
template: `<error code="%d">%s</error>`,
}
}