/
error.go
195 lines (168 loc) · 3.39 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package api
import (
"errors"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
"github.com/konveyor/tackle2-hub/api/filter"
"github.com/konveyor/tackle2-hub/api/sort"
"github.com/konveyor/tackle2-hub/model"
"github.com/mattn/go-sqlite3"
"gorm.io/gorm"
"net/http"
"os"
)
//
// BadRequestError reports bad request errors.
type BadRequestError struct {
Reason string
}
func (r *BadRequestError) Error() string {
return r.Reason
}
func (r *BadRequestError) Is(err error) (matched bool) {
_, matched = err.(*BadRequestError)
return
}
//
// BatchError reports errors stemming from batch operations.
type BatchError struct {
Message string
Items []BatchErrorItem
}
type BatchErrorItem struct {
Error error
Resource interface{}
}
func (r BatchError) Error() string {
return r.Message
}
func (r BatchError) Is(err error) (matched bool) {
_, matched = err.(BatchError)
return
}
//
// TrackerError reports an error stemming from the Hub being unable
// to communicate with an external issue tracker.
type TrackerError struct {
Reason string
}
func (r *TrackerError) Error() string {
return r.Reason
}
func (r *TrackerError) Is(err error) (matched bool) {
_, matched = err.(*TrackerError)
return
}
//
// Forbidden reports auth errors.
type Forbidden struct {
Reason string
}
func (r *Forbidden) Error() string {
return r.Reason
}
func (r *Forbidden) Is(err error) (matched bool) {
_, matched = err.(*Forbidden)
return
}
//
// ErrorHandler handles error conditions from lower handlers.
func ErrorHandler() gin.HandlerFunc {
return func(ctx *gin.Context) {
ctx.Next()
if len(ctx.Errors) == 0 {
return
}
err := ctx.Errors[0]
rtx := WithContext(ctx)
if errors.Is(err, &BadRequestError{}) ||
errors.Is(err, &filter.Error{}) ||
errors.Is(err, &sort.SortError{}) ||
errors.Is(err, validator.ValidationErrors{}) {
rtx.Respond(
http.StatusBadRequest,
gin.H{
"error": err.Error(),
})
return
}
if errors.Is(err, gorm.ErrRecordNotFound) {
if ctx.Request.Method == http.MethodDelete {
rtx.Status(http.StatusNoContent)
return
}
rtx.Respond(
http.StatusNotFound,
gin.H{
"error": err.Error(),
})
return
}
if errors.Is(err, os.ErrNotExist) {
rtx.Respond(
http.StatusNotFound,
gin.H{
"error": err.Error(),
})
return
}
if errors.Is(err, model.DependencyCyclicError{}) {
rtx.Respond(
http.StatusConflict,
gin.H{
"error": err.Error(),
})
return
}
if errors.Is(err, &TrackerError{}) {
rtx.Respond(
http.StatusServiceUnavailable,
gin.H{
"error": err.Error(),
})
return
}
if errors.Is(err, &Forbidden{}) {
rtx.Respond(
http.StatusForbidden,
gin.H{
"error": err.Error(),
})
return
}
sqliteErr := &sqlite3.Error{}
if errors.As(err, sqliteErr) {
switch sqliteErr.ExtendedCode {
case sqlite3.ErrConstraintUnique,
sqlite3.ErrConstraintPrimaryKey:
rtx.Respond(
http.StatusConflict,
gin.H{
"error": err.Error(),
})
return
}
}
bErr := &BatchError{}
if errors.As(err, bErr) {
rtx.Respond(
http.StatusBadRequest,
bErr.Items,
)
return
}
rtx.Respond(
http.StatusInternalServerError,
gin.H{
"error": err.Error(),
})
url := ctx.Request.URL.String()
log.Error(
err.Err,
"Request failed.",
"method",
ctx.Request.Method,
"url",
url)
}
}