Rollbar is Awesome

Ever wanted to see errors happening on your application? I use rollbar for this. And unless your app has over 5000 errors a month, it is free.

Installing Rollbar for Laravel is super easy. Below is the code you get when you first sign up for Rollbar.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
// installs global error and exception handlers
Rollbar::init(array('access_token' => 'YOUR_ACCESS_TOKEN_HERE'));

// Message at level 'info'
Rollbar::report_message('testing 123', 'info');

// Catch an exception and send it to Rollbar
try {
throw new Exception('test exception');
} catch (Exception $e) {
Rollbar::report_exception($e);
}

// Will also be reported by the exception handler
throw new Exception('test 2');
?>

You’ll also want to install rollbar library with composer.

1
composer require rollbar/rollbar

Next, where to place the error handling code? In Laravel 5, you’ll want to add it to app\Exceptions\Handler.php like so…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $e
* @return void
*/
public function report(Exception $e)
{
// use this rollbar for production environments
if (config('app.env') === 'production')
{
Rollbar::init(array('access_token' => 'YOUR_PROD_ACCESS_TOKEN'));

Rollbar::report_exception($e);
}

// maybe use your own DEV rollbar here?
if (config('app.env') === 'local')
{
Rollbar::init(array('access_token' => 'YOUR_DEV_ACCESS_TOKEN'));

Rollbar::report_exception($e);
}

parent::report($e);
}
}

This tool will keep track of all the errors your visitors are seeing. It doesn’t replace writing tests for the application necessarily but it does give an extra layer of comfort knowing when your application is erroring. Some times you can find and fix errors before a client has time to email you that screenshot and upload it to JIRA or Trello. Then you just say, yeah, I fixed that like 5 minutes ago dawg…* and walk off with a gangsta smile on your face.