Skip to content

Commit

Permalink
feat: Added default unhandled promise rejection handler (#67)
Browse files Browse the repository at this point in the history
* fix: Added default unhandled promise rejection handler

* fixed typos
  • Loading branch information
bflorian committed Apr 24, 2019
1 parent 12af9fe commit 1869a9c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -142,6 +142,14 @@ Configuration page strings are specified in a separate `locales/en.json` file, w
}
```

### Unhandled Promise Rejection Handling

By default, instantiation of the SmartApp object registers an "unhandledReject" handler
that logs unhandled promise rejections. If you don't want this behavior you can disable
it by passing an option to the SmartApp instantiation, e.g. `new SmartApp({logUnhandledRejections: false})`.
If you want to replace the handler you can do that by calling `unhandledRejectionHandler(promise => {...})`
on the SmartApp object.

### Making API calls outside of an EVENT handler

By default, the SmartApp SDK will facilitate API calls on behalf of a user within the `EVENT` lifecycle. These user tokens are ephemeral and last *5 minutes*. These access tokens are not able to be refreshed and should _not_ be stored. If you're making out-of-band API calls on behalf of a user's installed app, you will need to use the 24-hour access token that are supplied after `INSTALL` and `UPDATE` lifecycles. This token includes a `refresh_token`, and will be automatically refreshed by the SDK when necessary.
Expand Down
20 changes: 20 additions & 0 deletions lib/smart-app.js
Expand Up @@ -25,6 +25,7 @@ module.exports = class SmartApp {
* @prop {number} jsonSpace
* @prop {Boolean} enableEventLogging
* @prop {String} publicKey
* @prop {Boolean} logUnhandledRejections
*/
/**
* Create a SmartApp instance
Expand Down Expand Up @@ -60,6 +61,14 @@ module.exports = class SmartApp {
if (options.publicKey) {
signature.setPublicKey(options.publicKey)
}

this._unhandledRejectionHandler = reason => {
this._log.exception(reason)
}

if (options.logUnhandledRejections !== false) {
process.on('unhandledRejection', this._unhandledRejectionHandler)
}
}

/// /////////////////////////////
Expand Down Expand Up @@ -209,6 +218,17 @@ module.exports = class SmartApp {
return this
}

/**
* Replaces the default unhandled rejection handler. If you don't want to have a default handler at
* all then instantiate the app with new SmartApp({logUnhandledRejections: false})
*
* @param {Function} callback when a promise rejection is not handled
* @returns {SmartApp} SmartApp instance */
unhandledRejectionHandler(callback) {
this._unhandledRejectionHandler = callback
return this
}

/// ///////////////////////////
// Configuration/Initialize //
/// ///////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@smartthings/smartapp",
"version": "1.1.1",
"version": "1.1.4",
"description": "NodeJS SDK for SmartApps",
"displayName": "SmartThings SmartApp SDK for NodeJS",
"author": "SmartThings",
Expand Down

0 comments on commit 1869a9c

Please sign in to comment.