-
Notifications
You must be signed in to change notification settings - Fork 244
Fix: Correctly handle ErrInvalidSignature comparison #429
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
Conversation
This commit addresses an issue in the error handling logic within the echo_bot and kitchensink examples, where the ErrInvalidSignature error from the linebot package was not being correctly identified due to a direct comparison with the error interface. By updating the comparison to use err.Error() == linebot.ErrInvalidSignature.Error(), we ensure that the error messages are compared as strings, which is a more reliable method for identifying this specific error. This change results in the appropriate HTTP status codes being returned (400 for invalid signatures, 500 for other errors), improving the error feedback for API consumers. Affected files: - examples/echo_bot/server.go - examples/kitchensink/server.go
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #429 +/- ##
=======================================
Coverage 55.15% 55.15%
=======================================
Files 87 87
Lines 5483 5483
=======================================
Hits 3024 3024
Misses 2237 2237
Partials 222 222 ☔ View full report in Codecov by Sentry. |
examples/echo_bot/server.go
Outdated
| if err != nil { | ||
| log.Printf("Cannot parse request: %+v\n", err) | ||
| if err == linebot.ErrInvalidSignature { | ||
| if err.Error() == linebot.ErrInvalidSignature.Error() { |
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.
Could you try
// if err == ErrNotFound { … }
if errors.Is(err, linebot.ErrInvalidSignature) {
// something wasn't found
}
kkdai
left a comment
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.
Hi @easonchill
Thank you for your contribution, have you ever tried errors.Is() ?
Refer https://go.dev/blog/go1.13-errors
This commit updates the error checking logic in both the echo_bot and kitchensink examples to use the standard errors.Is function for identifying ErrInvalidSignature errors. This change enhances the readability and maintainability of the code by utilizing the idiomatic approach for error comparison in Go. It also ensures more reliable error handling by correctly identifying wrapped errors. Changes include: - Importing the "errors" package in both server.go files. - Replacing direct error message comparisons with errors.Is for ErrInvalidSignature checks. This update follows best practices for error handling in Go and aligns with the Go community's recommendations.
kkdai
left a comment
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.
LGTM
Background
While integrating LINE webhook events, I noticed an inconsistency in how errors, specifically ErrInvalidSignature, were being handled. The direct comparison of errors using == does not effectively catch ErrInvalidSignature due to the way Go's error interface works. This led to improper HTTP status codes being returned, affecting the error feedback mechanism.
Changes Made
This PR addresses the issue by updating the error comparison logic in both echo_bot and kitchensink examples. Instead of directly comparing error interfaces, the changes involve comparing the error messages:
In echo_bot/server.go and kitchensink/server.go, modified the conditional checks from if err == linebot.ErrInvalidSignature to if err.Error() == linebot.ErrInvalidSignature.Error(). This ensures that the error comparison is based on the error message string, which is a more reliable method for this scenario.
By the way,I have tried using errors.Is(err, linebot.ErrInvalidSignature)
But still can't work
Version
go version go1.21.3 darwin/arm64