Customizing error reports

In order to quickly reproduce and fix errors, it is often helpful to send additional application-specific diagnostic data to your BugSnag dashboard to provide a rich context that is also available for searching and filtering.

You can also amend the events captured by BugSnag to adjust the information shown on the dashboard and even choose not to send the event at all.

This documentation is for version 2 of the BugSnag Flutter library. We recommend upgrading to the latest release using our Upgrade guide. Documentation for the current release can be found here.

Updating events using callbacks

If you’d like to add diagnostic data to reports, or adjust event data conditionally, you can register a callback to be run before an error is delivered to your BugSnag dashboard using a callback:

void main() => bugsnag.start(
  runApp: () => runApp(const MyFlutterApp()),
  onError: [
    (event) {
      event.addMetadata('account', const { 'name': 'Acme Co.' });
      event.addMetadata('account', const { 'paying_customer': true });

      // Return `false` if you'd like to stop this error being reported
      return true;
    },
  ],
);

Adding and removing callbacks

We recommend adding callbacks through the addOnError configuration option to ensure that it is registered as soon as BugSnag starts. However, the following methods are provided to allow callbacks to be added and removed whilst the application is running:

bool cb(BugsnagEvent error) {
  return true;
}

bugsnag.addOnError(cb);
bugsnag.removeOnError(cb);

Discarding events

If you want to prevent an event from being sent to BugSnag, you can return false within a callback. This would allow for users to opt out of sending error reports, for example:

void main() => bugsnag.start(
  runApp: () => runApp(const MyFlutterApp()),
  onError: [
    (event) => userHasOptedOut(),
  ],
);

Global metadata

If you have metadata that is applicable to all captured events, it can be set globally on the BugSnag client and will be set on all subsequently generated events. To ensure that events contain metadata as soon as BugSnag starts capturing events, we recommend that you add metadata in your initial BugSnag configuration if the data is available at startup.

The top-level key in the map is displayed as a tab in the BugSnag dashboard:

void main() => bugsnag.start(
  runApp: () => runApp(const MyFlutterApp()),
  metadata: const {
    'company': {
      'name': 'Acme Co.',
      'country': 'uk',
    },
  },
);

Metadata can also be managed whilst your application is running using addMetadata and clearMetadata on the BugSnag client:

bugsnag.addMetadata('company', const {
  'name': 'Acme Co.',
  'country': 'uk',
});
// ...
bugsnag.clearMetadata('company');

Adding user data

Information about the user affected by errors can be added to events sent to your BugSnag dashboard by setting an initial user ID, email and name in your initial BugSnag configuration when BugSnag starts:

void main() => bugsnag.start(
  runApp: () => runApp(const MyFlutterApp()),
  user: BugsnagUser(id: '3', email: 'bugs.nag@bugsnag.com', name: 'Bugs Nag'),
);

When the Persist User configuration option configuration option is set to true, this user information will also be set in subsequent application launches.

If the user changes whilst the application is running, for example if a user signs in, you can update the user data on the BugSnag client for all subsequent events:

bugsnag.setUser(id: '3', email: 'bugs.nag@bugsnag.com', name: 'Bugs Nag');

You can use metadata to add additional user information to the “user” section.

Setting context

BugSnag uses the concept of “contexts” to help display and group your errors. The context represents what was happening in your application at the time an error occurs and is given high visual prominence in the dashboard.

By default this is set this to the name of the currently executing function. If you would like to set this value manually, you can set an initial value in your initial BugSnag configuration when BugSnag starts:

void main() => bugsnag.start(
  runApp: () => runApp(const MyFlutterApp()),
  context: 'ctx-id-1234',
);

The context can then be amended through the BugSnag client as/when it changes for all subsequent events:

bugsnag.setContext('User settings screen');

Alternatively, you can use the BugsnagNavigatorObserver to set the event context by observing your app’s route changes.

The BugsnagEvent class

An BugsnagEvent object represents an error captured by BugSnag and is available as a parameter on an event callback. The following properties and methods are available on an BugsnagEvent for you to query and update the captured data.

API Key

The API key used for events sent to BugSnag. Even though the API key is set when BugSnag is initialized, you may choose to send certain events to a different BugSnag project:

event.apiKey = 'YOUR-API-KEY';

App

Information set by the notifier about your app can be found in the event.app:

property type description
binaryArch String The architecture of the running application binary
buildUuid String The unique identifier for the build of the application (Android only)
bundleVersion String The bundle version used by the application (iOS only)
dsymUUIDs List<String> Unique identifier for the debug symbols file corresponding to the application
duration Number The number of milliseconds the application was running before the event occurred
durationInForeground Number The number of milliseconds the application was running in the foreground before the event occurred
id String The package name of the application
inForeground Boolean Whether the application was in the foreground when the event occurred
isLaunching Boolean Whether the application was still launching when the event occurred
releaseStage String The release stage set in Configuration
type String The application type set in Configuration
version String The version of the application set in Configuration
versionCode String The version code of the application set in Configuration (Android only)

These values can be accessed and amended if necessary:

event.app.type = 'worker';

A list of breadcrumbs leading up to the event can be found in event.breadcrumbs.

property type description
message String The description of the breadcrumb
metadata Map<String,Object> Diagnostic data relating to the breadcrumb
timestamp DateTime The timestamp that the breadcrumb was left
type BugsnagBreadcrumbType The type of breadcrumb left

These values can be accessed and amended if necessary:

event.breadcrumbs[0].message = 'Left homepage';

Context

The context represents what was happening in your application at the time an error occurs.

event.context = 'User settings screen';

See Setting context for more information.

Device

Information set by the notifier about the device on which the event occurred can be found in event.device:

property type description
cpuAbi List<String> The Application Binary Interface used (Android only)
freeDisk Number The number of free bytes of storage available on the device
freeMemory Number The available RAM on the device (in bytes)
id String A UUID generated by BugSnag and used for the individual application on a device
jailbroken String Whether the device has been jailbroken
locale String The IETF language tag of the locale used
manufacturer String The manufacturer of the device used
model String The model name of the device used
modelNumber String The model number of the device used
orientation String The orientation of the device when the event occurred: either portrait or landscape
osName String The name of the operating system running on the device used
osVersion String The version of the operating system running on the device used
runtimeVersions Map<String,String> A collection of names and their versions of the primary languages, frameworks or runtimes that the application is running on
time String The timestamp on the device when the event occurred
totalMemory Number The total RAM size of the device (in bytes)

These values can be accessed and amended if necessary:

event.device.locale = 'de-DE';

Errors

event.errors is an array of one or more BugsnagError objects. The first item in the list represents the thrown object. Each subsequent item represents the error that caused the preceding one.

An BugsnagError object contains the following information:

property type description
errorClass String The fully-qualified class name of the error generating the event
errorMessage String The message string from the error generating the event
stacktrace List<BugsnagStackframe> A representation of the stacktrace
type BugsnagErrorType The type of error based on the originating platform

These values can be accessed and amended if necessary:

event.errors[0].errorClass = 'MyError';

In an BugsnagError object, the stacktrace is a list of the following BugsnagStackframe objects:

property type description
codeIdentifier String A unique build identifier used to associate the stackframe with an uploaded symbols file (see the symbolication guide for more information
file String The location of the source file
frameAddress String The stack frame address (iOS only)
inProject Boolean Whether the package is considered to be in your project for the purposes of grouping and readability on the BugSnag dashboard.
isLr Boolean Whether the frame was within the link register (iOS only)
isPc Boolean Whether the frame was within the program counter (iOS only)
lineNumber Number The line number within the source file this stackframe refers to
machoFile String The Mach-O file used by the stackframe (iOS only)
machoLoadAddress Number The load address of the Mach-O file (iOS only)
machoUuid String A UUID identifying the Mach-O file used by the stackframe (iOS only)
machoVmAddress Number The VM address of the Mach-O file (iOS only)
method String The name of the method that was being executed
symbolAddress Number The address of the stackframe symbol (iOS only)

It is therefore possible to modify the stacktrace that is associated with the BugsnagEvent object in the following way:

event.errors[0].stacktrace.removeAt(0);

Feature Flags

Use the following operations to amend the feature flags to be sent with the event.

Declare a single feature flag or experiment with variant as an optional second parameter.

event.addFeature('Checkout button color', 'Blue');
event.addFeature('New checkout flow');

Declare multiple feature flags or experiments.

event.addFeatureFlags(const [
  BugsnagFeatureFlag('Checkout button color', 'Blue'),
  BugsnagFeatureFlag('Special offer', 'Free Coffee'),
  BugsnagFeatureFlag('New checkout flow'),
]);

Remove a single feature flag or experiment.

event.clearFeatureFlag('Special offer');

Remove all feature flags and experiments.

event.clearFeatureFlags();

For more information, see Feature flags & experiments.

Grouping Hash

The grouping hash of the event to override the default grouping on the dashboard. All events with the same grouping hash will be grouped together into one error. This is an advanced usage of the library and mis-using it will cause your events not to group properly in your dashboard.

As the name implies, this option accepts a hash of sorts.

event.groupingHash = event.errors[0].errorClass;

Metadata

Add and remove metadata from a specified section, which is shown as a tab on the BugSnag dashboard.

Data can be added key-by-key with a value that is a primitive type or a collection such as a map, set or array:

event.addMetadata('account', const {'name': 'Acme Co.'});
event.addMetadata('account', const {'paying_customer': true});
event.addMetadata('account', const {'roles': ['basic', 'admin']});

Alternatively a whole map of key-value pairs can be added for a section:

event.addMetadata('basket', const {
  'delivery': 'express',
  'sale': 'spring',
});

Metadata set on the event will be combined with global metadata set on the BugSnag client, with properties on the BugsnagEvent taking precedence.

To remove all the data from the specified section or from a key in the section:

event.clearMetadata('account');

You can also obtain a map of data in the specified section and optionally key:

event.clearMetadata('account', 'name');

Severity

The severity of the event. By default, unhandled errors will be BugsnagSeverity.error and handled errors sent with notify will be Severity.warning.

event.severity = BugsnagSeverity.warning;

Threads

If thread state is being captured along with the event, event.threads will contain a list of Thread objects as follows:

property type description
id Number The unique ID of the thread
isErrorReportingThread Boolean Whether the thread was the thread that caused the event
name String The name of the thread
state String The state of the thread when the event occurred
type BugsnagErrorType The type of thread based on the originating platform

These values can be accessed and amended if necessary:

event.threads[0].name = 'New name';

Unhandled

Whether the error was detected automatically by BugSnag (true), or reported manually via notify (false).

event.unhandled = true;

Changing the unhandled flag for an event will affect how it contributes to your application’s stability score.

User

The current user information.

event.setUser(id: '3', email: 'bugs.nag@bugsnag.com', name: 'Bugs Nag');

null can be used as a parameter value to clear the user attribute.