-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.go
59 lines (50 loc) · 1.87 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
52
53
54
55
56
57
58
59
package gospeak
import (
"errors"
"fmt"
)
// Deprecated: This type may be removed in the future.
type WebRPCError struct {
Name string `json:"error"`
Code int `json:"code"`
Message string `json:"msg"`
Cause string `json:"cause,omitempty"`
HTTPStatus int `json:"status"`
cause error
}
var _ error = WebRPCError{}
func (e WebRPCError) Error() string {
if e.cause != nil {
return fmt.Sprintf("%s %d: %s: %v", e.Name, e.Code, e.Message, e.cause)
}
return fmt.Sprintf("%s %d: %s", e.Name, e.Code, e.Message)
}
func (e WebRPCError) Is(target error) bool {
if rpcErr, ok := target.(WebRPCError); ok {
return rpcErr.Code == e.Code
}
return errors.Is(e.cause, target)
}
func (e WebRPCError) Unwrap() error {
return e.cause
}
func (e WebRPCError) WithCause(cause error) WebRPCError {
err := e
err.cause = cause
err.Cause = cause.Error()
return err
}
func (e WebRPCError) StackFrames() []uintptr {
return nil
}
// Webrpc errors
var (
ErrWebrpcEndpoint = WebRPCError{Code: 0, Name: "WebrpcEndpoint", Message: "endpoint error", HTTPStatus: 400}
ErrWebrpcRequestFailed = WebRPCError{Code: -1, Name: "WebrpcRequestFailed", Message: "request failed", HTTPStatus: 400}
ErrWebrpcBadRoute = WebRPCError{Code: -2, Name: "WebrpcBadRoute", Message: "bad route", HTTPStatus: 404}
ErrWebrpcBadMethod = WebRPCError{Code: -3, Name: "WebrpcBadMethod", Message: "bad method", HTTPStatus: 405}
ErrWebrpcBadRequest = WebRPCError{Code: -4, Name: "WebrpcBadRequest", Message: "bad request", HTTPStatus: 400}
ErrWebrpcBadResponse = WebRPCError{Code: -5, Name: "WebrpcBadResponse", Message: "bad response", HTTPStatus: 500}
ErrWebrpcServerPanic = WebRPCError{Code: -6, Name: "WebrpcServerPanic", Message: "server panic", HTTPStatus: 500}
ErrWebrpcInternalError = WebRPCError{Code: -7, Name: "WebrpcInternalError", Message: "internal error", HTTPStatus: 500}
)