-
Notifications
You must be signed in to change notification settings - Fork 1
/
error.go
39 lines (32 loc) · 909 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
package main
import (
"errors"
"fmt"
"net/http"
)
var (
// ErrInternalServer is a 500 error
ErrInternalServer = NewError(http.StatusInternalServerError, errors.New("internal server error"))
// ErrNotFound is a 404 error.
ErrNotFound = NewError(http.StatusNotFound, errors.New("not found"))
// ErrMethodNotAllowed is a 405 error.
ErrMethodNotAllowed = NewError(http.StatusMethodNotAllowed, errors.New("method not allowed"))
)
// NewErrBadRequestF returns a 404 bad request error
func NewErrNotFoundF(format string, a ...interface{}) Error {
return NewError(http.StatusNotFound, fmt.Errorf(format, a...))
}
// Error is a HTTP error.
type Error interface {
error
Status() int
}
type httpError struct {
error
status int
}
func (e *httpError) Status() int { return e.status }
// NewError creates a new HTTP error.
func NewError(status int, err error) Error {
return &httpError{err, status}
}