-
Notifications
You must be signed in to change notification settings - Fork 0
Fix serialization errors in custom error handling #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0c083bb
Fix error serialization in custom error handler
dcbd487
Fix response serialization error handling in Method function
3c14a4c
Add tests for unserializable output and error handling in serialization
aefbb9e
Fix fallback error handling in custom error serialization
e26d640
Fix error handling for response serialization in Method function
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package fiberoapi | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "io" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/gofiber/fiber/v2" | ||
| ) | ||
|
|
||
| // UnserializableOutput contains a channel which cannot be serialized to JSON | ||
| type UnserializableOutput struct { | ||
| Name string `json:"name"` | ||
| Channel chan string `json:"channel"` // Channels cannot be JSON serialized | ||
| } | ||
|
|
||
| type SerializationTestInput struct{} | ||
|
|
||
| type SerializationTestError struct { | ||
| StatusCode int `json:"statusCode"` | ||
| Message string `json:"message"` | ||
| } | ||
|
|
||
| func TestResponseSerializationError(t *testing.T) { | ||
| app := fiber.New() | ||
| oapi := New(app) | ||
|
|
||
| // Handler that returns an unserializable output (contains a channel) | ||
| Get(oapi, "/unserializable", func(c *fiber.Ctx, input SerializationTestInput) (UnserializableOutput, *SerializationTestError) { | ||
| return UnserializableOutput{ | ||
| Name: "test", | ||
| Channel: make(chan string), // This will fail JSON marshaling | ||
| }, nil | ||
| }, OpenAPIOptions{ | ||
| Summary: "Test endpoint with unserializable response", | ||
| }) | ||
|
|
||
| req := httptest.NewRequest("GET", "/unserializable", nil) | ||
| resp, err := app.Test(req) | ||
| if err != nil { | ||
| t.Fatalf("Failed to make request: %v", err) | ||
| } | ||
|
|
||
| // Should return 500 status code | ||
| if resp.StatusCode != 500 { | ||
| t.Errorf("Expected status 500, got %d", resp.StatusCode) | ||
| } | ||
|
|
||
| // Should return a proper error response | ||
| body, _ := io.ReadAll(resp.Body) | ||
| var errorResp ErrorResponse | ||
| if err := json.Unmarshal(body, &errorResp); err != nil { | ||
| t.Fatalf("Failed to unmarshal error response: %v. Body: %s", err, string(body)) | ||
| } | ||
|
|
||
| if errorResp.Code != 500 { | ||
| t.Errorf("Expected error code 500, got %d", errorResp.Code) | ||
| } | ||
|
|
||
| if errorResp.Type != "serialization_error" { | ||
| t.Errorf("Expected error type 'serialization_error', got '%s'", errorResp.Type) | ||
| } | ||
| } | ||
|
|
||
| // UnserializableError contains a channel which cannot be serialized | ||
| type UnserializableError struct { | ||
| StatusCode int `json:"statusCode"` | ||
| Channel chan string `json:"channel"` | ||
| } | ||
|
|
||
| func TestErrorSerializationError(t *testing.T) { | ||
| app := fiber.New() | ||
| oapi := New(app) | ||
|
|
||
| // Handler that returns an unserializable error | ||
| Get(oapi, "/unserializable-error", func(c *fiber.Ctx, input SerializationTestInput) (map[string]string, *UnserializableError) { | ||
| return nil, &UnserializableError{ | ||
| StatusCode: 400, | ||
| Channel: make(chan string), // This will fail JSON marshaling | ||
| } | ||
| }, OpenAPIOptions{ | ||
| Summary: "Test endpoint with unserializable error", | ||
| }) | ||
|
|
||
| req := httptest.NewRequest("GET", "/unserializable-error", nil) | ||
| resp, err := app.Test(req) | ||
| if err != nil { | ||
| t.Fatalf("Failed to make request: %v", err) | ||
| } | ||
|
|
||
| // Should return 500 status code (fallback error) | ||
| if resp.StatusCode != 500 { | ||
| t.Errorf("Expected status 500, got %d", resp.StatusCode) | ||
| } | ||
|
|
||
| // Should return a proper error response | ||
| body, _ := io.ReadAll(resp.Body) | ||
| var errorResp map[string]string | ||
| if err := json.Unmarshal(body, &errorResp); err != nil { | ||
| t.Fatalf("Failed to unmarshal error response: %v. Body: %s", err, string(body)) | ||
| } | ||
|
|
||
| if errorResp["error"] != "Failed to serialize error response" { | ||
| t.Errorf("Expected error message 'Failed to serialize error response', got '%s'", errorResp["error"]) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.