Skip to content

Commit

Permalink
Fix: Correctly handle ErrInvalidSignature comparison (#429)
Browse files Browse the repository at this point in the history
* Fix: Correctly handle ErrInvalidSignature comparison

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

* Refactor: Use errors.Is for ErrInvalidSignature checks

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.

* Add: missing echo_bot/server.go file to previous commit
  • Loading branch information
easonchill committed Mar 1, 2024
1 parent acce12d commit 0efcdb8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions examples/echo_bot/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
package main

import (
"errors"
"fmt"
"log"
"net/http"
"os"

"github.com/line/line-bot-sdk-go/v8/linebot"
"github.com/line/line-bot-sdk-go/v8/linebot/messaging_api"
"github.com/line/line-bot-sdk-go/v8/linebot/webhook"
)
Expand All @@ -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 errors.Is(err, webhook.ErrInvalidSignature) {
w.WriteHeader(400)
} else {
w.WriteHeader(500)
Expand Down
4 changes: 2 additions & 2 deletions examples/kitchensink/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package main

import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
Expand All @@ -25,7 +26,6 @@ import (
"path/filepath"
"runtime"

"github.com/line/line-bot-sdk-go/v8/linebot"
"github.com/line/line-bot-sdk-go/v8/linebot/messaging_api"
"github.com/line/line-bot-sdk-go/v8/linebot/webhook"
)
Expand Down Expand Up @@ -106,7 +106,7 @@ func NewKitchenSink(channelSecret, channelToken, appBaseURL string) (*KitchenSin
func (app *KitchenSink) Callback(w http.ResponseWriter, r *http.Request) {
cb, err := webhook.ParseRequest(app.channelSecret, r)
if err != nil {
if err == linebot.ErrInvalidSignature {
if errors.Is(err, webhook.ErrInvalidSignature) {
w.WriteHeader(400)
} else {
w.WriteHeader(500)
Expand Down

0 comments on commit 0efcdb8

Please sign in to comment.