forked from qiangxue/fasthttp-routing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
40 lines (33 loc) · 1.1 KB
/
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
// Copyright 2016 Qiang Xue. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package routing
import "net/http"
// HTTPError represents an HTTP error with HTTP status code and error message
type HTTPError interface {
error
// StatusCode returns the HTTP status code of the error
StatusCode() int
}
// Error contains the error information reported by calling Context.Error().
type httpError struct {
Status int `json:"status" xml:"status"`
Message string `json:"message" xml:"message"`
}
// NewHTTPError creates a new HttpError instance.
// If the error message is not given, http.StatusText() will be called
// to generate the message based on the status code.
func NewHTTPError(status int, message ...string) HTTPError {
if len(message) > 0 {
return &httpError{status, message[0]}
}
return &httpError{status, http.StatusText(status)}
}
// Error returns the error message.
func (e *httpError) Error() string {
return e.Message
}
// StatusCode returns the HTTP status code.
func (e *httpError) StatusCode() int {
return e.Status
}