Skip to content

Developer Guide Responses and Errors

Stefan Crkvenjakov edited this page Oct 7, 2026 · 1 revision

Responses, redirects and error handling

Developer Guide · Routing

Internals: Responses, Errors, Response, Redirect, ErrorHandler functions.

Actions return View, Redirect or Response. Bootstrap renders views, emits redirect headers, or calls Response::send(). Returning a Response lets routing and tests inspect behavior without sending immediately.

Excerpt as a controller action, inside an existing Controller subclass:

public function status(array|string $params): \response\Response
{
    return \response\Response::make(200)->data(['ready' => true])->asJson();
}

The emitted JSON is {"status":200,"message":"OK","data":{"ready":true}}. Arrays sent as application/json always get that envelope. Use make(status, message) or message() to customize the message; status() does not automatically update the text.

Builder method Use
make(status, message), status(), message() Status code and envelope message.
header(name, value) Add/replace a named header.
data(array) Structured payload.
file(path) File path payload, with MIME inferred from its extension when recognized.
as(mime) Explicit sender MIME.
asJson/Html/Xml/Css/Js/Text/Pdf/File Convenience MIME selections.
send() Emit status/headers/body using sender registry.
redirect(url, status) Immediate headers and process exit; return type void.

Explicit MIME wins, then recognized file extension, Accept header, then application/json. Accept parsing is a simple implemented resolver, not a guarantee of full negotiation. Supported senders cover JSON, XML, HTML/text, CSS/JS, images, video/audio, PDF and binary files. Inspect individual sender references for array/file conversion and missing-file behavior. An unregistered MIME raises RuntimeException; select asFile() explicitly for binary output. Response::setRegistry() can provide custom senders; SenderRegistry::register()/resolve()/has() are the relevant APIs.

Excerpt inside an action for a deferred redirect:

return \response\Response::make(303)->header('Location', '/hello')->asHtml();

Alternatively (new \redirect\Redirect())->redirect('/hello') returns a Redirect object that the bootstrap emits. Its construction initializes breadcrumb/session behavior. Redirect::route(name) uses an explicitly supplied name-to-URL array; it does not automatically query RouterManager. back() uses the last rendered page. After a successful view render the bootstrap records the current URI; Response branches exit before that breadcrumb update.

Handle expected failures

Validate request data in your action/service and return a suitable Response or view. The local application catches service exceptions, returns validation errors with 422, throttling with 429, and storage failures with 503. The framework has no public validator/form-request abstraction that replaces application checks.

Unexpected installed-boot exceptions go to handle_exception(). It wraps non-ExceptionBase errors in AWTException, follows previous exceptions for the root source, and produces CLI ANSI output or a 500 Blade error page. DEBUG adds details; production rendering uses a generic error template. CLI exception handling exits 1. Installed bootstrap registers a shutdown handler for E_PARSE/E_CORE_ERROR/E_COMPILE_ERROR, not every fatal condition. The uninstalled CLI branch does not register these handlers.

ExceptionBase carries an optional/current package Context and a previous Throwable; subclass buildMessage() customizes the message. RuntimeException is instead a normal RuntimeException subclass with its own constructor. Use ordinary InvalidArgumentException/RuntimeException for package failures unless a framework wrapper is needed to retain context.

Two local catch paths limit central handling: View::render() catches Throwable and prints a rendering error, and bootstrap/routes/router.php catches Exception around response/render emission and dies with a message. The route manager call happens before that local try. Handle/record expected failures at the service boundary and inspect these paths when troubleshooting; do not assume one uniform error response/logging policy.

There is no framework logging facade, structured log configuration or retry queue. The inspected package uses PHP error_log() for failures. Avoid exposing raw exception messages to production users in your own actions.

Clone this wiki locally