-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
52 lines (44 loc) · 1.05 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
package api
import (
"encoding/json"
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
var (
unsuccessful = Reply{Success: false}
notFound = Reply{Success: false, Error: "resource not found"}
notAllowed = Reply{Success: false, Error: "method not allowed"}
)
// ErrorResponse constructs an new response from the error or returns a success: false.
func ErrorResponse(err interface{}) Reply {
if err == nil {
return unsuccessful
}
rep := Reply{Success: false}
switch err := err.(type) {
case error:
rep.Error = err.Error()
case string:
rep.Error = err
case fmt.Stringer:
rep.Error = err.String()
case json.Marshaler:
data, e := err.MarshalJSON()
if e != nil {
panic(err)
}
rep.Error = string(data)
default:
rep.Error = "unhandled error response"
}
return rep
}
// NotFound returns a JSON 404 response for the API.
func NotFound(c *gin.Context) {
c.JSON(http.StatusNotFound, notFound)
}
// NotAllowed returns a JSON 405 response for the API.
func NotAllowed(c *gin.Context) {
c.JSON(http.StatusMethodNotAllowed, notAllowed)
}