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
13 changes: 9 additions & 4 deletions internal/common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ func Error(status int, title string, detail string) ProblemJSONError {
func CustomErrorHandler(ctx fiber.Ctx, err error) error {
var problemJSON *ProblemJSONError

// Retrieve the custom status code if it's a fiber.*Error
var e *fiber.Error
if errors.Is(err, e) {
problemJSON = &ProblemJSONError{Status: e.Code, Title: e.Message}
// Retrieve the custom status code if it's a fiber.Error.
fiberError, ok := errors.AsType[*fiber.Error](err)
if ok {
problemJSON = &ProblemJSONError{
Code: "",
Title: fiberError.Message,
Detail: "",
Status: fiberError.Code,
}
}

if problemJSON == nil {
Expand Down
16 changes: 15 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type TestCase struct {
expectedCode int
expectedBody string
expectedContentType string
expectedHeaders map[string]string
validateFunc func(t *testing.T, response map[string]any)
}

Expand Down Expand Up @@ -107,6 +108,9 @@ func runTestCases(t *testing.T, tests []TestCase) {
}

assert.Equal(t, test.expectedContentType, res.Header.Get("Content-Type"))
for name, expectedValue := range test.expectedHeaders {
assert.Equal(t, expectedValue, res.Header.Get(name))
}
})
}
}
Expand All @@ -118,7 +122,7 @@ func TestApi(t *testing.T) {
query: "GET /v1/i-dont-exist",

expectedCode: 404,
expectedBody: `{"title":"Not Found","detail":"Not Found","status":404}`,
expectedBody: `{"title":"Not Found","status":404}`,
expectedContentType: "application/problem+json",
},
}
Expand All @@ -131,6 +135,16 @@ func TestValidateEndpoint(t *testing.T) {
invalidYml := loadTestdata(t, "invalid.publiccode.yml")

tests := []TestCase{
{
description: "validate: unsupported method",
query: "GET /v1/validate",
expectedCode: 405,
expectedBody: `{"title":"Method Not Allowed","status":405}`,
expectedContentType: "application/problem+json",
expectedHeaders: map[string]string{
"Allow": "POST, QUERY",
},
},
{
description: "validate: empty body",
query: "QUERY /v1/validate",
Expand Down
16 changes: 15 additions & 1 deletion publiccode-validator-api.oas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,21 @@ paths:
title: empty body
detail: need a body to validate
status: 400
"405":
description: Method not allowed
headers:
Allow:
description: Supported HTTP methods for this endpoint
schema:
type: string
example: POST, QUERY
content:
application/problem+json:
schema:
$ref: "#/components/schemas/ProblemJSON"
example:
title: Method Not Allowed
status: 405
"404":
description: Not found
content:
Expand All @@ -113,7 +128,6 @@ paths:
$ref: "#/components/schemas/ProblemJSON"
example:
title: Not Found
detail: Not Found
status: 404

components:
Expand Down
Loading