-
Notifications
You must be signed in to change notification settings - Fork 0
/
responses.go
70 lines (62 loc) · 1.64 KB
/
responses.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
package api
import (
"net/http"
"github.com/labstack/echo/v4"
)
type (
ResponseMessage struct {
Message string `json:"message"`
Data map[string]interface{} `json:"data,omitempty"`
}
)
func RespMessage(m string, k *string, i interface{}) interface{} {
r := new(ResponseMessage)
r.Message = m
if k != nil && i != nil {
data := make(map[string]interface{})
data[*k] = i
}
return r
}
func RespNoContent(k string, i interface{}) interface{} {
data := make(map[string]interface{})
data[k] = i
response := new(ResponseMessage)
response.Message = "Not found"
response.Data = data
return response
}
func RespConflict(k string, i interface{}) interface{} {
data := make(map[string]interface{})
data[k] = i
response := new(ResponseMessage)
response.Message = "Model already exists"
response.Data = data
return response
}
func RespInternelServerError() interface{} {
response := new(ResponseMessage)
response.Message = "Internel server error, please check logs"
return response
}
func RespCreated() interface{} {
response := new(ResponseMessage)
response.Message = "Successful created"
return response
}
func RespHandlingError(c echo.Context, api_err ApiError, i interface{}) (err error) {
if api_err.Error != nil {
if api_err.Error == ErrorUnauthorized {
return c.JSON(http.StatusUnauthorized, api_err.Message)
}
if api_err.Error == ErrorForbidden {
return c.JSON(http.StatusForbidden, api_err.Message)
}
if api_err.Error == ErrorConflict {
return c.JSON(http.StatusConflict, api_err.Message)
}
api_err.LogError(c, i)
return c.JSON(http.StatusInternalServerError, api_err.Message)
}
return nil
}