HTTPException
extends Exception
in package
Base exception class for HTTP API errors.
Extends Exception with HTTP response code handling and structured error metadata. All API errors should extend or use this class to ensure proper HTTP responses and consistent error JSON formatting.
Key features:
- HTTP status codes: Automatically sets response code via http_response_code()
- Exception chaining: Supports previous exception for debugging
- Structured metadata: The $more field carries contextual data (field errors, etc)
- Smart constructor: Uses named arguments to avoid position confusion
- Fluent throwing: Static throw() method for elegant error creation
Usage:
// Direct throw
throw new HTTPException('invalid_token', 401);
// With metadata
throw new HTTPException('validation_error', 400, null, [
'fields' => ['email' => 'Invalid format']
]);
// Via static method (fluent)
HTTPException::throw(code: 403, msg: 'forbidden', more: ['reason' => 'insufficient_permissions']);
// Exception chaining
HTTPException::throw(code: 500, msg: 'database_error', parent: $pdoException);
Response format (via final_json):
{
"success": false,
"error": "invalid_token",
"type": "HTTPException",
"dbg": {
"more": { "details": "..." },
"trx": [ ... ]
}
}
Tags
Table of Contents
Properties
- $more : mixed
Methods
- __construct() : mixed
- Initialize an HTTP exception with response code and metadata.
- throw() : never
- Throw an exception with flexible named arguments.
Properties
$more read-only
public
mixed
$more
= null
Methods
__construct()
Initialize an HTTP exception with response code and metadata.
public
__construct([string $msg = 'server_error' ][, int $code = 500 ][, Throwable|null $parent = null ][, mixed $more = null ]) : mixed
Parameters
- $msg : string = 'server_error'
-
The error message. Defaults to 'server_error'.
- $code : int = 500
-
HTTP status code. Defaults to 500.
- $parent : Throwable|null = null
-
Previous exception for chaining.
- $more : mixed = null
-
Structured metadata about the error (array, object, etc). Exposed in debug responses for client insight.
throw()
Throw an exception with flexible named arguments.
public
static throw([int|null $code = null ][, string|null $msg = null ][, Throwable|null $parent = null ][, mixed $more = null ]) : never
Static factory method for elegant exception creation. Only non-null arguments are included in the constructor call, allowing clean fluent syntax without positional confusion or needing to pass null for skipped parameters.
Parameters
- $code : int|null = null
-
HTTP status code.
- $msg : string|null = null
-
Error message.
- $parent : Throwable|null = null
-
Previous exception for chaining.
- $more : mixed = null
-
Structured error metadata.
Tags
Return values
never —Always throws the constructed exception.