Skip to content

Type Alias: HTTPErrorOrigin ​

HTTPErrorOrigin = "action" | "request"

Where an error answered by an HTTP adapter comes from, given to the error of its responder: 'action' when the request reached an action and its dispatch failed (the entrypoint's middlewares, the validation of the input, the access control, the action itself); 'request' when it failed before (a middleware of the web framework such as an authenticator, a hook of fastify, a body too large) or matched no route.

Example ​

typescript
import type {Request, Response} from 'express'
import {describeError, type ExpressResponder, type HTTPErrorOrigin, statusOfError} from 'lakutata/adapter/express'

//The errors of the actions answered with 200, the others with their status
export const responder: ExpressResponder = {
    success: (response: Response, data: unknown): void => void response.json({data: data}),
    error: (response: Response, error: unknown, _request: Request, origin?: HTTPErrorOrigin): void => {
        response.status(origin === 'action' ? 200 : statusOfError(error)).json(describeError(error))
    }
}

Released under the MIT License.