Advanced Guide
Error Handling
GoFr provides a structured error handling approach to simplify error management in your applications. The errors package in GoFr provides functionality for handling errors in GoFr applications. It includes predefined HTTP and database errors, as well as the ability to create custom errors with additional context.
Pre-defined HTTP Errors
GoFr's http package offers several predefined error types to represent common HTTP error scenarios. These errors automatically handle HTTP status code selection. These include:
| Error Type | Description | Status Code |
|---|---|---|
ErrorInvalidParam | Represents an error due to an invalid parameter | 400 (Bad Request) |
ErrorMissingParam | Represents an error due to a missing parameter | 400 (Bad Request) |
ErrorEntityNotFound | Represents an error due to a not found entity | 404 (Not Found) |
ErrorEntityAlreadyExist | Represents an error due to creation of duplicate entity | 409 (Conflict) |
ErrorInvalidRoute | Represents an error for invalid route | 404 (Not Found) |
ErrorMethodNotAllowed | Represents an error for a registered route requested with a method it does not accept | 405 (Method Not Allowed) |
ErrorRequestTimeout | Represents an error for request which timed out | 408 (Request Timeout) |
ErrorPanicRecovery | Represents an error for request which panicked | 500 (Internal Server Error) |
A request for a path that is registered, but not for the method used, is answered with ErrorMethodNotAllowed: a 405 whose Allow header lists the methods the path accepts. A path that is not registered for any method is answered with ErrorInvalidRoute.
This includes HEAD: a HEAD request to a path registered only for GET is answered with 405, not 404, and its Allow header names GET. Register HEAD on the path if it should accept it. A health probe that sends HEAD sees this.
Usage:
To use the predefined HTTP errors, users need to import the GoFr http package and can simply call them:
import "gofr.dev/pkg/gofr/http"
err := http.ErrorMissingParam{Params: []string{"id"}}
Database Errors
Database errors in GoFr, represented in the datasource package, encapsulate errors related to database operations such as database connection, query failure, availability etc. The ErrorDB struct can be used to populate error as well as any custom message to it. Status Code: 500 (Internal Server Error)
Usage:
import "gofr.dev/pkg/gofr/datasource"
// Creating a custom error wrapped in underlying error for database operations
dbErr := datasource.ErrorDB{Err: err, Message: "error from sql db"}
// Adding stack trace to the error
dbErr = dbErr.WithStack()
// Creating a custom error only with error message and no underlying error.
dbErr2 := datasource.ErrorDB{Message : "database connection timed out!"}
Custom Errors
GoFr's error structs implements an interface with Error() string and StatusCode() int methods, users can override the status code by implementing it for their custom error.
Users can optionally define a log level for your error with the LogLevel() logging.Level methods
Usage:
type customError struct {
error string
}
func (c customError) Error() string {
return fmt.Sprintf("custom error: %s", c.error)
}
func (c customError) StatusCode() int {
return http.StatusMethodNotAllowed
}
func (c customError) LogLevel() logging.Level {
return logging.WARN
}
Extended Error Responses
For RFC 9457 style error responses with additional fields, implement the ResponseMarshaller interface:
type ResponseMarshaller interface {
Response() map[string]any
}
Usage:
type ValidationError struct {
Field string
Message string
Code int
}
func (e ValidationError) Error() string { return e.Message }
func (e ValidationError) StatusCode() int { return e.Code }
func (e ValidationError) Response() map[string]any {
return map[string]any{
"field": e.Field,
"type": "validation_error",
"details": "Invalid input format",
}
}
[!NOTE] The
messagefield is automatically populated from theError()method. Custom fields with the name "message" in theResponse()map should not be used as they will be ignored in favor of theError()value.
Hiding Internal Error Details
By default, the message a client receives for an error is the text of err.Error(). For an error GoFr cannot classify, or a database error, that text can include internal details such as SQL fragments, hostnames or driver messages. Set HIDE_INTERNAL_ERRORS=true to keep that text away from clients.
When it is enabled, the message is replaced with Internal Server Error when either (gRPC uses its own rule, described under Where it applies):
- the error does not implement
StatusCode() int, such as an error fromerrors.New, a raw driver error, or any error wrapped withfmt.Errorf("...: %w", err)(the wrapper does not carry the status code of the error inside it), or - its
StatusCode()returns 0, which the responder sends as 500, or - the error is a
datasource.ErrorDB(or*datasource.ErrorDB), which usually carries the driver's own text.
Every other error keeps its message, because an error that declares a status code was written for clients. This includes GoFr's predefined errors, such as ErrorEntityNotFound or ErrorInvalidParam, and your own errors with a StatusCode(), including a custom error that declares 500.
| Error returned by the handler | Message the client receives |
|---|---|
errors.New("pq: relation \"users\" does not exist") | Internal Server Error |
datasource.ErrorDB{Err: err, Message: "select failed"} | Internal Server Error |
fmt.Errorf("load user: %w", http.ErrorEntityNotFound{Name: "id", Value: "1"}) | Internal Server Error |
a custom error with StatusCode() 500 and the text payment provider is down, retry later | payment provider is down, retry later |
a custom error with StatusCode() 404, whatever its text | its own text |
[!WARNING] A message is kept whenever the error declares a status code, so text you put in your own errors still reaches clients, and so do its custom
ResponseMarshallerfields. This option keeps unclassified and database error text out of responses; it does not inspect what your own errors say.
When an error's message is replaced, its custom ResponseMarshaller fields are dropped too, so the client receives only {"message":"Internal Server Error"}. An error that is not replaced keeps its custom fields.
Example:
app.GET("/users/{id}", func(ctx *gofr.Context) (any, error) {
return nil, errors.New("dial tcp 10.0.3.7:5432: connect: connection refused")
})
Without HIDE_INTERNAL_ERRORS (or with false), the response is:
{
"error": {
"message": "dial tcp 10.0.3.7:5432: connect: connection refused"
}
}
With HIDE_INTERNAL_ERRORS=true, the response is:
{
"error": {
"message": "Internal Server Error"
}
}
Where it applies
- HTTP responses: errors returned by handlers and errors written by GoFr's middleware (authentication, rate limiting, the
QUERYcontent-type check). The status code stays the same, the customResponseMarshallerfields of a replaced error are dropped, and a handler that returns data together with an error still responds with 206 (Partial Content) and its data. - SSE and NDJSON streams: the terminal error frame says
{"error":"Internal Server Error"}when the stream's source fails with such an error. - GraphQL: a resolver's error, or a panic in a resolver, appears in
errors[].messageasInternal Server Error. Errors from parsing and validating the query are not affected. - gRPC: here an error is classified by its gRPC status, not by
StatusCode(). An error that is not a gRPCstatuserror is sent ascodes.Internalwith the messageInternal Server Error, instead ofcodes.Unknownwith the error's text. This includes your own errors with aStatusCode(), such as a custom 500 orErrorEntityNotFound: return astatus.Errorwith the code and message you want the client to see. Astatuserror returned as is is sent with its own message, including one passed on from a downstream gRPC call. When astatuserror is wrapped with%w, the client still gets its code but the message is replaced withInternal Server Error, because the wrapper's text can carry internal detail.context.Canceledandcontext.DeadlineExceededkeep their code; when wrapped, the wrapper's text is dropped. With the option off, the interceptor chain is unchanged. - MCP: a tool's error, and an error from the hook that runs before a tool, reach the AI client as
Internal Server Error.
[!NOTE]
- The original error is still logged on the server (for HTTP handlers, with the request's trace ID), so the real cause can be found for the request the client reports. A stream source's error, and the error of a custom
AuthProviderpassed tomiddleware.AuthMiddleware, are logged when their text is replaced, without a trace ID.- The switch is process-wide, not per app: once any
gofr.New()in the process readsHIDE_INTERNAL_ERRORS=true, it stays on for every app in the process, even one created later with the option unset orfalse.- Two GoFr messages that contain no internal detail are not replaced: the stream frame
stream value could not be encodedand the MCP tool panic texttool execution failed.- Any value other than
trueorfalselogs a warning at startup and keeps the default behavior.