A Go library for implementing RFC 9457 Problem Details for HTTP APIs. It provides a standard way to carry machine-readable details of errors in HTTP response content.
go get luigimorel.com/coronapackage main
import (
"net/http"
"luigimorel.com/corona"
)
func handler(w http.ResponseWriter, r *http.Request) {
// Create a problem for a bad request
problem := corona.BadRequest("Invalid input provided")
problem.WriteTo(w, r)
}problem := corona.New(
http.StatusNotFound,
"https://example.com/errors/not-found",
"Resource Not Found",
"The requested resource could not be found",
).WithInstance("/api/users/123")
problem.WriteTo(w, r)problem := corona.NewValidationProblem([]corona.ValidationError{
{Detail: "Name is required", Pointer: "/name"},
{Detail: "Email must be valid", Pointer: "/email"},
})
problem.WriteTo(w, r){
"type": "https://example.com/errors/validation",
"title": "Validation Error",
"status": 422,
"detail": "Your request parameters didn't validate.",
"errors": [
{
"detail": "Name is required",
"pointer": "/name"
}
]
}When the client accepts application/problem+xml, the response will be in XML format:
<problem xmlns="urn:ietf:rfc:7807">
<type>https://example.com/errors/validation</type>
<title>Validation Error</title>
<status>422</status>
<detail>Your request parameters didn't validate.</detail>
</problem>Problem: The main problem details structureValidationError: Example extension for validation errors
New(status int, typeURI, title, detail string) *Problem: Create a new problemNewFromStatus(status int) *Problem: Create a problem from HTTP statusBadRequest(detail string) *Problem: Create 400 Bad RequestUnauthorized(detail string) *Problem: Create 401 UnauthorizedForbidden(detail string) *Problem: Create 403 ForbiddenNotFound(detail string) *Problem: Create 404 Not FoundConflict(detail string) *Problem: Create 409 ConflictUnprocessableEntity(detail string) *Problem: Create 422 Unprocessable EntityInternalServerError(detail string) *Problem: Create 500 Internal Server ErrorServiceUnavailable(detail string) *Problem: Create 503 Service UnavailableNewValidationProblem(errors []ValidationError) *Problem: Create validation error problem
WithInstance(instance string) *Problem: Set instance URIWithExtension(name string, value interface{}) *Problem: Add custom extensionWriteTo(w http.ResponseWriter, r *http.Request) error: Write to HTTP responseError() string: Implement error interface
This project is licensed under the MIT License - see the LICENSE file for details.