-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy patherrors.go
51 lines (41 loc) · 1.17 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
package server
import (
"fmt"
"net/http"
"github.com/x1unix/go-playground/pkg/goplay"
)
// ErrSnippetTooLarge is snippet max size limit error
var ErrSnippetTooLarge = Errorf(
http.StatusRequestEntityTooLarge,
"code snippet too large (max %d bytes)",
goplay.MaxSnippetSize,
)
// HTTPError is HTTP response error
type HTTPError struct {
code int
parent error
}
// Error implements error
func (err *HTTPError) Error() string {
return fmt.Sprintf("%s: %s", http.StatusText(err.code), err.parent)
}
// Unwrap implements error
func (err *HTTPError) Unwrap() error {
return err.parent
}
// WriteResponse writes error to response
func (err *HTTPError) WriteResponse(rw http.ResponseWriter) {
resp := ErrorResponse{code: err.code, Error: err.parent.Error()}
resp.Write(rw)
}
// NewHTTPError constructs a new error
func NewHTTPError(code int, err error) *HTTPError {
return &HTTPError{code: code, parent: err}
}
// Errorf returns new formatted error
func Errorf(code int, format string, args ...interface{}) *HTTPError {
return NewHTTPError(code, fmt.Errorf(format, args...))
}
func NewBadRequestError(err error) *HTTPError {
return NewHTTPError(http.StatusBadRequest, err)
}