-
-
Notifications
You must be signed in to change notification settings - Fork 579
HTTPError Error() avoid nil pointer dereference on nil Cause #2238
Conversation
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.
Indeed! Thanks for the fix!
I'm not a fan of this change. The point of the
|
Alternatively if err == nil {
err = errors.New("unknown cause")
}
return HTTPError{Status: status, Cause: err} The reasoning is that there might be code out there that relies on |
If the purpose of the exported function // HTTPError a typed error returned by http Handlers and used for choosing error handlers
type HTTPError struct {
Status int `json:"status"`
Cause error `json:"error"`
}
// Error returns the cause of the error as string.
func (h HTTPError) Error() string {
return h.Cause.Error()
} |
If there's no cause, then I would expect the {
"code": 500,
"error": ""
} While I think something like this {
"code": 500
} or this {
"code": 500,
"error": "unknown"
} would be more appropriate for a |
@saurori, I think adding something similar to @famat's commented code could be better in the perspective of usage.
|
The point of this PR was to avoid a runtime panic if one passes If a runtime panic is not desirable, then what actually is returned if |
@sio4, @saurori: The more I think about HTTPError and how it wraps an existing error to add the additional context of which status code should be returned by the API the more I think the change should just be: // Error returns the cause of the error as string.
func (h HTTPError) Error() string {
if h.Cause != nil {
return h.Cause.Error()
}
return "unknown cause"
} Since the incorrect behavior is clearly this method and not how |
Do you mean
By the way, I think @saurori 's approach is not mainly about the usage but a possible runtime error. Also, I agree to return a string "<nil>" is also not a good thing (the result of this patch). So I think the combination of @saurori 's patch and your quick explanation of adding |
Yes you are right, I forgot for a moment there that |
Ah, already some more comments in realtime :-) Actually, my typing speed is not good enough, especially for English. I think we made some consensus now. :-) |
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.
Thanks for the quick update!
The default renderer
Error()
method does not check for anil
error
on the underlyingHTTPError
type. Anil pointer dereference
can be triggered by calling for example on the default renderer:c.Render(500, nil)
.This PR changes the
HTTPError
Error()
method to usefmt.Sprint()
instead of callingError()
on the underlyingerror
inCause
.