Check out our guides and API reference documentation on how to use Rollbar to monitor, debug, and triage errors.

Guides & SDKs    API Reference

Enable Code Context in a Traceback

Rollbar can show additional lines of context for each entry in a traceback, saving your trouble of jumping to your source code to figure out where exactly an exception occurred.

Expanded stack frame with 5 lines before and after

This guide will walk you through the necessary steps to enable this feature.

Enable Git Integration

Rollbar relies on retrieving data from your Git repository in order to show context around each stack frame. Each Rollbar project should be connected to a Git repository (typically a 1:1 mapping). To set up this integration, please check out one of the following guides:

Set server.root and code_version in error payloads

In order for Rollbar to retrieve data from your Git repository, it needs to know

  • What part of the stack frame path includes code from your source repository?
  • Optionally, what is the SHA of the version of the code where the exception occurred?

The first is specified by including a server.root property in the JSON payload which is sent back to Rollbar:

  "server": {
    "root": "/home/deploy/www/mox", 
    "branch": "master"

In this case, we tell Rollbar that the everything after /home/deploy/www/mox is our code. This should be an exact match to the path in the filename. In the screenshot at the top of the page, that corresponds to mox/views/

By default, Rollbar will retrieve code from the lastest version on your master branch, but you can use the server.branch property to tell Rollbar that the code lives in a different branch, or you can specify a different default branch for the project in Settings → Source Control.

Since the version where an error occurred may be one other than your latest, we recommend including the Git SHA of the deployed version in your JSON payload:

"code_version": "aaaaaabbbbbbccccccddddddeeeeeeffffff"

With this configuration set properly, Rollbar knows to retrieve the code context from
aaaaaabbbbbbccccccddddddeeeeeeffffff/mox/views/ within your repository.

Setting code_version correctly will also ensure that the Git link in each stack frame points to the correct version of your code:

The Github link in each stack frame goes to the line of code in the version in which the exception occurred.

You should now be able to see a stacktrace like this:

A python stacktrace with Github integration shows code context in each frame.

Troubleshooting Tips

I can't expand the stack frames in my project

This is likely because:

  • Git integration is not properly configured in your project. Rollbar needs read to be able to read from your repository in order to grab context. You can tell if Rollbar is properly connected by checking for the name of a Github user whose credentials are used to access the repository. Also, make sure you've entered the correct repo name and default branch:
  • Rollbar does not have access to your SCM repo - Check your SCM settings and make sure Rollbar has access. For example, in Github, check Settings → Applications
  • or your JSON payloads don't contain server.root or you've set up an incorrect value for server.root.

I can expand the stack frames, but I see one of the following error messages:

  • Update your credentials - Check user and/or project Source control credentials in User Settings and Project - Settings - Source Control Manager

User Settings - connected accounts

Project Settings - Integrations

  • Bad Request - Verify Source Control manager credentials are correct and you've set up the correct repo name in the Source control settings (as seen in the Project Settings - Integrations image above)

  • API Unavailable - Something is wrong with the external API (GitHub, GitLab or BitBucket). Check the direct link to the source code file.

  • Could not retrieve data from repository - Your repository is not accessible to Rollbar. Please check your repo settings in your Source control manager's settings.

  • Could not match line with provided code version or No data in file - We are likely gathering file contents from an outdated code version. Please send code_version in your item payload for the most accurate results.

If you're still experiencing issues setting up code context for your projects, please contact support!

Updated about a year ago

Enable Code Context in a Traceback

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.