-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose response HTTP status code in errors #52
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,52 @@ | ||
package fargo | ||
|
||
// MIT Licensed (see README.md) - Copyright (c) 2013 Hudl <@Hudl> | ||
|
||
import ( | ||
"errors" | ||
"strconv" | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestHTTPResponseStatusCode(t *testing.T) { | ||
Convey("An nil error should have no HTTP status code", t, func() { | ||
_, present := HTTPResponseStatusCode(nil) | ||
So(present, ShouldBeFalse) | ||
}) | ||
Convey("A foreign error should have no detectable HTTP status code", t, func() { | ||
_, present := HTTPResponseStatusCode(errors.New("other")) | ||
So(present, ShouldBeFalse) | ||
}) | ||
Convey("A fargo error generated from a response from Eureka", t, func() { | ||
verify := func(err *unsuccessfulHTTPResponse) { | ||
Convey("should have the given HTTP status code", func() { | ||
code, present := HTTPResponseStatusCode(err) | ||
So(present, ShouldBeTrue) | ||
So(code, ShouldEqual, err.statusCode) | ||
Convey("should produce a message", func() { | ||
msg := err.Error() | ||
if len(err.messagePrefix) == 0 { | ||
Convey("that lacks a prefx", func() { | ||
So(msg, ShouldNotStartWith, ",") | ||
}) | ||
} else { | ||
Convey("that starts with the given prefix", func() { | ||
So(msg, ShouldStartWith, err.messagePrefix) | ||
}) | ||
} | ||
Convey("that contains the status code in decimal notation", func() { | ||
So(msg, ShouldContainSubstring, strconv.Itoa(err.statusCode)) | ||
}) | ||
}) | ||
}) | ||
} | ||
Convey("with a message prefix", func() { | ||
verify(&unsuccessfulHTTPResponse{500, "operation failed"}) | ||
}) | ||
Convey("without a message prefix", func() { | ||
verify(&unsuccessfulHTTPResponse{statusCode: 500}) | ||
}) | ||
}) | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would unify order with string output. Maybe message prefix first and rcode second?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wrote the struct in order from most important to least important, from required to optional, thinking that the response code is the type's _raison d'être _. I tried to keep the string format consistent with the rest of fargo's error messages. I don't think you're advocating changing the string format.
I could swap the field order. I just wanted you to know that its current arrangement was a deliberate choice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please take a look at commit 4f48c58, in which I make sure to treat the message prefix as optional. Given that, let me know if you'd still prefer the field order to be swapped.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. If you feel it's more appropriate leave it. I was just feeling that's it might look better when creating new error to have message first, so it would be similar to
printf
function. I'll leave decision to you. Thanks for clarifying.