-
Notifications
You must be signed in to change notification settings - Fork 19
/
errors.go
72 lines (60 loc) · 1.93 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
63
64
65
66
67
68
69
70
71
72
package gongular
import (
"errors"
"fmt"
"net/http"
"reflect"
"strings"
)
var defaultErrorHandler = func(err error, c *Context) {
c.logger.Println("An error has occurred:", err)
switch err := err.(type) {
case InjectionError:
c.MustStatus(http.StatusInternalServerError)
c.logger.Println("Could not inject the requested field", err)
case ValidationError:
c.MustStatus(http.StatusBadRequest)
c.SetBody(map[string]interface{}{"ValidationError": err})
case ParseError:
c.MustStatus(http.StatusBadRequest)
c.SetBody(map[string]interface{}{"ParseError": err})
default:
c.SetBody(err.Error())
c.MustStatus(http.StatusInternalServerError)
}
c.StopChain()
}
// ErrorHandler is generic interface for error handling
type ErrorHandler func(err error, c *Context)
// ErrNoSuchDependency is thrown whenever the requested interface could not be found in the injector
var ErrNoSuchDependency = errors.New("No such dependency exists")
// InjectionError occurs whenever the listed dependency cannot be injected
type InjectionError struct {
Tip reflect.Type
Key string
UnderlyingError error
}
func (i InjectionError) Error() string {
return fmt.Sprintf("Could not inject type %s with key %s because %s", i.Key, i.Tip, i.UnderlyingError.Error())
}
// ValidationError occurs whenever one or more fields fail the validation by govalidator
type ValidationError struct {
Fields map[string]string
Place string
}
func (v ValidationError) Error() string {
s := []string{}
for k, v := range v.Fields {
s = append(s, fmt.Sprintf("%s: %s", k, v))
}
return fmt.Sprintf("Validation error in %s, %s", v.Place, strings.Join(s, ","))
}
// ParseError occurs whenever the field cannot be parsed, i.e. type mismatch
type ParseError struct {
Place string
FieldName string `json:",omitempty"`
Reason string
}
func (p ParseError) Error() string {
return fmt.Sprintf("Parse error: %s %s %s", p.Place, p.FieldName, p.Reason)
}