-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.go
125 lines (106 loc) · 2.76 KB
/
engine.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
package echo
import (
"encoding/json"
"encoding/xml"
"fmt"
"net/http"
"github.com/im-kulikov/helium/module"
"github.com/labstack/echo/v4"
"github.com/spf13/viper"
"go.uber.org/dig"
"go.uber.org/zap"
)
type (
errorResponse struct {
Error string `json:"error"`
Stack []string `json:"stack"`
Result []string `json:"result"`
}
// EngineParams struct
EngineParams struct {
dig.In
Config *viper.Viper `optional:"true"`
Binder echo.Binder `optional:"true"`
Logger *zap.Logger `optional:"true"`
EchoLogger echo.Logger `optional:"true"`
Validator echo.Validator `optional:"true"`
}
// CustomError interface
CustomError interface {
FormatResponse(ctx echo.Context) error
}
)
// Module engine
var Module = module.Module{
{Constructor: NewValidator},
{Constructor: NewBinder},
{Constructor: NewEngine},
}
func captureError(log *zap.Logger) echo.HTTPErrorHandler {
return func(err error, ctx echo.Context) {
var (
message string
code = http.StatusBadRequest
result = make([]string, 0)
trace = make([]string, 0)
)
switch custom := err.(type) {
case CustomError:
if err = custom.FormatResponse(ctx); err != nil {
log.Error("Capture error",
zap.Error(err))
}
return
case *json.UnmarshalTypeError:
message = fmt.Sprintf("JSON parse error: expected=%v, got=%v, offset=%v", custom.Type, custom.Value, custom.Offset)
case *json.SyntaxError:
message = fmt.Sprintf("JSON parse error: offset=%v, error=%v", custom.Offset, custom.Error())
case *xml.UnsupportedTypeError:
message = fmt.Sprintf("XML parse error: type=%v, error=%v", custom.Type, custom.Error())
case *xml.SyntaxError:
message = fmt.Sprintf("XML parse error: line=%v, error=%v", custom.Line, custom.Error())
case *echo.HTTPError:
code = custom.Code
message = custom.Message.(string)
default:
message = http.StatusText(code)
}
// Capture errors:
if code >= http.StatusInternalServerError {
log.Error("Request error",
zap.Error(err))
}
if errJSON := ctx.JSON(code, errorResponse{
Error: message,
Stack: trace,
Result: result,
}); errJSON != nil {
log.Error("Capture error",
zap.NamedError("json", errJSON),
zap.Error(err))
}
}
}
// NewEngine returns configured echo engine
func NewEngine(params EngineParams) *echo.Echo {
e := echo.New()
e.HidePort = true
e.HideBanner = true
if params.Config != nil && params.Config.GetBool("api.debug") {
e.Debug = true
}
if params.Binder != nil {
e.Binder = params.Binder
}
if params.EchoLogger != nil {
e.Logger = params.EchoLogger
}
if params.Logger != nil {
e.Logger = NewLogger(params.Logger)
e.HTTPErrorHandler = captureError(params.Logger)
}
if params.Validator != nil {
e.Validator = params.Validator
}
return e
}