Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion internal/common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"errors"
"log"

"github.com/gofiber/fiber/v3"
)
Expand Down Expand Up @@ -34,7 +35,12 @@ func CustomErrorHandler(ctx fiber.Ctx, err error) error {
case ProblemJSONError:
problemJSON = &e
default:
problemJSON = &ProblemJSONError{Status: fiber.StatusNotFound, Title: fiber.ErrNotFound.Message, Detail: e.Error()}
log.Printf("Unhandled request error: %v", err)

problemJSON = &ProblemJSONError{
Status: fiber.StatusInternalServerError,
Title: fiber.ErrInternalServerError.Message,
}
}
}

Expand Down
35 changes: 35 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package main

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strings"
Expand Down Expand Up @@ -129,6 +132,38 @@ func TestApi(t *testing.T) {
runTestCases(t, tests)
}

func TestUnhandledErrors(t *testing.T) {
for _, panics := range []bool{false, true} {
t.Run(fmt.Sprintf("panic=%t", panics), func(t *testing.T) {
var logs bytes.Buffer
previousOutput := log.Writer()
log.SetOutput(&logs)
t.Cleanup(func() { log.SetOutput(previousOutput) })

app := Setup()
app.Get("/error", func(ctx fiber.Ctx) error {
if panics {
panic("secret internal state")
}
return errors.New("secret internal state")
})

res, err := app.Test(httptest.NewRequest(http.MethodGet, "/error", nil))
if !assert.NoError(t, err) {
return
}
defer res.Body.Close()

body, err := io.ReadAll(res.Body)
assert.NoError(t, err)
assert.Equal(t, http.StatusInternalServerError, res.StatusCode)
assert.Equal(t, "application/problem+json", res.Header.Get("Content-Type"))
assert.JSONEq(t, `{"title":"Internal Server Error","status":500}`, string(body))
assert.Contains(t, logs.String(), "secret internal state")
})
}
}

func TestValidateEndpoint(t *testing.T) {
validYml := loadTestdata(t, "valid.publiccode.yml")
invalidYml := loadTestdata(t, "invalid.publiccode.yml")
Expand Down
20 changes: 20 additions & 0 deletions publiccode-validator-api.oas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ paths:
v: "1.0.0"
commit: "abc1234"

"500":
description: Unexpected server error
content:
application/problem+json:
schema:
$ref: "#/components/schemas/ProblemJSON"
example:
title: Internal Server Error
status: 500

/validate:
post:
operationId: validatePubliccode
Expand Down Expand Up @@ -137,6 +147,16 @@ paths:
title: Not Found
status: 404

"500":
description: Unexpected server error
content:
application/problem+json:
schema:
$ref: "#/components/schemas/ProblemJSON"
example:
title: Internal Server Error
status: 500

components:
schemas:
Status:
Expand Down
Loading