Skip to content

Commit ac4a6ce

Browse files
committed
Use constants from net/http pkg instead of self const and raw numbers
Signed-off-by: Oleksandr Redko <[email protected]>
1 parent 7a7ff1b commit ac4a6ce

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

.golangci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ linters:
1717
disable:
1818
- maligned
1919
- lll
20+
- gochecknoglobals

api.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
)
2424

2525
// DefaultHTTPCode is used when the error Code cannot be used as an HTTP code.
26-
var DefaultHTTPCode = 422
26+
var DefaultHTTPCode = http.StatusUnprocessableEntity
2727

2828
// Error represents a error interface all swagger framework errors implement
2929
type Error interface {
@@ -115,8 +115,6 @@ func MethodNotAllowed(requested string, allow []string) Error {
115115
return &MethodNotAllowedError{code: http.StatusMethodNotAllowed, Allowed: allow, message: msg}
116116
}
117117

118-
const head = "HEAD"
119-
120118
// ServeError the error handler interface implementation
121119
func ServeError(rw http.ResponseWriter, r *http.Request, err error) {
122120
rw.Header().Set("Content-Type", "application/json")
@@ -133,7 +131,7 @@ func ServeError(rw http.ResponseWriter, r *http.Request, err error) {
133131
case *MethodNotAllowedError:
134132
rw.Header().Add("Allow", strings.Join(err.(*MethodNotAllowedError).Allowed, ","))
135133
rw.WriteHeader(asHTTPCode(int(e.Code())))
136-
if r == nil || r.Method != head {
134+
if r == nil || r.Method != http.MethodHead {
137135
_, _ = rw.Write(errorAsJSON(e))
138136
}
139137
case Error:
@@ -144,15 +142,15 @@ func ServeError(rw http.ResponseWriter, r *http.Request, err error) {
144142
return
145143
}
146144
rw.WriteHeader(asHTTPCode(int(e.Code())))
147-
if r == nil || r.Method != head {
145+
if r == nil || r.Method != http.MethodHead {
148146
_, _ = rw.Write(errorAsJSON(e))
149147
}
150148
case nil:
151149
rw.WriteHeader(http.StatusInternalServerError)
152150
_, _ = rw.Write(errorAsJSON(New(http.StatusInternalServerError, "Unknown error")))
153151
default:
154152
rw.WriteHeader(http.StatusInternalServerError)
155-
if r == nil || r.Method != head {
153+
if r == nil || r.Method != http.MethodHead {
156154
_, _ = rw.Write(errorAsJSON(New(http.StatusInternalServerError, err.Error())))
157155
}
158156
}

auth.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
package errors
1616

17+
import "net/http"
18+
1719
// Unauthenticated returns an unauthenticated error
1820
func Unauthenticated(scheme string) Error {
19-
return New(401, "unauthenticated for %s", scheme)
21+
return New(http.StatusUnauthorized, "unauthenticated for %s", scheme)
2022
}

0 commit comments

Comments
 (0)