Medical Appointments API

DefaultErrorHandler
in package
implements ErrorHandlerInterface

A custom error handler class that implements the Slim `ErrorHandlerInterface`.

Handles various types of exceptions and generates appropriate error responses.

Tags
author

Luis Graciano contacto@luisgraciano.dev

Interfaces, Classes, Traits and Enums

ErrorHandlerInterface

Table of Contents

__invoke()  : ResponseInterface
Handles the exception and returns a response with appropriate error details.
getResponseWithCorsHeaders()  : ResponseInterface
Add CORS headers to the response and return a JSON response.

Methods

__invoke()

Handles the exception and returns a response with appropriate error details.

public __invoke(ServerRequestInterface $request, Throwable $exception, bool $displayErrorDetails, bool $logErrors, bool $logErrorDetails) : ResponseInterface
Parameters
$request : ServerRequestInterface

The PSR-7 request.

$exception : Throwable

The caught exception.

$displayErrorDetails : bool

Whether to display error details or not.

$logErrors : bool

Whether to log errors or not.

$logErrorDetails : bool

Whether to log error details or not.

Tags
OA\Schema

( schema="ResourceNotFound", title="ResourceNotFound", description="Represents a resource not found error.", required={"path", "code", "message"}, @OA\Property( property="method", type="string", description="The HTTP method.", example="GET" ), @OA\Property( property="path", type="string", description="The path of the resource.", example="/example/path" ), @OA\Property( property="code", type="string", description="The error code.", example="RESOURCE_NOT_FOUND" ), @OA\Property( property="message", type="string", description="The error message.", example="Resource not found." ) )

OA\Schema

( schema="NotFound", title="NotFound", description="Represents a not found error.", required={"message", "code"}, @OA\Property( property="message", type="string", description="The error message.", example="The resource with ID 1 was not found." ), @OA\Property( property="code", type="string", description="The error code.", example="RESOURCE_NOT_FOUND" ) )

OA\Schema

( schema="BusinessValidationError", title="BusinessValidationError", description="Represents a business validation error.", required={"message", "code"}, @OA\Property( property="message", type="string", description="The error message.", example="The parameter 'name' is required." ), @OA\Property( property="code", type="string", description="The error code.", example="PARAMETER_REQUIRED" ) )

OA\Schema

( schema="InternalServerError", title="InternalServerError", description="Represents an internal server error.", required={"code", "message"}, @OA\Property( property="code", type="string", description="The error code.", example="INTERNAL_SERVER_ERROR" ), @OA\Property( property="message", type="string", description="The error message.", example="An error has occurred. Please try again later." ), @OA\Property( property="error", type="string", description="The error details.", example="Parse error: syntax error..." ) )

OA\Response

( response=404, description="Not found", @OA\JsonContent( oneOf={ @OA\Schema(ref="#/components/schemas/NotFound"), @OA\Schema(ref="#/components/schemas/ResourceNotFound") } ) )

OA\Response

( response=400, description="Bad request", @OA\JsonContent(ref="#/components/schemas/BusinessValidationError") )

OA\Response

( response=500, description="Internal server error", @OA\JsonContent(ref="#/components/schemas/InternalServerError") )

Return values
ResponseInterface

The response with error details.

getResponseWithCorsHeaders()

Add CORS headers to the response and return a JSON response.

private getResponseWithCorsHeaders(ServerRequestInterface $request, ResponseInterface $response, mixed $result, int $status_code) : ResponseInterface
Parameters
$request : ServerRequestInterface

The PSR-7 request.

$response : ResponseInterface

The PSR-7 response.

$result : mixed

The response data.

$status_code : int

The HTTP status code.

Return values
ResponseInterface

The response with CORS headers and JSON data.

Search results