forked from vulcand/vulcand
-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.go
69 lines (55 loc) · 1.38 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
package scroll
import (
"fmt"
"net/http"
)
type GenericAPIError struct {
Reason string
}
func (e GenericAPIError) Error() string {
return e.Reason
}
type MissingFieldError struct {
Field string
}
func (e MissingFieldError) Error() string {
return fmt.Sprintf("Missing mandatory parameter: %v", e.Field)
}
type InvalidFormatError struct {
Field string
Value string
}
func (e InvalidFormatError) Error() string {
return fmt.Sprintf("Invalid format for parameter %v: %v", e.Field, e.Value)
}
type InvalidParameterError struct {
Field string
Value string
}
func (e InvalidParameterError) Error() string {
return fmt.Sprintf("Invalid parameter: %v %v", e.Field, e.Value)
}
type NotFoundError struct {
Description string
}
func (e NotFoundError) Error() string {
return e.Description
}
type ConflictError struct {
Description string
}
func (e ConflictError) Error() string {
return e.Description
}
func responseAndStatusFor(err error) (Response, int) {
switch err.(type) {
case GenericAPIError, MissingFieldError, InvalidFormatError, InvalidParameterError:
return Response{"message": err.Error()}, http.StatusBadRequest
case NotFoundError:
return Response{"message": err.Error()}, http.StatusNotFound
case ConflictError:
return Response{"message": err.Error()}, http.StatusConflict
default:
return Response{"message": "Internal Server Error"}, http.StatusInternalServerError
}
}