Exception
Exception
Framework base exception with JSON serialization support.
class Exception extends \Exception implements \JsonSerializableExtends PHP's native exception to normalize default HTTP-style error codes, preserve context from chained exceptions, and expose a stable payload shape for API responses via JsonSerializable.
Properties
class
Class name captured from the previous exception, when available.
protected string $classtrace
Standard stack trace.
protected array $traceMethods
__construct
Constructs a new exception instance.
public __construct(string $message, int $code = 500, ?\Throwable $previousException): voidThe exception code doubles as the HTTP response status throughout the framework (see {@see \Hazaar\Controller\Error::setException()}), so it's constrained to the valid HTTP status range (100-599). Codes outside that range — e.g. a PDO SQLSTATE or driver error code passed through from a wrapped exception — are not valid HTTP statuses and would otherwise reach the response writer unchanged, which some SAPIs (e.g. FrankenPHP) treat as fatal rather than rejecting gracefully. Such codes are normalized to 500 and appended to the message so the original value isn't lost.
Parameters
| Parameter | Type | Description |
|---|---|---|
$message | string | the Exception message to throw |
$code | int | the Exception code (defaults to 500 if not provided or outside the 100-599 HTTP status range) |
$previousException | \Throwable | the previous throwable used for exception chaining |
jsonSerialize
Serializes the exception data to an array suitable for JSON encoding.
public jsonSerialize(): voidThe returned array always includes:
- 'timestamp': The current Unix timestamp with microseconds.
- 'code': The exception code.
- 'message': The exception message.
If the 'display_errors' INI setting is enabled, the array also includes:
- 'file': The filename where the exception was thrown.
- 'line': The line number where the exception was thrown.
- 'class': The class name of the exception.
- 'trace': The stack trace as an array.
getClass
public getClass(): stringgetStackTrace
Returns the stack trace as an array.
public getStackTrace(): voidIf a previous exception was provided during construction, its stack trace is returned. Otherwise, the standard stack trace of this exception is returned.
Generated by Hazaar API Doc Generator on Wed, 22 Jul 2026 14:10:06 +0000