-
Notifications
You must be signed in to change notification settings - Fork 0
/
v1.go
44 lines (37 loc) · 1.23 KB
/
v1.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
// Package v1 represents types used by the web application for v1.
package v1
import "errors"
// ErrorResponse is the form used for API responses from failures in the API.
type ErrorResponse struct {
Error string `json:"error"`
Fields map[string]string `json:"fields,omitempty"`
}
// RequestError is used to pass an error during the request through the
// application with web specific context.
type RequestError struct {
Err error
Status int
}
// NewRequestError wraps a provided error with an HTTP status code. This
// function should be used when handlers encounter expected errors.
func NewRequestError(err error, status int) error {
return &RequestError{err, status}
}
// Error implements the error interface. It uses the default message of the
// wrapped error. This is what will be shown in the services' logs.
func (err *RequestError) Error() string {
return err.Err.Error()
}
// IsRequestError checks if an error of type RequestError exists.
func IsRequestError(err error) bool {
var re *RequestError
return errors.As(err, &re)
}
// GetRequestError returns a copy of the RequestError pointer.
func GetRequestError(err error) *RequestError {
var re *RequestError
if !errors.As(err, &re) {
return nil
}
return re
}