Skip to content

Commit 1869a9c

Browse files
authored
feat: Added default unhandled promise rejection handler (#67)
* fix: Added default unhandled promise rejection handler * fixed typos
1 parent 12af9fe commit 1869a9c

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ Configuration page strings are specified in a separate `locales/en.json` file, w
142142
}
143143
```
144144

145+
### Unhandled Promise Rejection Handling
146+
147+
By default, instantiation of the SmartApp object registers an "unhandledReject" handler
148+
that logs unhandled promise rejections. If you don't want this behavior you can disable
149+
it by passing an option to the SmartApp instantiation, e.g. `new SmartApp({logUnhandledRejections: false})`.
150+
If you want to replace the handler you can do that by calling `unhandledRejectionHandler(promise => {...})`
151+
on the SmartApp object.
152+
145153
### Making API calls outside of an EVENT handler
146154

147155
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.

lib/smart-app.js

+20
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module.exports = class SmartApp {
2525
* @prop {number} jsonSpace
2626
* @prop {Boolean} enableEventLogging
2727
* @prop {String} publicKey
28+
* @prop {Boolean} logUnhandledRejections
2829
*/
2930
/**
3031
* Create a SmartApp instance
@@ -60,6 +61,14 @@ module.exports = class SmartApp {
6061
if (options.publicKey) {
6162
signature.setPublicKey(options.publicKey)
6263
}
64+
65+
this._unhandledRejectionHandler = reason => {
66+
this._log.exception(reason)
67+
}
68+
69+
if (options.logUnhandledRejections !== false) {
70+
process.on('unhandledRejection', this._unhandledRejectionHandler)
71+
}
6372
}
6473

6574
/// /////////////////////////////
@@ -209,6 +218,17 @@ module.exports = class SmartApp {
209218
return this
210219
}
211220

221+
/**
222+
* Replaces the default unhandled rejection handler. If you don't want to have a default handler at
223+
* all then instantiate the app with new SmartApp({logUnhandledRejections: false})
224+
*
225+
* @param {Function} callback when a promise rejection is not handled
226+
* @returns {SmartApp} SmartApp instance */
227+
unhandledRejectionHandler(callback) {
228+
this._unhandledRejectionHandler = callback
229+
return this
230+
}
231+
212232
/// ///////////////////////////
213233
// Configuration/Initialize //
214234
/// ///////////////////////////

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@smartthings/smartapp",
3-
"version": "1.1.1",
3+
"version": "1.1.4",
44
"description": "NodeJS SDK for SmartApps",
55
"displayName": "SmartThings SmartApp SDK for NodeJS",
66
"author": "SmartThings",

0 commit comments

Comments
 (0)