ExceptionHandler
in package
Global exception and error handler for LWT.
Registers handlers for uncaught exceptions and PHP errors, logs errors, and displays appropriate error pages.
Tags
Table of Contents
Properties
- $debug : bool
- Whether we're in debug mode (show detailed errors).
- $instance : self|null
- The singleton instance.
- $logFile : string|null
- Path to the log file.
- $registered : bool
- Whether handlers are registered.
Methods
- __construct() : mixed
- Create a new exception handler.
- getInstance() : self
- Get or create the singleton instance.
- handleError() : bool
- Handle a PHP error by converting to ErrorException.
- handleException() : void
- Handle an uncaught exception.
- handleShutdown() : void
- Handle fatal errors on shutdown.
- isRegistered() : bool
- Check if handlers are registered.
- logException() : void
- Log an exception.
- register() : void
- Register the exception and error handlers.
- reset() : void
- Reset the handler state (for testing).
- setDebug() : self
- Set debug mode.
- setLogFile() : self
- Set the log file path.
- formatLogMessage() : string
- Format an exception for logging.
- getStatusTitle() : string
- Get a title for the HTTP status code.
- getUserMessage() : string
- Get a user-friendly message for the exception.
- isApiRequest() : bool
- Check if the current request is an API request.
- isRunningInPhpUnit() : bool
- Check if running in PHPUnit.
- renderHtmlError() : void
- Render an HTML error page.
- renderJsonError() : void
- Render a JSON error response.
- writeToLogFile() : void
- Write a message to the log file.
Properties
$debug
Whether we're in debug mode (show detailed errors).
private
bool
$debug
$instance
The singleton instance.
private
static self|null
$instance
= null
$logFile
Path to the log file.
private
string|null
$logFile
$registered
Whether handlers are registered.
private
static bool
$registered
= false
Methods
__construct()
Create a new exception handler.
public
__construct([bool $debug = false ][, string|null $logFile = null ]) : mixed
Parameters
- $debug : bool = false
-
Whether to show detailed error information
- $logFile : string|null = null
-
Path to log file (null to disable file logging)
getInstance()
Get or create the singleton instance.
public
static getInstance([bool $debug = false ][, string|null $logFile = null ]) : self
Parameters
- $debug : bool = false
-
Whether to show detailed errors
- $logFile : string|null = null
-
Path to log file
Return values
selfhandleError()
Handle a PHP error by converting to ErrorException.
public
handleError(int $severity, string $message, string $file, int $line) : bool
Parameters
- $severity : int
-
Error severity level
- $message : string
-
Error message
- $file : string
-
File where error occurred
- $line : int
-
Line number
Tags
Return values
bool —False to use PHP's default error handler
handleException()
Handle an uncaught exception.
public
handleException(Throwable $exception) : void
Parameters
- $exception : Throwable
-
The exception to handle
handleShutdown()
Handle fatal errors on shutdown.
public
handleShutdown() : void
isRegistered()
Check if handlers are registered.
public
static isRegistered() : bool
Return values
boollogException()
Log an exception.
public
logException(Throwable $exception) : void
Parameters
- $exception : Throwable
-
The exception to log
register()
Register the exception and error handlers.
public
register() : void
reset()
Reset the handler state (for testing).
public
static reset() : void
setDebug()
Set debug mode.
public
setDebug(bool $debug) : self
Parameters
- $debug : bool
-
Whether to show debug information
Return values
selfsetLogFile()
Set the log file path.
public
setLogFile(string|null $logFile) : self
Parameters
- $logFile : string|null
-
Path to log file
Return values
selfformatLogMessage()
Format an exception for logging.
private
formatLogMessage(Throwable $exception) : string
Parameters
- $exception : Throwable
-
The exception to format
Return values
stringgetStatusTitle()
Get a title for the HTTP status code.
private
getStatusTitle(int $statusCode) : string
Parameters
- $statusCode : int
-
HTTP status code
Return values
stringgetUserMessage()
Get a user-friendly message for the exception.
private
getUserMessage(Throwable $exception) : string
Parameters
- $exception : Throwable
-
The exception
Return values
stringisApiRequest()
Check if the current request is an API request.
private
isApiRequest() : bool
Return values
boolisRunningInPhpUnit()
Check if running in PHPUnit.
private
isRunningInPhpUnit() : bool
Return values
boolrenderHtmlError()
Render an HTML error page.
private
renderHtmlError(Throwable $exception, int $statusCode) : void
Parameters
- $exception : Throwable
-
The exception
- $statusCode : int
-
HTTP status code
renderJsonError()
Render a JSON error response.
private
renderJsonError(Throwable $exception, int $statusCode) : void
Parameters
- $exception : Throwable
-
The exception
- $statusCode : int
-
HTTP status code
writeToLogFile()
Write a message to the log file.
private
writeToLogFile(string $message) : void
Parameters
- $message : string
-
The message to write