Skip to content
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

Fix: Correctly handle ErrInvalidSignature comparison #429

Merged
merged 3 commits into from
Mar 1, 2024

Conversation

easonchill
Copy link
Contributor

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

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
@CLAassistant
Copy link

CLAassistant commented Feb 28, 2024

CLA assistant check
All committers have signed the CLA.

@codecov-commenter
Copy link

codecov-commenter commented Feb 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 55.15%. Comparing base (acce12d) to head (ba15107).

❗ Current head ba15107 differs from pull request most recent head c03d165. Consider uploading reports for the commit c03d165 to get more accurate results

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.
📢 Have feedback on the report? Share it here.

@kkdai kkdai self-requested a review February 29, 2024 12:29
@@ -41,7 +41,7 @@ func main() {
cb, err := webhook.ParseRequest(channelSecret, req)
if err != nil {
log.Printf("Cannot parse request: %+v\n", err)
if err == linebot.ErrInvalidSignature {
if err.Error() == linebot.ErrInvalidSignature.Error() {
Copy link
Member

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
}

Copy link
Member

@kkdai kkdai left a 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.
Copy link
Member

@kkdai kkdai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kkdai kkdai merged commit 0efcdb8 into line:master Mar 1, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants