Responses and errors
What a handler returns: values, response helpers and errors. For a guide, see Responses and Errors.
What a handler returns
Response helpers
All of them are exported by @dotpress/core. init accepts status and headers; headers set on ctx.res are merged.
redirect() sends the location as given: behind a proxy, build it with ctx.publicPath('/login').
stream()
The source is a ReadableStream, an iterable, an async iterable, or a function returning one. A function receives { signal }, aborted when the client disconnects. Chunks are strings, encoded as UTF-8, or Uint8Array. A string or a Uint8Array given as the source is one chunk.
sse()
The source yields events, or strings sent as data:
sse() sets Content-Type: text/event-stream, Cache-Control: no-cache and X-Accel-Buffering: no (no buffering in nginx).
file()
body is a Uint8Array, an ArrayBuffer, a Blob or a ReadableStream. The content type defaults to the type of the Blob, else application/octet-stream. Content-Length is set when the size is known, and filename is encoded for non-ASCII names.
Error helpers
Each one returns an HttpError: return notFoundError() or throw notFoundError(). message replaces the default message; data adds details to the body.
Errors produced by Dotpress
Validation issues have this shape:
Error body
Every error response has the same JSON body:
With exposeErrors: true, the data of a 500 holds the name, message and stack of the error, and the issues of an invalid response. Never in production.
HttpError
Subclass it, or wrap errorResponse(), for the errors of your domain:
isHttpError(value) recognizes an HttpError, even one created by another copy of @dotpress/core. toErrorBody(error) returns its body.
Headers set on ctx.res are kept on the response of an HttpError, not on the 500 of an unexpected error. See Custom errors and Unexpected errors.