diff --git a/pkg/modules/webhook/client.go b/pkg/modules/webhook/client.go index 6815fdd6..7ad40adf 100644 --- a/pkg/modules/webhook/client.go +++ b/pkg/modules/webhook/client.go @@ -3,6 +3,7 @@ package webhook import ( "fmt" "io" + "net/http" "strconv" "time" @@ -78,6 +79,10 @@ func (c client) send(body io.Reader, headers map[string]string, erroed bool) err return fmt.Errorf("send '%s' request to '%s': %w", method, URL, err) } + if resp.StatusCode >= http.StatusBadRequest { + return fmt.Errorf("send '%s' request to '%s': got status: '%s'", method, URL, resp.Status) + } + defer func() { err := resp.Body.Close() if err != nil { diff --git a/pkg/modules/webhook/middleware_test.go b/pkg/modules/webhook/middleware_test.go index a88c5ba5..60459182 100644 --- a/pkg/modules/webhook/middleware_test.go +++ b/pkg/modules/webhook/middleware_test.go @@ -339,6 +339,7 @@ func TestWebhookMiddlewareAsynchronousProcess(t *testing.T) { expectWebhookFilename string expectWebhookErrorStatus int expectWebhookErrorMessage string + returnedError *echo.HTTPError }{ { request: buildMultipartFormDataRequest(), @@ -363,8 +364,8 @@ func TestWebhookMiddlewareAsynchronousProcess(t *testing.T) { }(), expectWebhookContentType: echo.MIMEApplicationJSONCharsetUTF8, expectWebhookMethod: http.MethodPost, - expectWebhookErrorStatus: http.StatusInternalServerError, - expectWebhookErrorMessage: http.StatusText(http.StatusInternalServerError), + expectWebhookErrorStatus: http.StatusBadRequest, + expectWebhookErrorMessage: http.StatusText(http.StatusBadRequest), }, { request: func() *http.Request { @@ -387,6 +388,27 @@ func TestWebhookMiddlewareAsynchronousProcess(t *testing.T) { expectWebhookFilename: "foo", expectWebhookExtraHTTPHeaders: map[string]string{"foo": "bar"}, }, + { + request: func() *http.Request { + req := buildMultipartFormDataRequest() + req.Header.Set("Gotenberg-Output-Filename", "foo") + + return req + }(), + mod: buildWebhookModule(), + next: func() echo.HandlerFunc { + return func(c echo.Context) error { + ctx := c.Get("context").(*api.Context) + + return ctx.AddOutputPaths("/tests/test/testdata/api/sample3.pdf") + } + }(), + returnedError: echo.ErrInternalServerError, + // Even though an error is returned the expected response is still a pdf, since the webhook error is only logged + expectWebhookContentType: "application/pdf", + expectWebhookMethod: http.MethodPost, + expectWebhookFilename: "foo", + }, } { func() { srv := echo.New() @@ -500,6 +522,11 @@ func TestWebhookMiddlewareAsynchronousProcess(t *testing.T) { } errChan <- nil + + if tc.returnedError != nil { + return tc.returnedError + } + return nil } }(),