Skip to content

Commit 3f26d94

Browse files
authored
go-kit compatibility for HTTPStatusCode (#6)
1 parent fd599fc commit 3f26d94

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

http.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ type HTTPStatusCodeEr interface {
77
HTTPStatusCode() int
88
}
99

10+
// StatusCodeEr defines errors that should return a specific HTTP status code
11+
type StatusCodeEr interface {
12+
StatusCode() int
13+
}
14+
1015
// HTTPStatusCode returns the status code that a HTTP handler should return.
1116
//
1217
// If the error is nil, StatusOK is returned.
1318
//
14-
// If the error implements HTTPStatusCodeEr, it returns the corresponding status code.
19+
// If the error implements HTTPStatusCodeEr or StatusCodeEr, it returns the corresponding status code.
1520
//
1621
// It tries to check some stdlib errors (testing the error string, to avoid importing unwanted packages),
1722
// and returns appropriate status codes.
@@ -29,6 +34,9 @@ func HTTPStatusCode(err error) int {
2934
if status, ok := err.(HTTPStatusCodeEr); ok {
3035
return status.HTTPStatusCode()
3136
}
37+
if status, ok := err.(StatusCodeEr); ok {
38+
return status.StatusCode()
39+
}
3240
// Check errors from stdlib. Test string to avoid importing packages
3341
switch err.Error() {
3442
// package os

0 commit comments

Comments
 (0)