You are here:

2 ways to deal with errors in Magento 2

When running an online business, it is of crucial importance that you need to handle errors timely in order not to impact customer experience on your site. If not, there will be a high possibility that you will lose customers and sales.

In this tutorial, we will introduce two effective ways that you can monitor errors on your online-selling website that can ensure a frictionless purchasing experience for your customers. Let’s discover.

Way 1: Native exception monitoring in Magento 2

If you are running a small business on a single server, this way is suggested for you.

In Magento 2, the exception logs are put in a file by default. If you see the warning stating “there has been an error processing your request, Magento exception printing is disabled by default for security reasons”, then you need to check your server to make sure that it has permission to the log directory. To enable exception printing, in System, tap Configuration > Develop > Log Settings and finally click “Yes”.

{project root}\var\log\exception.log is where you can locate your logs. Here is an example of exception log, which you can see that each record number covers exception details like date and time of logging or log level.

Magento 2’s default error handler is normally located in {project root}\vendor\magento\framework\App\ErrorHandler.php. This is where you can extend the error handler as per whatever you want to customize the behavior.

Right under the ErrorHandler class, you can see the $msg of the handler function. Here, you can modify the log format.

Way 2: Using Rollbar to monitor errors

For those who are running enterprises, or large-sized stores with multiple servers, way 1 perhaps is not the best option since aggregating and analyzing all the logs become challenging because it’s hard to figure out what problem to solve first as well as how to find the root causes to fix errors. This is why you need to look for another way which is using Rollbar.

What is Rollbar and its offerings?

Rollbar is an error monitoring solution which detects all the errors on your site and helps you to determine the most serious one that needs handling first. As an error happens, Rollbar immediately alerts the admin. Also, it provides automatic tracking, error prioritizing, and full-stack visibility on both the website and the server.

When using this impactful tool, you will be likely to enjoy the ease of finding error causes thanks to its stack trace which shows the code line causing the error and extra related information, for example, types of users who are impacted or browsers they are using.

Setting up your Magento Project on Rollbar

Following these 6 steps to simply add Magento to Rollbar:

  1. Register with Rollbar. After that, you can create your project and choose the Backend from notifier options. In next steps, you will need the client-side access token to configure Rollbar, so select one that is generated for you.
  2. Type this code in the command prompt which is under the root directory:
  1. Find composer.json in the root project directory and run the following command:

    Now, time for generating the auto loading file. For doing that, open the command prompt and run the script below:
  1. In app/etc/env.php, configure Rollbar and substitute your access token for the post one. It’s also possible to change the environment as you want.
  1. To enable the Rollbar Magento SDK, write this code in the command prompt under the root project directory:
  1. Run this last command to upgrade Magento 2 setup:

Testing Rollbar and viewing errors

Now that you have successfully added Magento project to Rollbar, you can test how the tool works.

To do that, create a new page and click “Generate Uncaught Error” button. Once you tap on the button, it will generate an error in the code of the page you’ve created. At this time, you may want to see how the error looks like. Now, open the account’s item page in Rollbar to view errors and click on the items to get more error details.

Remember to check Rollbar’s traceback so that you will see the exact source code file, method and line number accounting for generating the errors.

Final say

Without the doubt, enhancing the customer experience on your site is a must if you want to increase sales for your store. With this in mind, ensuring that errors are tracked and monitored instantly so that it won’t affect the customer experience is what needs to be noted down in your list of strategies.

With two ways we have shown you above, error tracking and handling can’t be easier. Why hesitate? Just try it for your site and bring more fulfilling experience to customers right today.

Related articles
 

404

GET IT TOUTH

MageHit - No1 Magento Development Company

Back to Top