Different strategies. and throw errors directly in the application code within the controller. Next you'll learn how to use exception filters to handle unhandled exceptions throughout your application. In this tutorial you will use the one built in Chapter 1. You do not need to complete Chapter 2 to follow this tutorial. A development environment is required to follow this tutorial. Installed and. If you are using make sure your version is or higher. You can check your version by running in terminal. Optionally install extensions. The extension adds some really nice syntax highlighting. You can optionally access a terminal such as and to run the commands provided in this series.
Requesting an article that does not exist returns Requesting an article that does not exist returns an exception filter that extends classes in the core package. This class provides the default imple photo editing servies mentation of methods that return internal server error responses to the user. You can learn more about this in the documentation. You added a statement to log error messages to the console. This is useful for debugging purposes. Will be thrown for many different types of errors. So you need to figure out how to extract the error code from the exception. The exception has an attribute containing the error code. You can find a list of error codes in the Error Message Reference. The error code you are looking for is .
That it occurs due to a unique constraint violation. Now you will update the method to throw a response when this error occurs. You will also provide a custom error message to the user. Update your exception filter implementation like this Copy Here you are accessing the underlying frame object and modifying the response directly. The default is the framework used under the hood. For any exception code except , you will send the default internal server error response. Note For production applications please be careful not to reveal any sensitive information to the user in error messages. Applying the exception filter to your application Now for it to take effect you need to apply it to a certain scope. The scope of an exception filter can be a .