GitHub

server_error

API reference for server_error.

Errors of HTTP servers (Effect's HttpServerError), and the response a failed handler's cause becomes.

Every request failure is one HttpServerError whose reason says what went wrong: RequestParseError (400, or 413 for a body too large), RouteNotFound (404, skipped by error reporters), InternalError (500) or ResponseError (500, a response could not be sent). Each reason is respondable, so a server answers with its status.

Values

AttributeTypeDescription
CLIENT_ABORT_ANNOTATIONContext[ClientAbort | StackTrace]

Types

TypeDefinitionDescription
RequestErrortype RequestError = RequestParseError | RouteNotFound | OriginNotAllowed | InternalError
HttpServerErrorReasontype HttpServerErrorReason = RequestError | ResponseError

Classes

Functions

is_http_server_error

is_http_server_error(value) -> bool
ParameterTypeDescription
valueobject

Returns bool

def is_http_server_error(value: object) -> bool:
    return isinstance(value, HttpServerError)

request_parse_error

request_parse_error(request, description=None, cause=None, kind='Parse') -> HttpServerError
ParameterTypeDescription
requestServerRequest
descriptionstr | None = None
causeobject = None
kindLiteral['Parse', 'BodyTooLarge'] = 'Parse'

Returns HttpServerError

def request_parse_error(
    request: ServerRequest,
    description: str | None = None,
    cause: object = None,
    kind: Literal["Parse", "BodyTooLarge"] = "Parse",
) -> HttpServerError:
    return HttpServerError(RequestParseError(request, description, cause, kind))

cause_response

cause_response[E](cause) -> Effect[tuple[HttpServerResponse, Cause[E]]]

The response for a failed handler's cause, and the cause to report: a response carried by a defect wins; else the last failure's respondable (500 otherwise); an interruption alone is 499 when the client aborted, else 503. The response is added to the reported cause as a defect.

ParameterTypeDescription
causeCause[E]

Returns Effect[tuple[HttpServerResponse, Cause[E]]]

def cause_response[E](cause: Cause[E]) -> Effect[tuple[HttpServerResponse, Cause[E]]]:
    """The response for a failed handler's ``cause``, and the cause to
    report: a response carried by a defect wins; else the last failure's
    respondable (500 otherwise); an interruption alone is 499 when the
    client aborted, else 503. The response is added to the reported cause
    as a defect."""
    response: HttpServerResponse | None = None
    effect = succeed(_INTERNAL_SERVER_ERROR)
    failures: list[Reason[E]] = []
    interrupts: list[Interrupt] = []
    client_interrupt = False
    for reason in cause.reasons:
        match reason:
            case Fail(error):
                effect = to_response_or_else(error, _INTERNAL_SERVER_ERROR)
                failures.append(reason)
            case Die(defect):
                carried = response_of_defect(defect)
                if carried is not None:
                    response = carried
                else:
                    effect = to_response_or_else_defect(defect, _INTERNAL_SERVER_ERROR)
                    failures.append(reason)
            case Interrupt():
                client_interrupt = ClientAbort in reason.annotations
                if not failures:
                    interrupts.append(reason)
    if response is not None:
        return succeed((response, from_reasons(failures)))
    if interrupts and not failures:
        failures.extend(interrupts)
        effect = succeed(_CLIENT_ABORT if client_interrupt else _SERVER_ABORT)

    def with_response(sent: HttpServerResponse) -> tuple[HttpServerResponse, Cause[E]]:
        failures.append(Die(sent))
        return sent, from_reasons(failures)

    return effect.map(with_response)

cause_response_stripped

cause_response_stripped[E](cause) -> tuple[HttpServerResponse, Cause[E] | None]

The response a defect of cause carries (else 500), and the rest of the cause, if any.

ParameterTypeDescription
causeCause[E]

Returns tuple[HttpServerResponse, Cause[E] | None]

def cause_response_stripped[E](
    cause: Cause[E],
) -> tuple[HttpServerResponse, Cause[E] | None]:
    """The response a defect of ``cause`` carries (else 500), and the rest
    of the cause, if any."""
    response: HttpServerResponse | None = None
    rest: list[Reason[E]] = []
    for reason in cause.reasons:
        carried = response_of_defect(reason.defect) if isinstance(reason, Die) else None
        if carried is not None:
            response = carried
        else:
            rest.append(reason)
    return (
        _INTERNAL_SERVER_ERROR if response is None else response,
        from_reasons(rest) if rest else None,
    )

exit_response

exit_response[E](exit) -> HttpServerResponse

The response of a handler's exit.

ParameterTypeDescription
exitExit[HttpServerResponse, E]

Returns HttpServerResponse

def exit_response[E](exit: Exit[HttpServerResponse, E]) -> HttpServerResponse:
    """The response of a handler's exit."""
    if isinstance(exit, Success):
        return exit.value
    return cause_response_stripped(exit.cause)[0]