Exceptions
in package
uses
ResponseTrait
Exceptions manager
Tags
Table of Contents
Properties
- $ob_level : int
- Nesting level of the output buffering mechanism
- $codes : array<string, int>
- Allows child classes to override the status code that is used in their API.
- $config : Exceptions
- Config for debug exceptions.
- $format : string|null
- How to format the response data.
- $formatter : FormatterInterface|null
- Current Formatter instance. This is usually set by ResponseTrait::format
- $request : RequestInterface|null
- The request.
- $response : ResponseInterface
- The outgoing response.
- $viewPath : string
- The path to the directory containing the cli and html error view directories.
- $exceptionCaughtByExceptionHandler : Throwable|null
Methods
- __construct() : mixed
- cleanPath() : string
- This makes nicer looking paths for the error output.
- describeMemory() : string
- Describes memory usage in real-world units. Intended for use with memory_get_usage, etc.
- errorHandler() : bool
- The callback to be registered to `set_error_handler()`.
- exceptionHandler() : void
- Catches any uncaught errors and exceptions, including most Fatal errors (Yay PHP7!). Will log the error, display it if display_errors is on, and fire an event that allows custom actions to be taken at this point.
- highlightFile() : bool|string
- Creates a syntax-highlighted version of a PHP file.
- initialize() : void
- Responsible for registering the error, exception and shutdown handling of our application.
- shutdownHandler() : void
- Checks to see if any errors have happened during shutdown that need to be caught and handle them.
- collectVars() : array<string|int, mixed>
- Gathers the variables that will be made available to the view.
- determineCodes() : array<string|int, mixed>
- Determines the HTTP status code and the exit status code for this request.
- determineView() : string
- Determines the view to display based on the exception thrown, whether an HTTP or CLI request, etc.
- fail() : ResponseInterface
- Used for generic failures that no custom methods exist for.
- failForbidden() : ResponseInterface
- Used when access is always denied to this resource and no amount of trying again will help.
- failNotFound() : ResponseInterface
- Used when a specified resource cannot be found.
- failResourceExists() : ResponseInterface
- Use when trying to create a new resource and it already exists.
- failResourceGone() : ResponseInterface
- Use when a resource was previously deleted. This is different than Not Found, because here we know the data previously existed, but is now gone, where Not Found means we simply cannot find any information about it.
- failServerError() : ResponseInterface
- Used when there is a server error.
- failTooManyRequests() : ResponseInterface
- Used when the user has made too many requests for the resource recently.
- failUnauthorized() : ResponseInterface
- Used when the client is either didn't send authorization information, or had bad authorization credentials. User is encouraged to try again with the proper information.
- failValidationError() : ResponseInterface
- Used when the data provided by the client cannot be validated.
- failValidationErrors() : ResponseInterface
- Used when the data provided by the client cannot be validated on one or more fields.
- format() : string|null
- Handles formatting a response. Currently, makes some heavy assumptions and needs updating! :)
- maskSensitiveData() : array<string|int, mixed>
- Mask sensitive data in the trace.
- render() : void
- Given an exception and status code will display the error to the client.
- respond() : ResponseInterface
- Provides a single, simple method to return an API response, formatted to match the requested format, with proper content-type and status code.
- respondCreated() : ResponseInterface
- Used after successfully creating a new resource.
- respondDeleted() : ResponseInterface
- Used after a resource has been successfully deleted.
- respondNoContent() : ResponseInterface
- Used after a command has been successfully executed but there is no meaningful reply to send back to the client.
- respondUpdated() : ResponseInterface
- Used after a resource has been successfully updated.
- setResponseFormat() : $this
- Sets the format the response should be in.
- handleDeprecationError() : true
- isDeprecationError() : bool
- maskData() : array<string|int, mixed>|object
- renderBacktrace() : string
Properties
$ob_level
Nesting level of the output buffering mechanism
No longer used. Moved to BaseExceptionHandler.
public
int
$ob_level
$codes
Allows child classes to override the status code that is used in their API.
protected
array<string, int>
$codes
= ['created' => 201, 'deleted' => 200, 'updated' => 200, 'no_content' => 204, 'invalid_request' => 400, 'unsupported_response_type' => 400, 'invalid_scope' => 400, 'temporarily_unavailable' => 400, 'invalid_grant' => 400, 'invalid_credentials' => 400, 'invalid_refresh' => 400, 'no_data' => 400, 'invalid_data' => 400, 'access_denied' => 401, 'unauthorized' => 401, 'invalid_client' => 401, 'forbidden' => 403, 'resource_not_found' => 404, 'not_acceptable' => 406, 'resource_exists' => 409, 'conflict' => 409, 'resource_gone' => 410, 'payload_too_large' => 413, 'unsupported_media_type' => 415, 'too_many_requests' => 429, 'server_error' => 500, 'unsupported_grant_type' => 501, 'not_implemented' => 501]
$config
Config for debug exceptions.
protected
Exceptions
$config
$format
How to format the response data.
protected
string|null
$format
= 'json'
Either 'json' or 'xml'. If null is set, it will be determined through content negotiation.
Tags
$formatter
Current Formatter instance. This is usually set by ResponseTrait::format
protected
FormatterInterface|null
$formatter
$request
The request.
protected
RequestInterface|null
$request
$response
The outgoing response.
protected
ResponseInterface
$response
$viewPath
The path to the directory containing the cli and html error view directories.
No longer used. Moved to BaseExceptionHandler.
protected
string
$viewPath
$exceptionCaughtByExceptionHandler
private
Throwable|null
$exceptionCaughtByExceptionHandler
= null
Methods
__construct()
public
__construct(Exceptions $config) : mixed
Parameters
- $config : Exceptions
cleanPath()
This makes nicer looking paths for the error output.
public
static cleanPath(string $file) : string
Use dedicated clean_path()
function.
Parameters
- $file : string
Return values
stringdescribeMemory()
Describes memory usage in real-world units. Intended for use with memory_get_usage, etc.
public
static describeMemory(int $bytes) : string
No longer used. Moved to BaseExceptionHandler.
Parameters
- $bytes : int
Return values
stringerrorHandler()
The callback to be registered to `set_error_handler()`.
public
errorHandler(int $severity, string $message[, string|null $file = null ][, int|null $line = null ]) : bool
Parameters
- $severity : int
- $message : string
- $file : string|null = null
- $line : int|null = null
Tags
Return values
boolexceptionHandler()
Catches any uncaught errors and exceptions, including most Fatal errors (Yay PHP7!). Will log the error, display it if display_errors is on, and fire an event that allows custom actions to be taken at this point.
public
exceptionHandler(Throwable $exception) : void
Parameters
- $exception : Throwable
highlightFile()
Creates a syntax-highlighted version of a PHP file.
public
static highlightFile(string $file, int $lineNumber[, int $lines = 15 ]) : bool|string
No longer used. Moved to BaseExceptionHandler.
Parameters
- $file : string
- $lineNumber : int
- $lines : int = 15
Return values
bool|stringinitialize()
Responsible for registering the error, exception and shutdown handling of our application.
public
initialize() : void
Tags
shutdownHandler()
Checks to see if any errors have happened during shutdown that need to be caught and handle them.
public
shutdownHandler() : void
Tags
collectVars()
Gathers the variables that will be made available to the view.
protected
collectVars(Throwable $exception, int $statusCode) : array<string|int, mixed>
No longer used. Moved to BaseExceptionHandler.
Parameters
- $exception : Throwable
- $statusCode : int
Return values
array<string|int, mixed>determineCodes()
Determines the HTTP status code and the exit status code for this request.
protected
determineCodes(Throwable $exception) : array<string|int, mixed>
Parameters
- $exception : Throwable
Return values
array<string|int, mixed>determineView()
Determines the view to display based on the exception thrown, whether an HTTP or CLI request, etc.
protected
determineView(Throwable $exception, string $templatePath) : string
No longer used. Moved to ExceptionHandler.
Parameters
- $exception : Throwable
- $templatePath : string
Return values
string —The path and filename of the view file to use
fail()
Used for generic failures that no custom methods exist for.
protected
fail(array<string|int, mixed>|string $messages[, int $status = 400 ][, string|null $code = null ][, string $customMessage = '' ]) : ResponseInterface
Parameters
- $messages : array<string|int, mixed>|string
- $status : int = 400
-
HTTP status code
- $code : string|null = null
-
Custom, API-specific, error code
- $customMessage : string = ''
Return values
ResponseInterfacefailForbidden()
Used when access is always denied to this resource and no amount of trying again will help.
protected
failForbidden([string $description = 'Forbidden' ][, string|null $code = null ][, string $message = '' ]) : ResponseInterface
Parameters
- $description : string = 'Forbidden'
- $code : string|null = null
- $message : string = ''
Return values
ResponseInterfacefailNotFound()
Used when a specified resource cannot be found.
protected
failNotFound([string $description = 'Not Found' ][, string|null $code = null ][, string $message = '' ]) : ResponseInterface
Parameters
- $description : string = 'Not Found'
- $code : string|null = null
- $message : string = ''
Return values
ResponseInterfacefailResourceExists()
Use when trying to create a new resource and it already exists.
protected
failResourceExists([string $description = 'Conflict' ][, string|null $code = null ][, string $message = '' ]) : ResponseInterface
Parameters
- $description : string = 'Conflict'
- $code : string|null = null
- $message : string = ''
Return values
ResponseInterfacefailResourceGone()
Use when a resource was previously deleted. This is different than Not Found, because here we know the data previously existed, but is now gone, where Not Found means we simply cannot find any information about it.
protected
failResourceGone([string $description = 'Gone' ][, string|null $code = null ][, string $message = '' ]) : ResponseInterface
Parameters
- $description : string = 'Gone'
- $code : string|null = null
- $message : string = ''
Return values
ResponseInterfacefailServerError()
Used when there is a server error.
protected
failServerError([string $description = 'Internal Server Error' ][, string|null $code = null ][, string $message = '' ]) : ResponseInterface
Parameters
- $description : string = 'Internal Server Error'
-
The error message to show the user.
- $code : string|null = null
-
A custom, API-specific, error code.
- $message : string = ''
-
A custom "reason" message to return.
Return values
ResponseInterfacefailTooManyRequests()
Used when the user has made too many requests for the resource recently.
protected
failTooManyRequests([string $description = 'Too Many Requests' ][, string|null $code = null ][, string $message = '' ]) : ResponseInterface
Parameters
- $description : string = 'Too Many Requests'
- $code : string|null = null
- $message : string = ''
Return values
ResponseInterfacefailUnauthorized()
Used when the client is either didn't send authorization information, or had bad authorization credentials. User is encouraged to try again with the proper information.
protected
failUnauthorized([string $description = 'Unauthorized' ][, string|null $code = null ][, string $message = '' ]) : ResponseInterface
Parameters
- $description : string = 'Unauthorized'
- $code : string|null = null
- $message : string = ''
Return values
ResponseInterfacefailValidationError()
Used when the data provided by the client cannot be validated.
protected
failValidationError([string $description = 'Bad Request' ][, string|null $code = null ][, string $message = '' ]) : ResponseInterface
Use failValidationErrors instead
Parameters
- $description : string = 'Bad Request'
- $code : string|null = null
- $message : string = ''
Return values
ResponseInterfacefailValidationErrors()
Used when the data provided by the client cannot be validated on one or more fields.
protected
failValidationErrors(array<int, string>|string $errors[, string|null $code = null ][, string $message = '' ]) : ResponseInterface
Parameters
- $errors : array<int, string>|string
- $code : string|null = null
- $message : string = ''
Return values
ResponseInterfaceformat()
Handles formatting a response. Currently, makes some heavy assumptions and needs updating! :)
protected
format([array<string|int, mixed>|string|null $data = null ]) : string|null
Parameters
- $data : array<string|int, mixed>|string|null = null
Return values
string|nullmaskSensitiveData()
Mask sensitive data in the trace.
protected
maskSensitiveData(array<string|int, mixed> $trace, array<string|int, mixed> $keysToMask[, string $path = '' ]) : array<string|int, mixed>
No longer used. Moved to BaseExceptionHandler.
Parameters
- $trace : array<string|int, mixed>
- $keysToMask : array<string|int, mixed>
- $path : string = ''
Return values
array<string|int, mixed>render()
Given an exception and status code will display the error to the client.
protected
render(Throwable $exception, int $statusCode) : void
No longer used. Moved to BaseExceptionHandler.
Parameters
- $exception : Throwable
- $statusCode : int
respond()
Provides a single, simple method to return an API response, formatted to match the requested format, with proper content-type and status code.
protected
respond([array<string|int, mixed>|string|null $data = null ][, int|null $status = null ][, string $message = '' ]) : ResponseInterface
Parameters
- $data : array<string|int, mixed>|string|null = null
- $status : int|null = null
- $message : string = ''
Return values
ResponseInterfacerespondCreated()
Used after successfully creating a new resource.
protected
respondCreated([array<string|int, mixed>|string|null $data = null ][, string $message = '' ]) : ResponseInterface
Parameters
- $data : array<string|int, mixed>|string|null = null
- $message : string = ''
Return values
ResponseInterfacerespondDeleted()
Used after a resource has been successfully deleted.
protected
respondDeleted([array<string|int, mixed>|string|null $data = null ][, string $message = '' ]) : ResponseInterface
Parameters
- $data : array<string|int, mixed>|string|null = null
- $message : string = ''
Return values
ResponseInterfacerespondNoContent()
Used after a command has been successfully executed but there is no meaningful reply to send back to the client.
protected
respondNoContent([string $message = 'No Content' ]) : ResponseInterface
Parameters
- $message : string = 'No Content'
Return values
ResponseInterfacerespondUpdated()
Used after a resource has been successfully updated.
protected
respondUpdated([array<string|int, mixed>|string|null $data = null ][, string $message = '' ]) : ResponseInterface
Parameters
- $data : array<string|int, mixed>|string|null = null
- $message : string = ''
Return values
ResponseInterfacesetResponseFormat()
Sets the format the response should be in.
protected
setResponseFormat([string|null $format = null ]) : $this
Parameters
- $format : string|null = null
-
Response format
Tags
Return values
$thishandleDeprecationError()
private
handleDeprecationError(string $message[, string|null $file = null ][, int|null $line = null ]) : true
Parameters
- $message : string
- $file : string|null = null
- $line : int|null = null
Return values
trueisDeprecationError()
private
isDeprecationError(int $error) : bool
Parameters
- $error : int
Return values
boolmaskData()
private
maskData(array<string|int, mixed>|object $args, array<string|int, mixed> $keysToMask[, string $path = '' ]) : array<string|int, mixed>|object
No longer used. Moved to BaseExceptionHandler.
Parameters
- $args : array<string|int, mixed>|object
- $keysToMask : array<string|int, mixed>
- $path : string = ''
Return values
array<string|int, mixed>|objectrenderBacktrace()
private
static renderBacktrace(array<string|int, mixed> $backtrace) : string
Parameters
- $backtrace : array<string|int, mixed>