Skip to main content

Add A Crash Reporter To An Application

Deliver Application Crash Notifications When You Are Not Logging

Device and agent development code crashes are reported via the impCentral log, but they may occur at times when you are not observing the log. While you can pull down historical logs via the impCentral API to inspect them after the event, there is a limit on the number of available log entries; applications which log heavily may have introduced so many subsequent entries that the original crash report is no longer available.

One way around this is to relay log messages to a data store, but the following code recipe takes a slightly different approach. It traps all otherwise unhandled exceptions, assembles a crash report based upon that exception, and relays it to a notification service. One such service is Slack, and the code below includes a simple auxiliary class for sending the error as a message to Slack. However, the crash reporter code accepts any such service: you simply provide it with a reference to any agent-side function able to relay the crash report.

For example, using the SimpleSlack class, which includes a suitable message-relay method, post(), would be achieved as follows:

// This is a non-functional path, for illustration only
const SLACK_WEBHOOK_PATH = "T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX";

// Instance SimpleSlack with a Slack Incoming Webhook path
local slack = SimpleSlack(SLACK_WEBHOOK_PATH);

// Hand crashReporter a reference to the instance's post() method
crashReporter.init(slack.post.bindenv(slack));

Note We use Squirrel’s bindenv() to access the instance correctly.

The result is a posting to Slack:

You don’t need to use SimpleSlack — any code supporting a the same or a similar service will do. The only restriction is that it include a function which takes a single parameter: a string containing the crash report message. If you messaging class does not have such a function, you can write a bridge function that takes a message string and uses it in a call to your messaging class’ own ‘send’ method:

crashReporter.init(function(errorMessage) {
    // Call the send function
    messager.send(errorMessage, API_KEY);
}.bindenv(this));

The crash reporter code should be included in both your device and your agent code. Device crash reports are relayed to the agent (provided the device is connected), which sends them to the notification service. Agent crashes are likewise relayed.

Example Code: Crash Reporter

Example Code: Slack Message Poster

API Methods Used

Further Reading