-
Notifications
You must be signed in to change notification settings - Fork 16
/
error.go
45 lines (37 loc) · 1.21 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
// SPDX-License-Identifier: Apache-2.0
package ocpi
import (
"github.com/go-chi/render"
"net/http"
)
type ErrResponse struct {
Err error `json:"-"` // low-level runtime error
HTTPStatusCode int `json:"-"` // http response status code
StatusText string `json:"status"` // user-level status message
AppCode int64 `json:"code,omitempty"` // application-specific error code
ErrorText string `json:"error,omitempty"` // application-level error message, for debugging
}
func (e *ErrResponse) Render(w http.ResponseWriter, r *http.Request) error {
render.Status(r, e.HTTPStatusCode)
return nil
}
func ErrInvalidRequest(err error) render.Renderer {
return &ErrResponse{
Err: err,
HTTPStatusCode: http.StatusBadRequest,
StatusText: http.StatusText(http.StatusBadRequest),
ErrorText: err.Error(),
}
}
func ErrInternalError(err error) render.Renderer {
return &ErrResponse{
Err: err,
HTTPStatusCode: http.StatusInternalServerError,
StatusText: http.StatusText(http.StatusInternalServerError),
ErrorText: err.Error(),
}
}
var ErrNotFound = &ErrResponse{
HTTPStatusCode: http.StatusNotFound,
StatusText: http.StatusText(http.StatusNotFound),
}