Skip to content

Commit b5b67ae

Browse files
committed
Adds tests for global and local error responses
1 parent 514a97f commit b5b67ae

File tree

6 files changed

+37
-5
lines changed

6 files changed

+37
-5
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ linters:
1515
- ineffassign
1616
- staticcheck
1717
- unused
18+
- usestdlibvars
1819

1920
linters-settings:
2021
gofumpt:

examples/full-app-gourmet/server/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (rs Ressources) Setup(
2323
fuego.WithAutoAuth(controller.LoginFunc),
2424
fuego.WithTemplateFS(templates.FS),
2525
fuego.WithTemplateGlobs("**/*.html", "**/**/*.html"),
26-
fuego.WithGlobalResponseTypes(403, "Forbidden"),
26+
fuego.WithGlobalResponseTypes(http.StatusForbidden, "Forbidden"),
2727
}
2828

2929
options = append(serverOptions, options...)

examples/full-app-gourmet/views/views.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package views
22

33
import (
4+
"net/http"
45
"os"
56

67
"github.com/go-fuego/fuego"
@@ -24,7 +25,7 @@ func (rs Ressource) Routes(s *fuego.Server) {
2425

2526
// Public Chunks
2627
fuego.Get(s, "/recipes-list", rs.showRecipesList)
27-
fuego.Get(s, "/search", rs.searchRecipes).AddError(401, "Authorization Error").AddError(500, "My Server Error")
28+
fuego.Get(s, "/search", rs.searchRecipes).AddError(http.StatusUnauthorized, "Authorization Error").AddError(500, "My Server Error")
2829
fuego.Get(s, "/ingredients/preselect-unit", rs.unitPreselected).QueryParam("id", "")
2930

3031
// Admin Pages

openapi_operations.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,12 @@ func (r Route[ResponseBody, RequestBody]) AddError(code int, description string,
8989
}
9090

9191
func addResponse(s *Server, operation *openapi3.Operation, code int, description string, errorType ...any) {
92-
responseSchema := schemaTagFromType(s, HTTPError{})
92+
var responseSchema schemaTag
93+
9394
if len(errorType) > 0 {
9495
responseSchema = schemaTagFromType(s, errorType[0])
96+
} else {
97+
responseSchema = schemaTagFromType(s, HTTPError{})
9598
}
9699
content := openapi3.NewContentWithSchemaRef(&responseSchema.SchemaRef, []string{"application/json"})
97100

openapi_operations_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,33 @@ func TestCustomError(t *testing.T) {
7272
require.Equal(t, "My Validation Error", *route.Operation.Responses.Map()["400"].Value.Description)
7373
}
7474

75+
func TestCustomErrorGlobalAndOnRoute(t *testing.T) {
76+
type MyGlobalError struct {
77+
Message string
78+
}
79+
s := NewServer(
80+
WithGlobalResponseTypes(400, "My Global Error", MyGlobalError{}),
81+
WithGlobalResponseTypes(501, "Another Global Error", MyGlobalError{}),
82+
)
83+
84+
type MyLocalError struct {
85+
Message string
86+
}
87+
88+
routeGlobal := Get(s, "/test-global", testController)
89+
routeCustom := Get(s, "/test-custom", testController).
90+
AddError(400, "My Local Error", MyLocalError{}).
91+
AddError(419, "My Local Teapot")
92+
93+
require.Equal(t, "My Global Error", *routeGlobal.Operation.Responses.Map()["400"].Value.Description, "Overrides Fuego's default 400 error")
94+
require.Equal(t, "Another Global Error", *routeGlobal.Operation.Responses.Map()["501"].Value.Description)
95+
96+
require.Equal(t, "My Local Error", *routeCustom.Operation.Responses.Map()["400"].Value.Description, "Local error overrides global error")
97+
require.Equal(t, "My Local Teapot", *routeCustom.Operation.Responses.Map()["419"].Value.Description)
98+
require.Equal(t, "Internal Server Error _(panics)_", *routeCustom.Operation.Responses.Map()["500"].Value.Description, "Global error set by default by Fuego")
99+
require.Equal(t, "Another Global Error", *routeCustom.Operation.Responses.Map()["501"].Value.Description, "Global error is still available")
100+
}
101+
75102
func TestCookieParams(t *testing.T) {
76103
t.Run("basic cookie", func(t *testing.T) {
77104
s := NewServer()

options.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ func NewServer(options ...func(*Server)) *Server {
115115
WithSerializer(Send),
116116
WithErrorSerializer(SendError),
117117
WithErrorHandler(ErrorHandler),
118-
WithGlobalResponseTypes(400, "Bad Request _(validation or deserialization error)_", HTTPError{}),
119-
WithGlobalResponseTypes(500, "Internal Server Error _(panics)_", HTTPError{}),
118+
WithGlobalResponseTypes(http.StatusBadRequest, "Bad Request _(validation or deserialization error)_", HTTPError{}),
119+
WithGlobalResponseTypes(http.StatusInternalServerError, "Internal Server Error _(panics)_", HTTPError{}),
120120
}
121121

122122
for _, option := range append(defaultOptions[:], options...) {

0 commit comments

Comments
 (0)