Error
Error
Abstract controller action class.
- Full name:
\Hazaar\Controller\Error
- Parent class:
\Hazaar\Controller\Diagnostic
Properties
type
protected int $type
errno
protected int|string $errno
errstr
protected string $errstr
errfile
protected string $errfile
errline
protected int $errline
errtype
protected string $errtype
errclass
protected string $errclass
callstack
protected array $callstack
shortMessage
protected string $shortMessage
code
The HTTP error code to throw. By default it is 500 Internal Server Error.
protected int $code
status
protected string $status
responseType
protected string $responseType
status_codes
private static array<int,string> $status_codes
- This property is static.
Methods
__construct
Base controller constructor.
public __construct(mixed $name = 'Error'): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$name | mixed | The name of the controller. This is the name used when generating URLs. |
getStatusMessage
Retrieves the status message corresponding to a given status code.
public getStatusMessage(null|int $code = null): null|string
If no status code is provided, it uses the instance's current status code.
Parameters:
Parameter | Type | Description |
---|---|---|
$code | null|int | The status code to get the message for. If null, the instance's current status code is used. |
Return Value:
the status message corresponding to the provided status code, or null if the status code is not found
setError
Sets the error details based on the provided arguments.
public setError(): void
This method can handle different types of errors:
- If the first argument is an instance of \Throwable, it extracts the error details from the exception.
- If the first argument is an array, it assumes it's a shutdown error and extracts the details accordingly.
- Otherwise, it assumes it's a standard error and extracts the details from the provided arguments.
getErrorTypeString
Returns a string representation of the given error type.
public static getErrorTypeString(int $type): string
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$type | int | The error type constant (e.g., E_ERROR, E_WARNING). |
Return Value:
the string representation of the error type
getErrorMessage
Retrieves the error message with details about the error.
public getErrorMessage(): string
This method constructs a string that includes the error message, the line number where the error occurred, and the file in which the error was found.
Return Value:
the detailed error message
getMessage
Retrieves the error message.
public getMessage(): string
Return Value:
the error message
getTrace
Get the error code.
public getTrace(): array<int,string>
cleanOutputBuffer
Cleans the output buffer by ending all active output buffering levels.
public cleanOutputBuffer(): void
This method iterates through all active output buffering levels and ends them using ob_end_clean()
. This ensures that any buffered output is discarded and the output buffer is completely cleaned.
runner
Outputs an error message and terminates the script.
public runner(): void
This method prints a formatted error message that includes the error number, the line number where the error occurred, the file in which the error occurred, and the error string. After printing the error message, the script is terminated with the error number as the exit status.
json
Generates a JSON response containing error details.
public json(): \Hazaar\Controller\Response\JSON
This method constructs an error array with the following structure:
- 'ok': A boolean indicating the success status (always false).
- 'timestamp': The current timestamp.
- 'error': An array containing error details:
- 'type': The error number.
- 'status': The HTTP status code.
- 'str': The error message.
- 'class' (optional): The class where the error occurred (if display_errors is enabled).
- 'line' (optional): The line number where the error occurred (if display_errors is enabled).
- 'file' (optional): The file where the error occurred (if display_errors is enabled).
- 'trace' (optional): The debug backtrace (if display_errors is enabled).
Return Value:
the JSON response containing the error details
xmlrpc
Generates an XML-RPC fault response.
public xmlrpc(): \Hazaar\Controller\Response\XML
This method constructs an XML structure representing an XML-RPC fault response. The response includes details about the error class, error code, status, error message, file, and line number where the error occurred.
Return Value:
the XML-RPC fault response
text
Generates a detailed text response for an exception.
public text(): \Hazaar\Controller\Response\Text
This method constructs a formatted string containing information about the exception, including the environment, timestamp, class, error number, file, line, message, and backtrace. The backtrace includes details about each call in the stack, such as the file, line, class, and function.
Return Value:
a text response containing the formatted exception details
html
Generates an HTML response for an error.
public html(): \Hazaar\Controller\Response\HTML
This method creates a new Layout instance for the error view and populates it with various error details such as environment, error type, error code, error message, file, line, class, type, short message, trace, status code, and execution time. It then renders the view and returns it as an HTML response.
Return Value:
the rendered HTML response containing the error details
loadStatusCodes
Loads the status codes from the HTTP_Status.dat file and returns an array of status codes.
private loadStatusCodes(): array<int,string>
Return Value:
the array of status codes, where the key is the status code and the value is the corresponding status message
Inherited methods
__construct
Base controller constructor.
public __construct(?string $name = null): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$name | ?string | The name of the controller. This is the name used when generating URLs. |
__toString
Convert the controller object into a string.
public __toString(): string
__get
Get the specified helper object.
public __get(string $helper): ?\Hazaar\Controller\Helper
Parameters:
Parameter | Type | Description |
---|---|---|
$helper | string | the name of the helper |
initialize
Initializes the controller with the given request and determines the response type.
public initialize(null|\Hazaar\Application\Request $request = null): null|\Hazaar\Controller\Response
This method overrides the parent initialize method to set the response type based on various conditions such as environment variables, the PHP SAPI, and request headers.
Parameters:
Parameter | Type | Description |
---|---|---|
$request | null|\Hazaar\Application\Request | the request object, or null if not available |
Return Value:
the response object, or null if not available
run
Executes the diagnostic run process.
final public run(null|\Hazaar\Application\Route $route = null): \Hazaar\Controller\Response
This method determines the appropriate response type based on the presence of a response type method or the existence of a 'run' method. If neither is found, it defaults to an HTML response. The response is then wrapped in the appropriate Response object if it is not already an instance of Response.
- This method is final.
Parameters:
Parameter | Type | Description |
---|---|---|
$route | null|\Hazaar\Application\Route | the route object, which may be null |
Return Value:
the response object, which can be of type JSON or HTML
runAction
Executes the specified action and returns the response.
public runAction(string $actionName, array $actionArgs = [], bool $namedActionArgs = false): \Hazaar\Controller\Response
Parameters:
Parameter | Type | Description |
---|---|---|
$actionName | string | the name of the action to execute |
$actionArgs | array | the arguments to pass to the action |
$namedActionArgs | bool | whether the action arguments are named |
Return Value:
the response generated by the action
Throws:
if the response is invalid
shutdown
final public shutdown(\Hazaar\Controller\Response $response): void
- This method is final.
Parameters:
Parameter | Type | Description |
---|---|---|
$response | \Hazaar\Controller\Response |
getName
Get the name of the controller.
public getName(): string
setStatus
Set the default return status code.
public setStatus(?int $code = null): void
Parameters:
Parameter | Type | Description |
---|---|---|
$code | ?int |
getStatus
Get the status code of the controller.
public getStatus(): int
getBasePath
Get the base path of the controller.
public getBasePath(): string
setBasePath
Set the base path for the controller.
public setBasePath(string $path): void
Parameters:
Parameter | Type | Description |
---|---|---|
$path | string |
redirect
Generate a redirect response to redirect the browser.
public redirect(string $location, bool $saveURI = false): \Hazaar\Controller\Response\HTTP\Redirect
It's quite common to redirect the user to an alternative URL. This may be to forward the request to another website, forward them to an authentication page or even just remove processed request parameters from the URL to neaten the URL up.
Parameters:
Parameter | Type | Description |
---|---|---|
$location | string | The URI you want to redirect to |
$saveURI | bool | Optionally save the URI so we can redirect back. See: Hazaar\Application::redirectBack() |
redirectBack
Redirect back to a URI saved during redirection.
public redirectBack(?string $altURL = null): false|\Hazaar\Controller\Response\HTTP\Redirect
This mechanism is used with the $saveURI parameter of Hazaar\Application::redirect()
so save the current URI into the session so that once we're done processing the request somewhere else we can come back to where we were. This is useful for when a user requests a page but isn't authenticated, we can redirect them to a login page and then that page can call this Hazaar\Application::redirectBack()
method to redirect the user back to the page they were originally looking for.
Parameters:
Parameter | Type | Description |
---|---|---|
$altURL | ?string |
getURL
Generate a URL relative to the controller.
public getURL(): \Hazaar\Application\URL
This is the controller relative method for generating URLs in your application. URLs generated from here are relative to the controller. For URLs that are relative to the current application see Hazaar\Application::url()
.
Parameters are dynamic and depend on what you are trying to generate.
For examples see: Generating URLs
addHelper
Add a helper to the controller.
public addHelper((\Hazaar\Controller\Helper|string)[]|\Hazaar\Controller\Helper|string $helper, array $args = [], string $alias = null): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$helper | (\Hazaar\Controller\Helper|string)[]|\Hazaar\Controller\Helper|string | The helper to add to the controller. This can be a helper object, a helper class name or an array of helpers. |
$args | array | an array of arguments to pass to the helper constructor |
$alias | string | The alias to use for the helper. If not provided, the helper name will be used. |
hasHelper
Checks if a helper exists.
public hasHelper(string $helper): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$helper | string | the name of the helper to check |
cacheAction
public cacheAction(string $actionName, int $timeout = 60, bool $private = false): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$actionName | string | |
$timeout | int | |
$private | bool |
findHelper
Find a helper class by name.
private findHelper(string $name): ?string
This method searches for view helper classes based on the given name. The search order is important because it allows apps to override built-in helpers.
Parameters:
Parameter | Type | Description |
---|---|---|
$name | string | the name of the helper class to find |
getCacheKey
Get the cache key for the current action.
private getCacheKey(string $controller, string $action, array $actionArgs = null, null|string& $cacheName = null): false|string
Parameters:
Parameter | Type | Description |
---|---|---|
$controller | string | the controller name |
$action | string | the action name |
$actionArgs | array | the action arguments |
$cacheName | null|string | the cache name |
cacheResponse
private cacheResponse(\Hazaar\Application\Route $route, \Hazaar\Controller\Response $response): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$route | \Hazaar\Application\Route | |
$response | \Hazaar\Controller\Response |
getCachedResponse
private getCachedResponse(\Hazaar\Application\Route $route): ?\Hazaar\Controller\Response
Parameters:
Parameter | Type | Description |
---|---|---|
$route | \Hazaar\Application\Route |
stream
Sends a stream response to the client.
public stream(array|string $value): bool
This method sends a stream response to the client, allowing the client to download the response as a file. It sets the necessary headers for the response and flushes the output buffer to ensure the response is sent immediately.
Parameters:
Parameter | Type | Description |
---|---|---|
$value | array|string | The value to be streamed. If an array is provided, it will be converted to a JSON string before streaming. |
Return Value:
returns true if the stream response was successfully sent, false otherwise
init
protected init(\Hazaar\Application\Request $request): void
Parameters:
Parameter | Type | Description |
---|---|---|
$request | \Hazaar\Application\Request |
initResponse
protected initResponse(\Hazaar\Application\Request $request): ?\Hazaar\Controller\Response
Parameters:
Parameter | Type | Description |
---|---|---|
$request | \Hazaar\Application\Request |
registerMethod
Registers a new method with the given name and callback.
public registerMethod(string $name, callable $callback): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$name | string | the name of the method to register |
$callback | callable | the callback function to associate with the method |
Return Value:
returns true if the method was successfully registered
Throws:
if a method with the given name already exists
view
Loads a view.
protected view(string $view): void
Parameters:
Parameter | Type | Description |
---|---|---|
$view | string | the name of the view to load |
layout
Sets the layout view for the current controller.
protected layout(string $view): void
This method sets the layout view that will be used to render the response.
Parameters:
Parameter | Type | Description |
---|---|---|
$view | string | the name of the layout view to be used |
setCaller
public setCaller(array $caller): void
Parameters:
Parameter | Type | Description |
---|---|---|
$caller | array |
json
Returns a JSON response with an error message.
public json(): \Hazaar\Controller\Response\JSON
Return Value:
the JSON response containing the error message
xmlrpc
Generates an XML-RPC response.
public xmlrpc(): \Hazaar\Controller\Response\XML
This method creates a SimpleXMLElement with a root element xml
and adds a child element data
with the content 'NO CONTENT'. It then returns this XML structure wrapped in a Response\XML object.
Return Value:
the XML response object containing the generated XML structure
html
Generates an HTML response with a predefined message.
public html(): \Hazaar\Controller\Response\HTML
Return Value:
the HTML response containing 'NO CONTENT' and the specified status code
text
Returns a text response with the content 'NO CONTENT' and the specified status code.
public text(): \Hazaar\Controller\Response\Text
Return Value:
the text response object
hazaar
Outputs a diagnostic dump and terminates the script.
public hazaar(): void
This method sets the HTTP response code to the value of the $code
property, outputs a diagnostic message, dumps a placeholder string 'NO CONTENT', prints a backtrace, and then exits the script.
Automatically generated on 2024-11-14