Error pages
GOX allows you to customize the generated responses when an request ends with an HTTP error.
These pages are used to answer errors like 403, 404 or 500in addition to any other state code between 400 and 599.
Error pages use the same capabilities as any other gox.Page, so they can build templates, use components and access the gox.Context.
Special file
Error pages are defined in a special file located in the directory pages/.
pages/
└── .error.gox This file can declare one or more error handlers.
module error
import "github.com/gox-lang/gox"
func Default(ctx *gox.Context) gox.Page {
return <span>
{{ ctx.Error.GetStatusCode() }}
-
{{ ctx.Error.GetStatusMessage() }}
</span>
}
func Error404(ctx *gox.Context) gox.Page {
return <span>Página no encontrada.</span>
}
func Error500(ctx *gox.Context) gox.Page {
return <span>Error interno del servidor.</span>
} GOX automatically detects this file during boot and uses it to respond HTTP errors.
Specific handlers
Each function represents a specific HTTP status code.
func Error403(ctx *gox.Context) gox.Page func Error404(ctx *gox.Context) gox.Page func Error500(ctx *gox.Context) gox.Page When an request ends with a given status code, GOX tries to run the corresponding handler.
Default handler
In addition to specific handlers, a function can be declared Default.
func Default(ctx *gox.Context) gox.Page {
return <span>
{{ ctx.Error.GetStatusCode() }}
-
{{ ctx.Error.GetStatusMessage() }}
</span>
} GOX uses this handler when there is no specific function for the generated status code.
Handler resolution
When an error occurs, GOX tries to solve the response using the following order.
Error<status>
↓
Default
↓
Respuesta interna del servidor For example, a mistake 404 use the next flow.
Error404
↓
Default
↓
Respuesta interna Error context
During the execution of an error page, ctx.Error contains information on the error that caused the response.
func Error404(ctx *gox.Context) gox.Page {
return <main>
<h1>{{ ctx.Error.GetStatusCode() }}</h1>
<p>{{ ctx.Error.GetStatusMessage() }}</p>
</main>
} On a normal page, ctx.Error It usually is nil.
The complete documentation for this object is found in Error.
End a response with an error
A page can also end the response by using ctx.Response.DoneWith.
func GET(ctx *gox.Context) gox.Page {
if !CurrentUser(ctx).IsAdmin() {
ctx.Response.DoneWith(403)
}
return <h1>Panel de administración</h1>
} When the status code represents an HTTP error, GOX interrupts the execution of the page and runs the corresponding handler defined in .error.gox.
Custom response
DoneWith also allows to respond immediately using a specific component.
func AccessDenied() gox.Component {
return <h1>Access denied</h1>
}
func GET(ctx *gox.Context) gox.Page {
if !CurrentUser(ctx).IsAdmin() {
ctx.Response.DoneWith(
403,
AccessDenied,
)
}
return <h1>Panel de administración</h1>
} When a component is provided, GOX directly uses that content as a response and does not run the handlers defined in .error.gox.
Lifecycle
The error pages are part of the normal GOX response cycle.
For this reason, they can only be executed as long as the HTTP response has not yet started to be sent to the client.
Once GOX starts writing the response body, it is no longer possible to replace it with a custom error page.
Integration with Go
Error pages are normal Go functions.
They can use components, auxiliary functions, structures, interfaces and any other language feature to build the response.
func Error500(ctx *gox.Context) gox.Page {
support := "[email protected]"
return <main>
<h1>Internal Server Error</h1>
<p>
Contact {{ support }}
</p>
</main>
} Best practices
- Define a handler
Defaultto cover any non-personalized status code. - Create specific handlers only when the response must be different.
- Use
ctx.Response.DoneWithto end the response when the error is part of the normal flow of the application. - Use
ctx.Erroronly within the error pages. - Avoid revealing internal application information in error responses.
To learn more about PagesSee your documentation. To learn more about ContextSee your documentation. To learn more about Response, check your documentation.