How to provide appropriate HTTP code to a request in the event of an error
I. Introduction
HTTP status codes are standard responses sent by a server in response to a request made by a client. They provide information about the result of the request. When an error occurs, it is crucial to assign the correct HTTP code to help the client understand the nature of the error.
II. HTTP Code structure
HTTP codes are divided into several categories:
- 1xx : Informations (ex. 100 Continue)
- 2xx : Success (ex. 200 OK)
- 3xx : Redirection (ex. 301 Moved Permanently)
- 4xx : Customer errors (ex. 404 Not Found)
- 5xx : Server errors (ex. 500 Internal Server Error)
III. Common error codes
1. Customer errors (4xx)
400 Bad Request
Description : The request is ill-formed or invalid.
Use : Use this code when the data sent by the client does not respect the expected format.
401 Unauthorized
Description : Authentication required and failed or not provided.
Use : Use this code when the user attempts to access a protected resource without providing the necessary credentials.
403 Forbidden
Description : The server understands the request, but refuses to execute it.
Use : Use this code when the user does not have the necessary authorizations to access the resource.
404 Not Found
Description : The requested resource was not found on the server.
Use : Use this code when the requested URL does not correspond to any existing resource.
2. Server errors (5xx)
500 Internal Server Error
Description : A generic error has occurred on the server.
Use : Use this code when the server encounters an unexpected condition that prevents it from processing the request.
502 Bad Gateway
Description : The server has received an invalid response from an upstream server.
Use : Use this code when your server acts as a gateway or proxy and does not receive a valid response.
503 Service Unavailable
Description : The server is temporarily unable to process the request (due to overload or maintenance).
Use : Use this code when the service is temporarily unavailable.
IV. Using the errorAndDie method
Tiki's Feedback class provides a handy method for handling errors and returning the appropriate HTTP codes. Here's how to use this method:
Method : errorAndDie
/** * Redirect to a page with error feedback and Die * * @param string $message * @param int $httpCode * @param string|null $errorPage * @return never * @throws Exception */ public static function errorAndDie(string $message, int $httpCode, ?string $errorPage = null): never { $errorPage = $errorPage ?? "error.tpl"; $smarty = TikiLib::lib('smarty'); $smarty->assign('errortype', $httpCode); $smarty->assign('msg', $message); http_response_code($httpCode); $smarty->display($errorPage); die; }
Parameters
$message : The message that will be displayed to the user. Use this parameter to provide clear information about the error that has occurred.
$httpCode : The HTTP error code to be returned. This must correspond to the specific error encountered (for example, 404 for a page not found).
$errorPage : The error page to be returned if different from error.tpl. If this parameter is null, the default error.tpl page will be used.
Example of use
Here's an example of how to call this method:
if ($someErrorCondition) { Feedback::errorAndDie("The requested resource cannot be found.", 404); }
In this example, if an error condition is encountered, an appropriate error message is displayed to the user, and the HTTP code 404 is returned.
V. Conclusion
To ensure efficient communication between client and server, it is essential to assign appropriate HTTP codes when handling errors. These standard status codes play a key role in enabling the server to respond clearly and accurately to client requests. By following the guidelines presented and using the errorAndDie method, you make it easier to understand the errors encountered, contributing to a better user experience.