-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
52 lines (43 loc) · 1.13 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
package hydrator
import (
"github.com/goadify/goadify/pkg/httperr"
"github.com/goadify/openapi/crud/go/gen"
"net/http"
)
type errorHydrator struct {
displayInternalMessages bool
}
type ErrorOption func(*errorHydrator)
func ErrorDisplayInternalMessages(display bool) ErrorOption {
return func(hydrator *errorHydrator) {
hydrator.displayInternalMessages = display
}
}
func Error(err error, options ...ErrorOption) *gen.ErrorStatusCode {
hydrator := new(errorHydrator)
for _, option := range options {
option(hydrator)
}
code := http.StatusInternalServerError
message := http.StatusText(code)
displayMessage := gen.OptString{}
if hydrator.displayInternalMessages {
message = err.Error()
}
if httpErr, ok := err.(httperr.ReadableHttpError); ok {
code = httpErr.Code()
message = httpErr.Message()
displayMessage.SetTo(httpErr.ReadableMessage())
} else if httpErr, ok := err.(httperr.HttpError); ok {
code = httpErr.Code()
message = httpErr.Message()
}
return &gen.ErrorStatusCode{
StatusCode: code,
Response: gen.Error{
Code: code,
Message: message,
DisplayMessage: displayMessage,
},
}
}