-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a feature to control the status code
- Loading branch information
Showing
4 changed files
with
116 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package lieut | ||
|
||
// StatusCodeError represents an error that reports an associated status code. | ||
type StatusCodeError interface { | ||
error | ||
|
||
// StatusCode returns the status code of the error, which can be used by an | ||
// app's execution error to know which status code to return. | ||
StatusCode() int | ||
} | ||
|
||
type statusCodeError struct { | ||
error | ||
|
||
statusCode int | ||
} | ||
|
||
// ErrWithStatusCode takes an error and a status code and returns a type that | ||
// satisfies StatusCodeError. | ||
func ErrWithStatusCode(err error, statusCode int) StatusCodeError { | ||
return &statusCodeError{error: err, statusCode: statusCode} | ||
} | ||
|
||
// StatusCode returns the status code of the error. | ||
func (e *statusCodeError) StatusCode() int { | ||
return e.statusCode | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package lieut | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
) | ||
|
||
func TestErrWithStatusCode(t *testing.T) { | ||
errMsg := "test error" | ||
errCode := 107 | ||
|
||
err := errors.New(errMsg) | ||
|
||
statusCodeErr := ErrWithStatusCode(err, errCode) | ||
if statusCodeErr == nil { | ||
t.Fatal("ErrWithStatusCode returned nil") | ||
} | ||
|
||
if gotMsg := statusCodeErr.Error(); gotMsg != errMsg { | ||
t.Errorf("err.Error() returned %q, wanted %q", gotMsg, errMsg) | ||
} | ||
|
||
if gotCode := statusCodeErr.StatusCode(); gotCode != errCode { | ||
t.Errorf("err.Error() returned %v, wanted %v", gotCode, errCode) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters