Posts

Showing posts with the label Exception

PHP SPL Exceptions Use Cases

An exception is used to immediately terminate the operation due to incorrect use of the code or incorrect data transferred for processing, or the inability to transfer data to another system. LogicException (and inherited) is thrown before the operation is executed, due to incorrect code usage. For example, an unknown function is called, a variable is passed with the wrong type, value, or wrong string length. RuntimeException (and inherited) is thrown during the execution of an operation, due to invalid data being passed or the inability to pass data to another system. For example, while processing the configuration file, it turned out that the value of the parameter was incorrect or the connection to the database was not established. Throwing LogicException and RuntimeException results in a HTTP 500 (Internal Server Error) server response because the error is on the server side. SPL Exceptions Class Tree LogicException BadFunctionCallException ...

Handling Exceptions in a Symfony Application

There are two main types of application errors: The request can be processed, but the data is not valid The request could not be processed due to a broken part of the application or system If the requested URL is not found because it is not defined in the API specification, then an HTTP 404 (Not Found) response is returned. The request can be processed, but the data is not valid refers to a server response with an HTTP code 400 (Bad Request) detailing exactly which data sent is invalid. Errors of this kind are foreseen in advance and their handling is a normal part of the application. For example, after submitting the news form data to the server, one of the fields did not pass validation and the server returned an HTTP 400 (Bad Request) response with error details to be shown to the user. This type of error can not be logged, because error details are returned immediately with the server response and no server-side fixes are required. This kind of error displays the sa...