-
Notifications
You must be signed in to change notification settings - Fork 0
Adds Error envelope #62
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 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a6f9455
Add `ErrorEnvelope` struct and `HandleHttpError` utility with tests
jlpdeveloper 66bf415
Refactor error handling across product and platform handlers using `H…
jlpdeveloper 3c2715c
Improve `HandleHttpError` to default invalid status codes to 500 and …
jlpdeveloper 1c435d0
chore: adds junie folder to gitignore
jlpdeveloper 2a9f77e
Refactor response encoding in `product` and `platform` handlers to us…
jlpdeveloper 39a0fec
Improve error handling when writing JSON responses in `product` handl…
jlpdeveloper 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,4 +38,6 @@ coverage/ | |
|
|
||
| # Liquibase Specific files/folders | ||
| liquibase.properties | ||
| liquibase_libs/ | ||
| liquibase_libs/ | ||
|
|
||
| .junie | ||
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,39 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "log/slog" | ||
| "net/http" | ||
| ) | ||
|
|
||
| type ErrorEnvelope struct { | ||
| Type string `json:"type"` | ||
| Title string `json:"title"` | ||
| Status int `json:"status"` | ||
| Detail string `json:"detail,omitempty"` | ||
| Instance string `json:"instance,omitempty"` | ||
| } | ||
|
|
||
| // HandleHttpError handles an error and writes it to the response writer | ||
| // in the format specified by RFC 7807. If an invalid status code is provided, | ||
| // it will default to 500 Internal Server Error. | ||
| func HandleHttpError(w http.ResponseWriter, err ErrorEnvelope, statusCode int) { | ||
| if err.Type == "" { | ||
| err.Type = "about:blank" | ||
| } | ||
| if http.StatusText(statusCode) == "" { | ||
| slog.Error("Unknown HTTP status code", "status_code", statusCode) | ||
| statusCode = http.StatusInternalServerError | ||
| } | ||
| if err.Title == "" { | ||
| err.Title = http.StatusText(statusCode) | ||
| } | ||
| err.Status = statusCode | ||
|
|
||
| w.Header().Set("Content-Type", "application/problem+json") | ||
| w.WriteHeader(statusCode) | ||
|
|
||
| if encodeErr := json.NewEncoder(w).Encode(err); encodeErr != nil { | ||
| slog.Error("Failed to encode error response", "error", encodeErr) | ||
| } | ||
| } |
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,99 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestHandleHttpError(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| err ErrorEnvelope | ||
| statusCode int | ||
| expectedStatus int | ||
| expectedBody string | ||
| expectedTitle string | ||
| expectedType string | ||
| }{ | ||
| { | ||
| name: "Empty Envelope", | ||
| err: ErrorEnvelope{}, | ||
| statusCode: http.StatusBadRequest, | ||
| expectedStatus: http.StatusBadRequest, | ||
| expectedTitle: "Bad Request", | ||
| expectedType: "about:blank", | ||
| }, | ||
| { | ||
| name: "With Detail", | ||
| err: ErrorEnvelope{ | ||
| Detail: "Validation failed", | ||
| }, | ||
| statusCode: http.StatusUnprocessableEntity, | ||
| expectedStatus: http.StatusUnprocessableEntity, | ||
| expectedTitle: "Unprocessable Entity", | ||
| expectedType: "about:blank", | ||
| }, | ||
| { | ||
| name: "Custom Type and Title", | ||
| err: ErrorEnvelope{ | ||
| Type: "https://example.com/probs/out-of-credit", | ||
| Title: "You do not have enough credit.", | ||
| }, | ||
| statusCode: http.StatusForbidden, | ||
| expectedStatus: http.StatusForbidden, | ||
| expectedTitle: "You do not have enough credit.", | ||
| expectedType: "https://example.com/probs/out-of-credit", | ||
| }, | ||
| { | ||
| name: "Zero Status Code Defaults to 500", | ||
| err: ErrorEnvelope{Detail: "Something went wrong"}, | ||
| statusCode: 0, | ||
| expectedStatus: http.StatusInternalServerError, | ||
| expectedTitle: "Internal Server Error", | ||
| expectedType: "about:blank", | ||
| }, | ||
| { | ||
| name: "Negative Status Code Defaults to 500", | ||
| err: ErrorEnvelope{Detail: "Something went wrong"}, | ||
| statusCode: -1, | ||
| expectedStatus: http.StatusInternalServerError, | ||
| expectedTitle: "Internal Server Error", | ||
| expectedType: "about:blank", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| rr := httptest.NewRecorder() | ||
| HandleHttpError(rr, tt.err, tt.statusCode) | ||
|
|
||
| if rr.Code != tt.expectedStatus { | ||
| t.Errorf("expected status %d, got %d", tt.expectedStatus, rr.Code) | ||
| } | ||
|
|
||
| contentType := rr.Header().Get("Content-Type") | ||
| if contentType != "application/problem+json" { | ||
| t.Errorf("expected Content-Type application/problem+json, got %q", contentType) | ||
| } | ||
|
|
||
| var got ErrorEnvelope | ||
| if err := json.NewDecoder(rr.Body).Decode(&got); err != nil { | ||
| t.Fatalf("failed to decode response: %v", err) | ||
| } | ||
|
|
||
| if got.Status != tt.expectedStatus { | ||
| t.Errorf("expected envelope status %d, got %d", tt.expectedStatus, got.Status) | ||
| } | ||
|
|
||
| if got.Title != tt.expectedTitle { | ||
| t.Errorf("expected envelope title %q, got %q", tt.expectedTitle, got.Title) | ||
| } | ||
|
|
||
| if got.Type != tt.expectedType { | ||
| t.Errorf("expected envelope type %q, got %q", tt.expectedType, got.Type) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.