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

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 see one of the following guides:

Set root/server.root and code_version in error payloads

NOTE: the field referenced in this section is called root in some libraries, but is referred to as server.root for this Javascript example.

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?
  • 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 configuration which is sent back to Rollbar:

payload: {
  environment: "production",
  code_version: "79b2071e3350a8779d43f203bdd698ae7f34f3e6",
  server: {
    root: "file:///Users/david/Documents/dev-stuff/js-demo/",
    branch: optional
  }
}

The server.root value should match the file path reported by the items in Rollbar. In this case, we tell Rollbar that everything after file:///Users/david/Documents/dev-stuff/js-demo/ is our code. This also matches the home directory for the github repo: in this case, https://github.com/dbw0011/js-demo/ is the home repo and it has index.html as a file in the top-level folder. This is an example of a web page being opened from a local drive, so in most cases a web app will have a URL for the path.

By default, Rollbar will retrieve code from your main 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.

1242

The "Project Root" field should be used if there are sub-folders between the home repo and the file.

Make sure to set the "Project Root" field when applicable. This field is used to point to folders within the specified repo where the code can be found.

The code version where the error occurred is also required. Add 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/index.html 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.

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 javascript stacktrace with Github integration shows code context in  each frame.

A javascript 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
1167

User Settings - connected accounts

1528

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!