Catcher
Catcher is Flutter plugin which automatically catches error/exceptions and handle them. Catcher offers mutliple way to handle errors. Catcher is heavily inspired from ACRA: https://github.com/ACRA/acra Catcher is solution for developers which want to get errors informations without using Crashlytics or Sentry product. It's also great if you have your own backend where you're storing application logs, so you can manipulate it anyway you want.
Catcher supports Android, iOS and Web platforms.
Install
Add this line to your pubspec.yaml:
Then run this command:
Then add this import:
Platform support
Catcher supports Android, iOS and Web platforms. Due to issues with web framework state and missing plugins, web implementation offers less features than mobile. Here is features support table:
Feature | Android | iOS | Web |
---|---|---|---|
Application data in report | ✔️ | ✔️ | ❌ |
Console handler | ✔️ | ✔️ | ✔️ |
Discord handler | ✔️ | ✔️ | ✔️ |
Email auto handler | ✔️ | ✔️ | ❌ |
File handler | ✔️ | ✔️ | ❌ |
Http handler | ✔️ | ✔️ | ✔️ |
Sentry handler | ✔️ | ✔️ | ✔️ |
Slack handler | ✔️ | ✔️ | ❌ |
Toast handler | ✔️ | ✔️ | ✔️ |
Crashlytics handler | ✔️ | ✔️ | ❌️ |
Dialog report mode | ✔️ | ✔️ | ✔️ |
Page report mode | ✔️ | ✔️ | ✔️ |
Silent report mode | ✔️ | ✔️ | ✔️ |
Explicit report mode map | ✔️ | ✔️ | ✔️ |
Explicit report handler map | ✔️ | ✔️ | ✔️ |
Error widget | ✔️ | ✔️ | ✔️ |
Features supported may change in future, due to web framework and plugins intensive development.
Basic example
Basic example utilizes debug config with Dialog Report Mode and Console Handler and release config with Dialog Report Mode and Email Manual Handler.
To start using Catcher, you have to:
- Create Catcher configuration (you can use only debug config at start)
- Create Catcher instance and pass your root widget along with catcher configuration
- Add navigator key to MaterialApp or CupertinoApp
Here is complete example:
If you run this code you will see screen with "Generate error" button on the screen.
After clicking on it, it will generate test exception, which will be handled by Catcher. Before Catcher process exception to handler, it will
show dialog with information for user. This dialog is shown because we have used DialogReportHandler. Once user confirms action in this dialog,
report will be send to console handler which will log to console error informations.
Dialog with default confirmation message
Console handler output
Catcher usage
Adding navigator key
In order to make work Page Report Mode and Dialog Report Mode, you must include navigator key. Catcher plugin exposes key which must be included in your MaterialApp or WidgetApp:
You need to provide this key, because Catcher needs context of navigator to show dialogs/pages. There is no need to include this navigator key if you won't use Page/Dialog Report Mode.
You can also provide your own navigator key if need to. You can provide it with Catcher constructor (see below). Please check custom navigator key example to see basic example.
Catcher configuration
Catcher instance needs 1 required and 3 optional parameters.
- rootWidget (required) - instance of your root application widget
- debugConfig (optional) - config used when Catcher detects that application runs in debug mode
- releaseConfig (optional) - config used when Catcher detects that application runs in release mode
- profileConfig (optional) - config used when Catcher detects that application runs in profile mode
- enableLogger (optional) - enable/disable internal Catcher logs
- navigatorKey (optional) - provide optional navigator key from outside of Catcher
CatcherOptions parameters:
reportMode - describes how error report will be shown to user, see report modes to get more informations
handlers - list of handlers, which will process report, see handlers to get more informations
handlerTimeout - timeout in milliseconds, this parameter describes max time of handling report by handler
customParameters - map of additional parameters that will be included in report (for example user id or user name)
Report catched exception
Catcher won't process exceptions catched in try/catch block. You can send exception from try catch block to Catcher:
Localization
Catcher allows to create localizations for Report modes. To add localization support, you need setup
few things:
Add navigatorKey in your MaterialApp:
Add flutter localizations delegates and locales in your MaterialApp:
Add localizationOptions in catcherOptions:
You can add translate for given parameters:
If you want to override default english texts, just add simply localization options for "en" language.
There are build in support for languages:
- english
- chinese
- hindi
- spanish
- malay
- russian
- portuguese
- french
- polish
- italian
- korean
Complete Example:
Report modes
Report mode is the process of gathering user permission to handle error. User can accept or deny permission to handle error. There are 4 types of report mode:
Silent Report Mode
Silent Report Mode is default report mode. This report mode doesn't ask user for permission to handle crash logs. It will push logs automatically to handlers.
Notification Report Mode
Notification Report Mode has been removed because of incompatbility with firebase. Please check local_notifications_example to re-add local notificaitons to your app.
Dialog Report Mode
Dialog Report Mode shows dialog with information about error. Dialog has title, description and 2 buttons: Accept and Cancel. Once user clicks on Accept button, report will be pushed to handlers.
See localization options to change default texts.
Dialog report mode
Page Report Mode
Page Report Mode shows new page with information about error. Page has title, description, stack trace view and 2 buttons: Accept and Cancel. Once user clicks on Accept button, report will be pushed to handlers.
Page Report Mode can be configured with optional parameters:
showStackTrace (optional) - enables/disables stack trace view
See localization options to change default texts.
Page report mode
Handlers
Handlers are an last point in error processing flow. They are doing specific task with error report, for example logging report to console.
Console Handler
Console Handler is the default and basic handler. It show crash log in console. Console logger allows you to parametrize log output:
- enableApplicationParameters (optional) - display in log section with application data:
- enableDeviceParameters (optional) - display in log section with device data (it will show android/ios data):
-
enableCustomParameters (optional) - display in log section with custom parameters passed to Catcher constructor
-
enableStackTrace (optional) - display in log section with stack trace:
Email Manual Handler
Email manual handler can be used to send email manually by user. It opens default email application with prepared email.
Email Manual Handler parameters:
- recipients (required) - list of email addresses of recipients
- enableDeviceParameters (optional) - see Console Handler description
- enableStackTrace (optional) - see Console Handler description
- enableCustomParameters (optional) - see Console Handler description
- enableApplicationParameters (optional) - see Console Handler description
- sendHtml (optional) - enable/disable html email formatting
- emailTitle (optional) - set custom email title
- emailHeader (optional) - set additional email text header
- printLogs (optional) - enable/disable debug logs
Email Auto Handler
Email handler can be used to send automatically email with error reports. Email handler has multiple configuration parameters. Few of them are required, other are optional. These parameters are required:
We need to setup email smtp server, email account and recipient. Currently, only Gmail was tested and worked. You can try use other email providers, but there can be errors.
List of all parameters:
- smtpHost (required) - host address of your email, for example host for gmail is smtp.gmail.com
- smtpPort (required) - smtp port of your email, for example port for gmail is 587
- senderEmail (required) - email from which Catcher will send email (it will be sender of error emails)
- senderName (required) - name of sender email
- senderPassword (required) - password for sender email
- recipients (required) - list which contains recipient emails
- enableSSL (optional) - if your email provider supports SSL, you can enable this option
- enableDeviceParameters (optional) - please look in console handler description
- enableApplicationParameters (optional) - please look in console handler description
- enableStackTrace (optional) - please look in console handler description
- enableCustomParameters (optional) - please look in console handler description
- emailTitle (optional) - custom title of report email, if not set then title will be:
Handled Error: >> [Error name] <<
- emailHeader (optional)- custom header message before report data
- sendHtml (optional) - enable/disable html data in your email, if enabled then html will be sent and your report will look much better
- printLog (optional) - enable/disable debug logs
Example email:
Http Handler
Http Handler provides feature for sending report to external server. Data will be encoded in JSON and sent to specified server. Currently only POST request can be send. Minimal example:
All parameters list:
- requestType (required) - type of request, currently only POST is supported
- endpointUri (required) - uri address of server
- headers (optional) - map of additional headers that can be send in http request
- requestTimeout (optional) - request time in milliseconds
- printLogs (optional) - show debug logs
You can try using example backend server which handles logs. It's written in Java 8 and Spring Framework and uses material design.
You can find code of backend server here: https://github.com/jhomlala/catcher/tree/master/backend
Note: Remeber to add Internet permission in Android Manifest:
File Handler
File handler allows to store logs in file. Minimal example:
All parameters list:
- file (required) - the file where you want to store your logs
- enableDeviceParameters (optional) - please look in console handler description
- enableApplicationParameters (optional) - please look in console handler description
- enableStackTrace (optional) - please look in console handler description
- enableCustomParameters (optional) - please look in console handler description
- printLogs (optional) - enable/disable debug logs
Example of logging to file in external directory: https://github.com/jhomlala/catcher/blob/master/example/lib/file_example.dart
Toast Handler
Toast handler allows to show short message in toast. Minimal example:
All parameters list:
- gravity (optional) - location of the toast on screen top/middle/bottom
- length (optional) - length of toast: long or short
- backgroundColor (optional) - background color of toast
- textColor (optional) - text color of toast
- fontSize (optional) - text size
- customMessage (optional) - custom message for toast, if not set then "Error occured: error" will be displayed.
Sentry Handler
Sentry handler allows to send handled errors to Sentry.io. Before using sentry handler, you need to create your project in
Sentry.io page and then copy DSN link. Example:
All parameters list:
- sentryClient - sentry client instance
- enableDeviceParameters (optional) - please look in console handler description
- enableApplicationParameters (optional) - please look in console handler description
- enableCustomParameters (optional) - please look in console handler description
- printLogs (optional) - enable/disable debug logs
Slack Handler
Slack Handler allows to send messages to your Slack workspace. You can specify destination
of your message and format. You need to register webhook in your workspace to make this handler
works: https://api.slack.com/incoming-webhooks.
All parameters list:
- webhookUrl (required) - url of your webhook
- channel (required) - your channel name (i.e. #catcher)
- username (optional) - name of the integration bot
- iconEmoji (optional) - avatar of the integration bot
- enableDeviceParameters (optional) - please look in console handler description
- enableApplicationParameters (optional) - please look in console handler description
- enableCustomParameters (optional) - please look in console handler description
- enableStackTrace (optional) - please look in console handler description
- printLogs (optional) - enable/disable debug logs
Discord Handler
Discord Handler allows to send messages to your Discord workspace. You need to register webhook in your server to make this handler
works: https://support.discordapp.com/hc/en-us/articles/228383668-Intro-to-Webhooks.
All parameters list:
- webhookUrl (required) - url of your webhook
- enableDeviceParameters (optional) - please look in console handler description
- enableApplicationParameters (optional) - please look in console handler description
- enableCustomParameters (optional) - please look in console handler description
- enableStackTrace (optional) - please look in console handler description
- printLogs (optional) - enable/disable debug logs
Crashlytics Handler
Crashlytics Handler allows to send crash reports to Firebase service. You need to setup your project
with firebase libraries first. Follow instructions from here:
https://pub.dev/packages/firebase_crashlytics and https://firebase.google.com/docs/crashlytics/get-started
All parameters list:
- enableDeviceParameters (optional) - please look in console handler description
- enableApplicationParameters (optional) - please look in console handler description
- enableCustomParameters (optional) - please look in console handler description
- printLogs (optional) - enable/disable debug logs
Explicit exception report handler map
Explicit exception report handler map allows you to setup report handler for specific exception. For example if you want to setup Console Handler for FormatException, you can write:
Now if FormatException
will be catched, then Console Handler will be used. Warning: if you setup explicit exception map for specific exception, then only this handler will be used for this exception!
Explicit exception report mode map
Same as explicit report handler map, but it's for report mode. Let's say you want to use specific report mode for some exception:
When FormatException
will be catched, then NotificationReportMode will be used. For other exceptions, Catcher will use DialogReportMode.
Error widget
You can add error widget which will replace red screen of death. To add this into your app, see code below:
You need to add in your MaterialApp or CupertinoApp builder method with Catcher.addDefaultErrorWidget()
. This will add error handler for each widget in your app.
You can provide optional parameters:
- showStacktrace - show/hide stacktrace
- title - custom title for error widget
- description - custom description for error widget
- maxWidthForSmallMode - max width for "small" mode, default is 150
Error widget will replace your widget if he fails to render. If width of widget is less than maxWidthForSmallMode then "small" mode will be enabled, which will show only error icon
With error widget | Without error widget |
![]() | ![]() |
Current config
You can get currently used config by using:
This can be used for example to change custom parameters in runtime.
Test exception
Send test exception: