-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
167 lines (145 loc) · 6.66 KB
/
status.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// Copyright (C) 2018 - 2021 assay.it
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/assay-it/sdk-go
//
package http
import (
"fmt"
"net/http"
)
/*
StatusCode is a base type for typesafe HTTP status codes. The library advertises
a usage of "pattern-matching" on HTTP status handling, which helps developers to
catch mismatch of HTTP statuses along with other side-effect failures.
The final values of HTTP statuses embeds StatusCode type. It makes them to look
like a "sum-types" and be compatible with any other error (side effect failures)
within IO category. Use final type instances in the error handling routines.
Use type switch for error handling "branches"
switch e := io.Fail.(type) {
case nil:
// Nothing
case StatusCode:
switch err {
case http.StatusOK:
// HTTP 200 OK
case http.StatusNotFound:
// HTTP 404 NotFound
default:
// any other HTTP errors
}
default:
// any other errors
}
Conditional error handling on expected HTTP Status
if errors.Is(io.Fail, http.StatusNotFound) {
}
Conditional error handling on any HTTP Status
if _, ok := io.Fail.(http.StatusCode); ok {
}
*/
type StatusCode int
// NewStatusCode transforms integer codes to types
func NewStatusCode(code int, required ...StatusCode) StatusCode {
req := 0
if len(required) > 0 {
req = required[0].Value()
}
return StatusCode((req << 16) | code)
}
// Error makes StatusCode to be error
func (e StatusCode) Error() string {
status := e.Value()
if req := e.Required(); req != 0 {
return fmt.Sprintf("HTTP Status `%d %s`, required `%d %s`.",
status, http.StatusText(status), req, http.StatusText(req))
}
return fmt.Sprintf("HTTP %d %s", status, http.StatusText(status))
}
// Is compares wrapped errors
func (e StatusCode) Is(err error) bool {
if code, ok := err.(StatusCode); ok {
return e.Value() == code.Value()
}
return false
}
// Value transforms StatusCode type to integer value: StatusCode ⟼ int
func (e StatusCode) Value() int {
return int(e) & 0xffff
}
// Required returns awaited (expected, required) HTTP status
func (e StatusCode) Required() int {
return int(e) >> 16
}
//
const (
//
StatusContinue = StatusCode(http.StatusContinue)
StatusSwitchingProtocols = StatusCode(http.StatusSwitchingProtocols)
StatusProcessing = StatusCode(http.StatusProcessing)
StatusEarlyHints = StatusCode(http.StatusEarlyHints)
//
StatusOK = StatusCode(http.StatusOK)
StatusCreated = StatusCode(http.StatusCreated)
StatusAccepted = StatusCode(http.StatusAccepted)
StatusNonAuthoritativeInfo = StatusCode(http.StatusNonAuthoritativeInfo)
StatusNoContent = StatusCode(http.StatusNoContent)
StatusResetContent = StatusCode(http.StatusResetContent)
StatusPartialContent = StatusCode(http.StatusPartialContent)
StatusMultiStatus = StatusCode(http.StatusMultiStatus)
StatusAlreadyReported = StatusCode(http.StatusAlreadyReported)
StatusIMUsed = StatusCode(http.StatusIMUsed)
//
StatusMultipleChoices = StatusCode(http.StatusMultipleChoices)
StatusMovedPermanently = StatusCode(http.StatusMovedPermanently)
StatusFound = StatusCode(http.StatusFound)
StatusSeeOther = StatusCode(http.StatusSeeOther)
StatusNotModified = StatusCode(http.StatusNotModified)
StatusUseProxy = StatusCode(http.StatusUseProxy)
StatusTemporaryRedirect = StatusCode(http.StatusTemporaryRedirect)
StatusPermanentRedirect = StatusCode(http.StatusPermanentRedirect)
//
StatusBadRequest = StatusCode(http.StatusBadRequest)
StatusUnauthorized = StatusCode(http.StatusUnauthorized)
StatusPaymentRequired = StatusCode(http.StatusPaymentRequired)
StatusForbidden = StatusCode(http.StatusForbidden)
StatusNotFound = StatusCode(http.StatusNotFound)
StatusMethodNotAllowed = StatusCode(http.StatusMethodNotAllowed)
StatusNotAcceptable = StatusCode(http.StatusNotAcceptable)
StatusProxyAuthRequired = StatusCode(http.StatusProxyAuthRequired)
StatusRequestTimeout = StatusCode(http.StatusRequestTimeout)
StatusConflict = StatusCode(http.StatusConflict)
StatusGone = StatusCode(http.StatusGone)
StatusLengthRequired = StatusCode(http.StatusLengthRequired)
StatusPreconditionFailed = StatusCode(http.StatusPreconditionFailed)
StatusRequestEntityTooLarge = StatusCode(http.StatusRequestEntityTooLarge)
StatusRequestURITooLong = StatusCode(http.StatusRequestURITooLong)
StatusUnsupportedMediaType = StatusCode(http.StatusUnsupportedMediaType)
StatusRequestedRangeNotSatisfiable = StatusCode(http.StatusRequestedRangeNotSatisfiable)
StatusExpectationFailed = StatusCode(http.StatusExpectationFailed)
StatusTeapot = StatusCode(http.StatusTeapot)
StatusMisdirectedRequest = StatusCode(http.StatusMisdirectedRequest)
StatusUnprocessableEntity = StatusCode(http.StatusUnprocessableEntity)
StatusLocked = StatusCode(http.StatusLocked)
StatusFailedDependency = StatusCode(http.StatusFailedDependency)
StatusTooEarly = StatusCode(http.StatusTooEarly)
StatusUpgradeRequired = StatusCode(http.StatusUpgradeRequired)
StatusPreconditionRequired = StatusCode(http.StatusPreconditionRequired)
StatusTooManyRequests = StatusCode(http.StatusTooManyRequests)
StatusRequestHeaderFieldsTooLarge = StatusCode(http.StatusRequestHeaderFieldsTooLarge)
StatusUnavailableForLegalReasons = StatusCode(http.StatusUnavailableForLegalReasons)
//
StatusInternalServerError = StatusCode(http.StatusInternalServerError)
StatusNotImplemented = StatusCode(http.StatusNotImplemented)
StatusBadGateway = StatusCode(http.StatusBadGateway)
StatusServiceUnavailable = StatusCode(http.StatusServiceUnavailable)
StatusGatewayTimeout = StatusCode(http.StatusGatewayTimeout)
StatusHTTPVersionNotSupported = StatusCode(http.StatusHTTPVersionNotSupported)
StatusVariantAlsoNegotiates = StatusCode(http.StatusVariantAlsoNegotiates)
StatusInsufficientStorage = StatusCode(http.StatusInsufficientStorage)
StatusLoopDetected = StatusCode(http.StatusLoopDetected)
StatusNotExtended = StatusCode(http.StatusNotExtended)
StatusNetworkAuthenticationRequired = StatusCode(http.StatusNetworkAuthenticationRequired)
)