Skip to content

Commit

Permalink
handler: use errors.As when casting an error to handler.Error
Browse files Browse the repository at this point in the history
A datastore may wrap errors with additional information. Before the fix,
the error was converted to the InternalServerError type by the handler.
  • Loading branch information
mcdoker18 committed Oct 21, 2024
1 parent 9779a84 commit 774debc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 5 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,10 @@ 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 in order to ensure proper error handling
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 774debc

Please sign in to comment.