-
Notifications
You must be signed in to change notification settings - Fork 0
/
codes.go
120 lines (102 loc) · 4.28 KB
/
codes.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
// Copyright 2018 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package errcode
import "net/http"
var (
// InternalCode is equivalent to HTTP 500 Internal Server Error
InternalCode = NewCode("internal").SetHTTP(http.StatusInternalServerError)
// InvalidInputCode is equivalent to HTTP 400 Bad Request
InvalidInputCode = NewCode("input").SetHTTP(http.StatusBadRequest)
// NotFoundCode is equivalent to HTTP 404 Not Found
NotFoundCode = NewCode("missing").SetHTTP(http.StatusNotFound)
// StateCode is an error that is invalid due to the current object state
// This is mapped to HTTP 400
StateCode = NewCode("state").SetHTTP(http.StatusBadRequest)
)
// invalidInput gives the code InvalidInputCode
type invalidInputErr struct{ CodedError }
// NewInvalidInputErr creates an invalidInput from an err
// If the error is already an ErrorCode it will use that code
// Otherwise it will use InvalidInputCode which gives HTTP 400
func NewInvalidInputErr(err error) ErrorCode {
return invalidInputErr{NewCodedError(err, InvalidInputCode)}
}
var _ ErrorCode = (*invalidInputErr)(nil) // assert implements interface
var _ HasClientData = (*invalidInputErr)(nil) // assert implements interface
// internalError gives the code InvalidInputCode
type internalErr struct{ CodedError }
// NewInternalErr creates an internalError from an err
// If the given err is an ErrorCode that is a descendant of InternalCode,
// its code will be used.
// This ensures the intention of sending an HTTP 50x.
func NewInternalErr(err error) ErrorCode {
code := InternalCode
if errcode, ok := err.(ErrorCode); ok {
errCode := errcode.Code()
if errCode.IsAncestor(InternalCode) {
code = errCode
}
}
return internalErr{CodedError{GetCode: code, Err: err}}
}
var _ ErrorCode = (*internalErr)(nil) // assert implements interface
var _ HasClientData = (*internalErr)(nil) // assert implements interface
// notFound gives the code NotFoundCode
type notFoundErr struct{ CodedError }
// NewNotFoundErr creates a notFound from an err
// If the error is already an ErrorCode it will use that code
// Otherwise it will use NotFoundCode which gives HTTP 404
func NewNotFoundErr(err error) ErrorCode {
return notFoundErr{NewCodedError(err, NotFoundCode)}
}
var _ ErrorCode = (*notFoundErr)(nil) // assert implements interface
var _ HasClientData = (*notFoundErr)(nil) // assert implements interface
// CodedError is a convenience to attach a code to an error and already satisfy the ErrorCode interface.
// If the error is a struct, that struct will get preseneted as data to the client.
//
// To override the http code or the data representation or just for clearer documentation,
// you are encouraged to wrap CodeError with your own struct that inherits it.
// Look at the implementation of invalidInput, internalError, and notFound.
type CodedError struct {
GetCode Code
Err error
}
// NewCodedError is for constructing broad error kinds (e.g. those representing HTTP codes)
// Which could have many different underlying go errors.
// Eventually you may want to give your go errors more specific codes.
// The second argument is the broad code.
//
// If the error given is already an ErrorCode,
// that will be used as the code instead of the second argument.
func NewCodedError(err error, code Code) CodedError {
if errcode, ok := err.(ErrorCode); ok {
code = errcode.Code()
}
return CodedError{GetCode: code, Err: err}
}
var _ ErrorCode = (*CodedError)(nil) // assert implements interface
var _ HasClientData = (*CodedError)(nil) // assert implements interface
func (e CodedError) Error() string {
return e.Err.Error()
}
// Code returns the GetCode field
func (e CodedError) Code() Code {
return e.GetCode
}
// GetClientData returns the underlying Err field.
func (e CodedError) GetClientData() interface{} {
if errCode, ok := e.Err.(ErrorCode); ok {
return ClientData(errCode)
}
return e.Err
}