Skip to content

Commit

Permalink
handler: unwrap errors using errors.As (#1209)
Browse files Browse the repository at this point in the history
* handler: use errors.As when casting an error to handler.Error

A datastore may wrap errors with additional information. Before the fix,
the error was converted to the InternalServerError type by the handler.

* Update pkg/handler/patch_test.go

---------

Co-authored-by: Marius <[email protected]>
  • Loading branch information
mcdoker18 and Acconut authored Oct 22, 2024
1 parent 9779a84 commit 47b53ab
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
7 changes: 6 additions & 1 deletion pkg/handler/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handler_test
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"strings"
Expand Down Expand Up @@ -140,7 +141,11 @@ func TestPatch(t *testing.T) {
})

SubTest(t, "UploadNotFoundFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
store.EXPECT().GetUpload(gomock.Any(), "no").Return(nil, ErrNotFound)
// We wrap the error test whether the handler correctly unwraps it again
// to get an handler.Error.
err := fmt.Errorf("extra info: %w", ErrNotFound)

store.EXPECT().GetUpload(gomock.Any(), "no").Return(nil, err)

handler, _ := NewHandler(Config{
StoreComposer: composer,
Expand Down
5 changes: 3 additions & 2 deletions pkg/handler/unrouted_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1185,8 +1185,9 @@ func (handler *UnroutedHandler) terminateUpload(c *httpContext, upload Upload, i
func (handler *UnroutedHandler) sendError(c *httpContext, err error) {
r := c.req

detailedErr, ok := err.(Error)
if !ok {
var detailedErr Error

if !errors.As(err, &detailedErr) {
c.log.Error("InternalServerError", "message", err.Error())
detailedErr = NewError("ERR_INTERNAL_SERVER_ERROR", err.Error(), http.StatusInternalServerError)
}
Expand Down

0 comments on commit 47b53ab

Please sign in to comment.