/
errors.go
executable file
·50 lines (41 loc) · 1.35 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
package errors
import (
"net/http"
"sort"
"github.com/go-ozzo/ozzo-validation"
)
type validationError struct {
Field string `json:"field"`
Error string `json:"error"`
}
// InternalServerError creates a new API error representing an internal server error (HTTP 500)
func InternalServerError(err error) *APIError {
return NewHTTPError(http.StatusInternalServerError, "INTERNAL_SERVER_ERROR", Params{"error": err.Error()})
}
// NotFound creates a new API error representing a resource-not-found error (HTTP 404)
func NotFound(resource string) *APIError {
return NewHTTPError(http.StatusNotFound, "NOT_FOUND", Params{"resource": resource})
}
// Unauthorized creates a new API error representing an authentication failure (HTTP 401)
func Unauthorized(err string) *APIError {
return NewHTTPError(http.StatusUnauthorized, "UNAUTHORIZED", Params{"error": err})
}
// InvalidData converts a data validation error into an API error (HTTP 400)
func InvalidData(errs validation.Errors) *APIError {
result := []validationError{}
fields := []string{}
for field := range errs {
fields = append(fields, field)
}
sort.Strings(fields)
for _, field := range fields {
err := errs[field]
result = append(result, validationError{
Field: field,
Error: err.Error(),
})
}
err := NewHTTPError(http.StatusBadRequest, "INVALID_DATA", nil)
err.Details = result
return err
}