Symfony is a PHP model-view-controller framework used to develop web applications. The main reasons Symfony has gained fame among the developer community are its database engine compatibility, its faster development, and its robust system. These features make it easy to develop and customize web applications.

To make your web application successful, you should consider how user experience is affected by the inevitable errors that pop up in production. Customers can walk away from a purchase or churn out from a subscription when applications do not run properly. A fast response to critical problems is essential. To minimize customer impact, it is important to track and fix issues quickly.

In this tutorial, we’ll show you how to add native error handling so you can capture handled and unhandled errors. Doing so lets you present a clear message to the user as well as track the error to prioritize fixes.

Then, we’ll show you how to monitor errors using Rollbar. Rollbar is an error monitoring service that tracks errors centrally and gives you tools to better monitor and troubleshoot problems in production. We'll show a working example for Symfony 3, but Rollbar also supports Symfony 4.

Native Error Handling in Symfony 3

Starting at the most basic level, error handling can be achieved using try, catch and finally statements. These statements can be used to handle caught exceptions in Symfony components.

Symfony 3 also offers multiple approaches to handling uncaught exceptions. Below is one example by which you can handle an uncaught exception globally and report it to error reporting services like Rollbar. Below are some simple steps to create a global error handler in Symfony 3:

Step 1

Find the AppBundle in your project and create a package for a global error handler. In our case it is EventListener. You can find AppBundle under the src folder in the project directory.

Step 2

Next, create a global error handler in the EventListener package. You can handle errors here using a function named onKernelException.

This is a great way to present a better-formatted error page to the user. However, you should have a way to track how often these errors happen and a method of receiving alerts about potentially critical user experience issues. Also, you need sufficient contextual information to determine the cause of the problem so you can fix it quickly. It’s better to capture this data automatically so you don’t have to rely on complaints to your support team.

Monitoring Errors With Rollbar

Rollbar helps you monitor real-world applications, providing live error feed and instant alerts to make you aware of errors as they appear. It automatically tracks errors and provides contextual data to debug errors. No error reports or feedback from the user are required to identify these errors. Rollbar can quickly track and resolve errors. It offers information related to errors using statistics, charts, and historical graphs. Learn more about Rollbar’s features and how they can help solve problems quickly.

Below, we've created an example Symfony app that triggers an exception when the user clicks on a button. The error message is recorded on Rollbar with a stack trace where you can see the line of code that caused the error.

How to Set Up a Symfony Project on Rollbar

Below are some simple steps to add Symfony to Rollbar. You can find more details in our Symfony documentation.

Visit https://rollbar.com and sign up for an account if you haven’t done so yet. Next, create your project and select Backend from the list of notifiers. Select the client-side access token that is generated for you. You’ll need this to configure Rollbar in the steps below.

Open the command prompt in your project directory and type the following command to install the Rollbar Symfony SDK:

As an alternative to step 4, to configure Rollbar in your Symfony app, you can add the code below in the AppBundle\Controller class. Normally you can find Controller class under the \vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Controller folder of your project root directory.

When you click the "Generate an error" button it triggers the uncaught method of DefaultController where we are generating a “Creating default object from empty value: Creating default object from empty value” exception.

Viewing Errors in Rollbar

Open Rollbar to see what these errors look like in your account’s item page. The error we just generated should be called "Creating default object from empty value: Creating default object from empty value."

You can get more information about what generated the error by clicking on the item where you can see a traceback showing the exact source code file, method, and line number. This provides more context to help you find the root cause of errors.

A lot of contextual information is provided by Rollbar that will help you in troubleshooting and debugging the problems quickly. The dashboard gives you a summary of errors, showing how many times each occurred and how many people were affected.

Conclusion

Symfony is a powerful and robust PHP framework. It has unique ways of handling errors. Error monitoring is one of the most important tools for identifying and fixing errors. Rollbar helps you monitor the errors, prioritize high-impact ones, and capture more contextual data so you can fix problems faster. It provides developers with information about what happened before, during and after an error was generated.