-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
62 lines (52 loc) · 1.51 KB
/
errors.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
package hr
import (
"fmt"
"io"
"net/http"
)
type Error struct {
Code int
Detail string
}
func (e Error) Error() string {
return fmt.Sprintf(`{"code":%d,"detail":"%s"}`, e.Code, e.Detail)
}
func (e Error) WriteTo(w io.Writer) (int64, error) {
if rw, ok := w.(http.ResponseWriter); ok {
code := e.Code
if code >= 1000 {
// when it's self-defined error code, we use a 200 status code
// in the response header instead, leaving the error handling
// to the client.
code = http.StatusOK
}
rw.Header().Set("Content-Type", "application/json; charset=utf-8")
rw.WriteHeader(code)
}
n, err := fmt.Fprint(w, e.Error())
return int64(n), err
}
func ParseError(err error) Error {
if herr, ok := err.(Error); ok {
return herr
}
return Error{
Code: http.StatusInternalServerError,
Detail: err.Error(),
}
}
func BadRequest(format string, v ...interface{}) Error {
return Error{Code: http.StatusBadRequest, Detail: fmt.Sprintf(format, v...)}
}
func Forbidden(format string, v ...interface{}) Error {
return Error{Code: http.StatusForbidden, Detail: fmt.Sprintf(format, v...)}
}
func Unauthorized(format string, v ...interface{}) Error {
return Error{Code: http.StatusUnauthorized, Detail: fmt.Sprintf(format, v...)}
}
func NotFound(format string, v ...interface{}) Error {
return Error{Code: http.StatusNotFound, Detail: fmt.Sprintf(format, v...)}
}
func InternalServerError(format string, v ...interface{}) Error {
return Error{Code: http.StatusInternalServerError, Detail: fmt.Sprintf(format, v...)}
}