/
error.go
52 lines (43 loc) · 836 Bytes
/
error.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
package rack
import (
"errors"
"net/http"
)
type (
// StatusError represents a status code error
StatusError struct {
code int
err error
}
statusError interface {
Code() int
error
}
)
// StatusCode returns the status code for the specified error
func StatusCode(err error) int {
var se statusError
if errors.As(err, &se) {
return se.Code()
}
return http.StatusInternalServerError
}
// WrapError wraps the specified error
func WrapError(code int, err error) *StatusError {
return &StatusError{
code: code,
err: err,
}
}
// Code returns the error status code
func (e *StatusError) Code() int {
return e.code
}
// Error returns the error message
func (e *StatusError) Error() string {
return e.err.Error()
}
// Unwrap returns the wrapped error
func (e *StatusError) Unwrap() error {
return e.err
}