This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
115 lines (99 loc) · 2.69 KB
/
handler.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package handler
import (
"database/sql"
"fmt"
"io"
"log"
"net/http"
"net/url"
"github.com/dmitrymomot/media-srv/repository"
"github.com/dmitrymomot/media-srv/storage"
"github.com/pkg/errors"
)
type (
// Wrap function makes a handler compatible with the default go mux interface
Wrap func(w http.ResponseWriter, r *http.Request) error
// ValidationError ...
ValidationError struct {
errBag url.Values
}
// HTTPError struct
HTTPError struct {
Code int `json:"code"`
Message interface{} `json:"message"`
}
resizerFunc func(f io.ReadCloser, w, h int) (io.ReadSeeker, error)
// Handler struct
Handler struct {
db *sql.DB
query *repository.Queries
storage *storage.Interactor
resize resizerFunc
}
)
// NewHTTPError factory
func NewHTTPError(code int, msg interface{}) error {
return HTTPError{Code: code, Message: msg}
}
// NewValidationError factory
func NewValidationError(errs url.Values) *ValidationError {
return &ValidationError{errBag: errs}
}
// Add validation errors to errors bag
func (e *ValidationError) Add(key string, vals []string) {
if e.errBag == nil {
e.errBag = url.Values{}
}
e.errBag[key] = vals
}
// Set validation errors bag
func (e *ValidationError) Set(errBag url.Values) {
if e.errBag == nil {
e.errBag = url.Values{}
}
e.errBag = errBag
}
// GetAll validation errors
func (e *ValidationError) GetAll() url.Values {
return e.errBag
}
// Implementation of the error interface
func (e *ValidationError) Error() string {
return fmt.Sprintf("validationError: %v", e.errBag)
}
// Implementation of the error interface
func (e HTTPError) Error() string {
return fmt.Sprintf("code: %d; message: %v", e.Code, e.Message)
}
// ServeHTTP func is http.Handler interface implementation
func (h Wrap) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err := h(w, r); err != nil {
switch err.(type) {
case *ValidationError:
er := err.(*ValidationError)
jsonResponse(w, http.StatusUnprocessableEntity, data{"validationError": er.GetAll()})
return
case HTTPError:
er := err.(HTTPError)
jsonResponse(w, er.Code, data{"error": er.Message})
return
}
if errors.Is(err, sql.ErrNoRows) {
jsonResponse(w, http.StatusNotFound, data{"error": http.StatusText(http.StatusNotFound)})
return
}
log.Println(errors.Wrap(err, "undefined http error"))
jsonResponse(w, http.StatusInternalServerError, data{"error": http.StatusText(http.StatusInternalServerError)})
return
}
}
// New is a factory function,
// returns a new instance of the HTTP handler interactor
func New(db *sql.DB, q *repository.Queries, s *storage.Interactor, rf resizerFunc) *Handler {
return &Handler{
db: db,
query: q,
storage: s,
resize: rf,
}
}