Error Handling
A missing user and an unavailable database need different HTTP responses. In Photodrop, the controller maps expected outcomes to stable client responses and returns unexpected errors. The generated HTTP server uses Web with its Echo adapter to handle those returned errors.
Expected Errors
Keep status decisions in the controller. Services return ordinary Go errors so HTTP, commands, and jobs can handle the same outcome differently.
The users lookup handler uses errors.Is so a wrapped ErrUserNotFound still maps to 404. This is the existing Show method in internal/users/controller.go, with errors, net/http, and github.com/goforj/web imported.
// Show translates the service result into the public HTTP contract.
func (c *Controller) Show(ctx web.Context) error {
user, err := c.service.Find(ctx.Context(), ctx.Param("id"))
if errors.Is(err, ErrUserNotFound) {
return ctx.JSON(http.StatusNotFound, map[string]string{
"error": "user not found",
})
}
if err != nil {
return err
}
return ctx.JSON(http.StatusOK, user)
}The request contract applies the same approach to Store.
| Outcome | Controller response |
|---|---|
| The body exceeds the route limit | 413 Content Too Large |
| The body cannot be decoded | 400 with invalid_payload |
| Decoded fields fail validation | 422 with validation_failed and field codes |
| The user does not exist | 404 with {"error":"user not found"} |
| An unexpected service operation fails | Return the error to the HTTP adapter |
These codes and bodies are application contracts. GoForj does not automatically turn an arbitrary service error into a domain-specific response. Do not return raw binder, database, or upstream error text to clients.
Unexpected Errors
Return unexpected errors before writing a response. Wrap them with %w when you need to add safe operational context and preserve errors.Is and errors.As behavior. Avoid logging the same failure at each layer.
In the default HTTP server, an ordinary returned error produces 500 Internal Server Error with this JSON body.
{"message":"Internal Server Error"}This default body differs from the users controller's {"error":...} responses. Clients should not assume all failures share one envelope. A HEAD error response has no body.
The adapter uses Echo's default error handler with error exposure disabled. An error that implements StatusCode() int can choose another status. Echo HTTP errors can also carry a public message. Treat such messages as client-visible data. Use explicit controller responses when you need the stable application codes above.
If a handler has already committed a response, the default error handler leaves it alone. A failed stream or a JSON serialization failure after output begins can leave a partial body. Returning an error cannot undo bytes already sent.
Reporting and Logs
The generated HTTP server enables access logging by default. Its request event includes the returned error when one exists. Echo's default error handler does not separately log the original error. Keep HTTP_ACCESS_LOG_ENABLED enabled or arrange application-owned error reporting when you need those failures in production logs.
In GoForj v0.40.0, the access-log middleware observes the handler before Echo renders a returned error. Its status can reflect the pre-error response status. Use the returned error field and an actual HTTP response test when investigating an unexpected failure, rather than relying on that access-log status alone.
Use Logging for collection and safe fields, Metrics for failure trends, and Inspects for execution details when enabled. Never add passwords, authorization headers, cookies, or raw request bodies to error messages or log fields.
Local Error Detail
When the App environment is local, the generated internal/http/server.go middleware can log HTTP error response events with a written status of 400 or higher and the response body. This capture runs before Echo renders an error returned by the handler. It is useful for explicit controller responses, but it does not guarantee capture of adapter-generated error bodies.
Local capture does not enable error exposure in Echo's default client response. In production, investigate with collected logs, metrics, and Inspects. Keep sensitive details out of response bodies in every environment.
Panics
Return errors for expected failures. The generated HTTP middleware does not install webmiddleware.Recover() automatically. A panic is not the same path as returning an error, and you should not promise a JSON 500 body for it.
If the App needs panic recovery, install Web middleware with webmiddleware.Recover() at the intended route-group or route boundary and test the resulting response. Recovery converts the panic into a returned error for the adapter to handle. It does not make the failed operation safe to retry.
Custom Error Responses
Map application errors in controllers or shared web.Middleware when several routes need one envelope. Keep response shaping near the HTTP boundary and preserve error context for reporting.
Replacing Echo's global error handler is an adapter-level customization in the HTTP server bootstrap. The generated bootstrap lives in internal/http/server.go. Prefer controller or middleware mappings until the entire App needs a different error policy.
Readiness Errors
Readiness uses a separate diagnostic contract. Public responses expose a summary. Authorized readiness includes structured dependency checks only when the caller supplies the diagnostic token. Do not copy raw dependency errors into ordinary API responses.
Verify Error Responses
With the users route running in Photodrop, request a missing user from another terminal.
curl -i http://localhost:3000/api/v1/users/missingExpect 404 Not Found with this body.
{"error":"user not found"}Add HTTP route tests for expected errors and an injected unexpected service failure. Assert the actual status and body. Test that the unexpected failure returns 500 without its internal error text, and that any custom middleware preserves the intended error envelope.