-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patherror.go
135 lines (123 loc) · 3.6 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* @license
* Copyright 2017 Telefónica Investigación y Desarrollo, S.A.U
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package govice
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
)
// Error is a custom error. This struct stores information to generate an HTTP error response if required.
type Error struct {
Message string `json:"-"`
Status int `json:"-"`
Alarm string `json:"-"`
Code string `json:"error"`
Description string `json:"error_description,omitempty"`
}
func (e *Error) Error() string {
return e.Message
}
// Response generates a JSON document for an Error.
// JSON is in the form: {"error": "invalid_request", "error_description": "xxx"}
func (e *Error) Response(w http.ResponseWriter) {
data, err := json.Marshal(e)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(e.Status)
w.Write(data)
}
// GetResponse to get a http.Response object from an Error.
func (e *Error) GetResponse() *http.Response {
r := &http.Response{}
if data, err := json.Marshal(e); err != nil {
r.StatusCode = http.StatusInternalServerError
} else {
r.Header = make(http.Header)
r.Header.Add("Content-Type", "application/json")
r.StatusCode = e.Status
r.Body = ioutil.NopCloser(bytes.NewReader(data))
}
return r
}
// NewServerError to create a server_error Error
func NewServerError(message string) *Error {
return &Error{
Message: message,
Status: http.StatusInternalServerError,
Code: "server_error",
}
}
// NewBadGatewayError to create a bad gateway error.
func NewBadGatewayError(message string) *Error {
return &Error{
Message: message,
Status: http.StatusBadGateway,
Code: "server_error",
}
}
// NewInvalidRequestError to create an invalid_request Error
func NewInvalidRequestError(message string, description string) *Error {
return &Error{
Message: message,
Status: http.StatusBadRequest,
Code: "invalid_request",
Description: description,
}
}
// NewUnauthorizedClientError to create an unauthorized_client Error
func NewUnauthorizedClientError(message string, description string) *Error {
return &Error{
Message: message,
Status: http.StatusForbidden,
Code: "unauthorized_client",
Description: description,
}
}
// NotFoundError with a not found Error
var NotFoundError = &Error{
Message: "not found",
Status: http.StatusNotFound,
Code: "invalid_request",
Description: "not found",
}
// ReplyWithError to send a HTTP response with the error document.
func ReplyWithError(w http.ResponseWriter, r *http.Request, err error) {
logger := GetLogger(r)
switch e := err.(type) {
case *Error:
if logger != nil {
if e.Status >= http.StatusBadRequest && e.Status < http.StatusInternalServerError {
logger.Info(err.Error())
} else if e.Alarm != "" {
logContext := LogContext{Alarm: e.Alarm}
logger.ErrorC(logContext, err.Error())
} else {
logger.Error(err.Error())
}
}
e.Response(w)
default:
if logger != nil {
logger.Error(err.Error())
}
NewServerError("").Response(w)
}
}