-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
92 lines (75 loc) · 2.32 KB
/
response.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
package request
import (
"net/http"
"github.com/macinnir/dvc/core/lib/utils/errors"
)
// Response is the API response handler
type Response struct {
// userLogDAL dal.IUserLogDAL
// userDeviceDAL dal.IUserDeviceDAL
// auth integrations.IAuth
// userDAL dal.IUserDAL
}
// NewResponse returns a new response object
func NewResponse() *Response {
return &Response{}
}
// HandleError handles errors returned from the service layer and
// calls a api error handler to return the corresponding HTTP response
func (s *Response) HandleError(r *Request, w http.ResponseWriter, e error) {
// t := reflect.TypeOf(e)
switch e.(type) {
case errors.ArgumentError:
s.BadRequest(r, w, e)
case errors.InternalError:
s.InternalServerError(r, w, e)
case errors.ForbiddenError:
s.Forbidden(r, w)
case errors.RecordNotFoundError:
s.NotFound(r, w)
case errors.NotAuthorizedError:
s.Unauthorized(r, w)
default:
s.InternalServerError(r, w, e)
}
}
// NotImplemented shows a text response for not implemented method (501)
func (s *Response) NotImplemented(r *Request, w http.ResponseWriter) {
NotImplemented(r, w)
}
// InternalServerError returns a 500 server error response
func (s *Response) InternalServerError(r *Request, w http.ResponseWriter, e error) {
InternalServerError(r, w, e)
}
// NotFound returns a not-found status
func (s *Response) NotFound(r *Request, w http.ResponseWriter) {
NotFound(r, w)
}
// BadRequest returns a bad request status (400)
func (s *Response) BadRequest(r *Request, w http.ResponseWriter, e error) {
BadRequest(r, w, e)
}
// Unauthorized returns an unauthorized status (401)
func (s *Response) Unauthorized(r *Request, w http.ResponseWriter) {
Unauthorized(r, w)
}
// Forbidden returns a forbidden status (403)
func (s *Response) Forbidden(r *Request, w http.ResponseWriter) {
Forbidden(r, w)
}
// NoContent returns a noContent status (204)
func (s *Response) NoContent(r *Request, w http.ResponseWriter) {
NoContent(r, w)
}
// Created returns a created status (201)
func (s *Response) Created(r *Request, w http.ResponseWriter) {
Created(r, w)
}
// JSON Returns an ok status with json-encoded body
func (s *Response) JSON(r *Request, w http.ResponseWriter, body interface{}) {
JSON(r, w, body)
}
// OK Returns an ok status
func (s *Response) OK(r *Request, w http.ResponseWriter) {
OK(r, w)
}