-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patherror.go
141 lines (120 loc) · 4.96 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2020 xgfone
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ship
import (
"errors"
"fmt"
"net/http"
)
// Some non-HTTP Errors
var (
ErrMissingContentType = errors.New("missing the header 'Content-Type'")
ErrInvalidRedirectCode = errors.New("invalid redirect status code")
ErrSessionNotExist = errors.New("session does not exist")
ErrInvalidSession = errors.New("invalid session")
)
// Some HTTP error.
var (
ErrBadRequest = NewHTTPServerError(http.StatusBadRequest)
ErrUnauthorized = NewHTTPServerError(http.StatusUnauthorized)
ErrForbidden = NewHTTPServerError(http.StatusForbidden)
ErrNotFound = NewHTTPServerError(http.StatusNotFound)
ErrMethodNotAllowed = NewHTTPServerError(http.StatusMethodNotAllowed)
ErrStatusNotAcceptable = NewHTTPServerError(http.StatusNotAcceptable)
ErrRequestTimeout = NewHTTPServerError(http.StatusRequestTimeout)
ErrStatusConflict = NewHTTPServerError(http.StatusConflict)
ErrStatusGone = NewHTTPServerError(http.StatusGone)
ErrStatusRequestEntityTooLarge = NewHTTPServerError(http.StatusRequestEntityTooLarge)
ErrUnsupportedMediaType = NewHTTPServerError(http.StatusUnsupportedMediaType)
ErrTooManyRequests = NewHTTPServerError(http.StatusTooManyRequests)
ErrInternalServerError = NewHTTPServerError(http.StatusInternalServerError)
ErrStatusNotImplemented = NewHTTPServerError(http.StatusNotImplemented)
ErrBadGateway = NewHTTPServerError(http.StatusBadGateway)
ErrServiceUnavailable = NewHTTPServerError(http.StatusServiceUnavailable)
ErrStatusGatewayTimeout = NewHTTPServerError(http.StatusGatewayTimeout)
ErrStatusHTTPVersionNotSupported = NewHTTPServerError(http.StatusHTTPVersionNotSupported)
)
// ErrSkip is not an error, which is used to suggest that the middeware should
// skip and return it back to the outer middleware to handle.
var ErrSkip = errors.New("skip")
// RouteError represents a route error when adding a route.
type RouteError struct {
Err error
Route
}
func (re RouteError) Error() string {
if re.Name == "" {
return fmt.Sprintf("%s: path=%s, method=%s", re.Err, re.Path, re.Method)
}
return fmt.Sprintf("%s: name=%s, path=%s, method=%s",
re.Err, re.Name, re.Path, re.Method)
}
// HTTPServerError represents a server error with HTTP Status Code.
type HTTPServerError struct {
Code int
Err error
CT string // Content-Type
}
// NewHTTPServerError returns a new HTTPServerError.
func NewHTTPServerError(code int, msg ...string) HTTPServerError {
if len(msg) == 0 {
return HTTPServerError{Code: code, Err: errors.New(http.StatusText(code))}
}
return HTTPServerError{Code: code, Err: errors.New(msg[0])}
}
func (e HTTPServerError) Error() string { return e.Err.Error() }
// Unwrap unwraps the inner error.
func (e HTTPServerError) Unwrap() error { return e.Err }
// NewCT returns a new HTTPServerError with the new ContentType ct.
func (e HTTPServerError) NewCT(ct string) HTTPServerError { e.CT = ct; return e }
// New returns a new HTTPServerError with the new error.
func (e HTTPServerError) New(err error) HTTPServerError { e.Err = err; return e }
// Newf is equal to New(fmt.Errorf(msg, args...)).
func (e HTTPServerError) Newf(msg string, args ...interface{}) HTTPServerError {
if len(args) == 0 {
return e.New(errors.New(msg))
}
return e.New(fmt.Errorf(msg, args...))
}
// HTTPClientError represents an response error from the http client.
type HTTPClientError struct {
Code int `json:"code" xml:"code"`
Method string `json:"method" xml:"method"`
URL string `json:"url" xml:"url"`
Data string `json:"data" xml:"data"`
Err error `json:"err" xml:"err"`
}
// NewHTTPClientError returns a new HTTPClientError.
func NewHTTPClientError(method, url string, code int, err error,
data ...string) HTTPClientError {
var d string
if len(data) > 0 {
d = data[0]
}
return HTTPClientError{Method: method, URL: url, Code: code, Data: d, Err: err}
}
func (e HTTPClientError) Unwrap() error { return e.Err }
func (e HTTPClientError) String() string { return e.Error() }
func (e HTTPClientError) Error() string {
var err string
if e.Err != nil {
err = fmt.Sprintf(", err=%s", e.Err.Error())
}
var data string
if e.Data != "" {
data = fmt.Sprintf(", data=%s", e.Data)
}
return fmt.Sprintf("method=%s, url=%s, code=%d%s%s",
e.Method, e.URL, e.Code, data, err)
}