-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
114 lines (94 loc) · 2.8 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
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
package ng
import (
"fmt"
"log"
)
//Errors is the container of the standard error
//from jsonapi.org
type Errors struct {
Errors []*Error `json:"errors"`
}
//NewErrors create new empty container
func NewErrors() *Errors {
return &Errors{}
}
//Append add new error to the container
func (e *Errors) Append(errs ...*Error) {
e.Errors = append(e.Errors, errs...)
}
//AppendSlice add new slice of error to the container
func (e *Errors) AppendSlice(errs []*Error) {
for _, err := range errs {
e.Errors = append(e.Errors, err)
}
}
//AppendErrors add new errors from the same errors structure
func (e *Errors) AppendErrors(errs *Errors) {
if len(errs.Errors) > 0 {
e.Errors = append(e.Errors, errs.Errors...)
}
}
//AppendError add new standard error from Go Package
//to the container
func (e *Errors) AppendError(status string, err error) {
e.Errors = append(e.Errors, NewFromError(status, err))
}
//AppendSliceError add new slice of standard error from
//Go package to the container
func (e *Errors) AppendSliceError(status string, errs []error) {
for _, err := range errs {
e.Errors = append(e.Errors, NewFromError(status, err))
}
}
//NewError add new error with status and detail
func (e *Errors) NewError(status, detail string) {
e.Errors = append(e.Errors, NewError(status, detail))
}
//NewFieldError add new error with field, and detail
func (e *Errors) NewFieldError(status, field, detail string) {
e.Errors = append(e.Errors, NewFieldError(status, field, detail))
}
//NotFound add new error with 404 not found
func (e *Errors) NotFound(detail string) {
e.Errors = append(e.Errors, NotFound(detail))
}
//InternalServerError add new error with 500 internal server error
func (e *Errors) InternalServerError(detail string) {
e.Errors = append(e.Errors, InternalServerError(detail))
}
//BadRequest add new error with 400 bad request
func (e *Errors) BadRequest(detail string) {
e.Errors = append(e.Errors, BadRequest(detail))
}
//Unauthorized add new error with 401 unauthorized
func (e *Errors) Unauthorized(detail string) {
e.Errors = append(e.Errors, Unauthorized(detail))
}
//Forbidden add new error with 403 forbidden
func (e *Errors) Forbidden(detail string) {
e.Errors = append(e.Errors, Forbidden(detail))
}
//Conflict add new error with 401 conflict
func (e *Errors) Conflict(detail string) {
e.Errors = append(e.Errors, Conflict(detail))
}
//IsError check if it has more than zero error(s)
func (e *Errors) IsError() bool {
return len(e.Errors) > 0
}
//LogPrint just print the error message using log from
//Go package
func (e *Errors) LogPrint() {
for _, err := range e.Errors {
log.Printf("%s", err.Detail)
log.Println()
}
}
//FmtPrint just print the error message using fmt from
//Go Package
func (e *Errors) FmtPrint() {
for _, err := range e.Errors {
fmt.Printf("%s", err.Detail)
fmt.Println()
}
}