How we built Rollbar integration for error reporting

Hey there,

As part of our custom support offering, we just helped a customer set their app with Rollbar, an error-reporting service that deliver you a smartphone notification whenever something goes wrong with your app. With their permission, we’re sharing how we did it with the rest of the community:


First step: Write a function to use the Rollbar API to submit a message. (You might want to put it in a Module so you can use it from lots of different places):

def send_rollbar_message(msg, level='error'):  
  payload = {
    'access_token': 'YOUR-ROLLBAR-CLIENT-TOKEN-GOES-HERE',
    'data': {
      'environment': 'production',
      'platform': 'browser',
      'body': {
        'level': level,
        'message': {
          'body': msg
        }
      }
    }
  }
  anvil.http.request('https://api.rollbar.com/api/1/item/', method='POST', json=True, data=payload)

Second step: Set a default error handler that calls this function to report errors:

def handle_error(err):
  # Oops! Hit Rollbar, tell them something bad happened
  send_rollbar_message(str(err))
  # And now show the red box again
  raise err

anvil.set_default_error_handling(handle_error)

That’s it! Now, any uncaught errors in your Anvil forms will be reported via Rollbar, so you can check the App Logs, find what went wrong, and fix it right away. And if you want to pass a non-fatal message, you can call send_rollbar_message('some string', level='message').


Do you want this kind of hands-on help with your Anvil development? Contact support@anvil.works to enquire about a support agreement – we’d be happy to help.

6 Likes

Nice. Just to say though, your link to custom error handling is wrong - should be :

https://anvil.works/doc/index.html#custom-error-handling

EDIT - now your one is right and mine is wrong! Magic things keep changing …

1 Like

Is it sufficient to call anvil.set_default_error_handling() once in the app’s startup form, or must it be called from every form?

It’s sufficient to call it once in the app’s startup form (it’s a global setting).

1 Like

FYI: the token used in the example must be a post_client_item token. You can find it in the Rollbar UI under Project Access Tokens. Wasn’t obvious to me.

And the level field should be at the same level as platform.

3 Likes