-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add RFC 7807 Problem Details Support for Error Responses #1505
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
Add RFC 7807 Problem Details Support for Error Responses #1505
Conversation
- Add ProblemDetails structure and methods - Implement common error types (validation, auth, etc.) - Add error handling middleware - Include comprehensive tests - Add usage example
@jsxs0 Thank you for your contribution to implementing RFC 7807 for standardized error responses. This is a valuable addition to the GoFr framework. However, I have a few suggestions to improve the implementation:
Suggested ApproachI propose an alternative approach that leverages configuration to determine the error response format. This approach allows for more flexibility and extensibility. Environment Variable ConfigurationWe introduce an
ImplementationIn the Responder, we check the Example Usagepackage main
import (
"gofr.dev/pkg/gofr"
"net/http"
)
type MyCustomError struct {
Type string `json:"type"`
Title string `json:"title"`
Status int `json:"status"`
Detail string `json:"detail"`
Instance string `json:"instance"`
}
func (e MyCustomError) MarshalJSON() ([]byte, error) {
// marshal the struct
}
func main() {
app := gofr.New()
app.GET("/users/:id", func(c *gofr.Context) (interface{}, error) {
id := c.PathParam("id")
if id == "invalid" {
return nil, MyCustomError{
Type: "https://api.example.com/problems/validation",
Title: "Validation Error",
Status: http.StatusBadRequest,
Detail: "Invalid user ID format",
}
}
if id == "999" {
return nil, MyCustomError{
Type: "https://api.example.com/problems/not-found",
Title: "Resource Not Found",
Status: http.StatusNotFound,
Detail: "User not found",
Instance: "/users/999",
}
}
return map[string]string{"id": id, "name": "John Doe"}, nil
})
app.Run()
} Please do let me know your thoughts on this. |
ERROR_RESPONSE env variable based error outputs does not feel like a good usecase for a configuration. Considering the framework calls itself opinionated, it should decide the error format and stick to it. Users who want different formats for some reason, should be able to implement a custom error struct. |
@Umang01-hash, @vikash Thank you both for your feedback. I've revised the code to address both perspectives while maintaining the framework's opinionated nature:
I hope this approach provides a good balance. It maintains a consistent error-handling strategy while offering flexibility for various use cases. Please check the updated example in |
I believe there's a better alternative to this. Adding the Optional pattern still restricts the error fields to what we define, limiting flexibility. I recently discussed this with @Umang01-hash, and I found his approach more effective. It would be great if you could have a discussion with him on Discord and collaborate on this PR as co-authors. |
With the current implementation, handlers are expected to return 'error' - which is an interface. That can be used to provide any kind of information about the error. Besides RFC is not a standard, yet. Although there exist a need to provide structured error, the direction of the PR is not consistent with how errors are handled in the framework. Considering these, we are closing the PR for now. We need to have a discussion on this. |
Description:
This PR implements RFC 7807 (Problem Details for HTTP APIs) to provide a standardized way of handling API error responses in GoFr. It adds support for structured error responses with standard fields like type, title, status, detail and instance, along with extensible custom fields.
The implementation includes common error types for validation, authentication, authorization and not found errors, with appropriate status codes and messages. The error handling middleware automatically converts errors to the RFC 7807 format.
Fixes #1445
Breaking Changes (if applicable):
None. The implementation is backward compatible with existing error handling.
Additional Information:
Example usage:
Response format:
Checklist:
goimport
andgolangci-lint