Repository: iopipe/iopipe-js-core
Pull request #294 (2018-09-07 21:17:04)

fix(promise rejection handling): Adding back in promise rejection ha…
…ndling


Patch:
diff --git a/.circleci/config.yml b/.circleci/config.yml
index b34acc3..28a7658 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -52,7 +52,7 @@ jobs:
           key: v1-dist-{{ .Environment.CIRCLE_BRANCH }}-{{ .Environment.CIRCLE_SHA1 }}
       - run: yarn run build
       - run: cp .npmrc-ci .npmrc
-      - run: npm publish --access public
+      - run: yarn run release
 
 workflows:
   version: 2
diff --git a/package.json b/package.json
index 637c970..25a44be 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "@iopipe/core",
-  "version": "1.14.2",
+  "version": "0.0.0-semantically-released",
   "description": "IOpipe Lambda Analytics & Tracing Agent",
   "main": "dist/iopipe.js",
   "scripts": {
@@ -16,6 +16,7 @@
     "jest": "iopipe-scripts test",
     "prepare": "npm run build",
     "prepublishOnly": "npm run build",
+    "release": "iopipe-scripts release",
     "test": "npm run lint && npm run build && npm run jest && npm run testProjects",
     "testProjects": "node util/testProjects",
     "webpack": "webpack --progress --profile --colors"
diff --git a/src/index.js b/src/index.js
index f02c109..6dd51ef 100644
--- a/src/index.js
+++ b/src/index.js
@@ -36,6 +36,16 @@ function setupTimeoutCapture(wrapperInstance) {
   }, endTime);
 }
 
+process.on('unhandledRejection', error => {
+  const ctx = getInvocationContext();
+  if (ctx && ctx.iopipe && ctx.iopipe.label) {
+    // default node behavior is to log these types of errors
+    console.error(error);
+    ctx.iopipe.label('@iopipe/unhandled-promise-rejection');
+    ctx.iopipe.label('@iopipe/error');
+  }
+});
+
 //TODO: refactor to abide by max-params rule*/
 /*eslint-disable max-params*/
 


Test patch:
diff --git a/testProjects/promiseRejection/package.json b/testProjects/promiseRejection/package.json
new file mode 100644
index 0000000..152764d
--- /dev/null
+++ b/testProjects/promiseRejection/package.json
@@ -0,0 +1,12 @@
+{
+  "name": "promiseRejectionTest",
+  "version": "0.0.0",
+  "description": "",
+  "main": "handler.test.js",
+  "scripts": {
+    "test": "node test.js"
+  },
+  "dependencies": {
+    "aws-lambda-mock-context": "^3.1.1"
+  }
+}
diff --git a/testProjects/promiseRejection/test.js b/testProjects/promiseRejection/test.js
new file mode 100644
index 0000000..14ea2b6
--- /dev/null
+++ b/testProjects/promiseRejection/test.js
@@ -0,0 +1,37 @@
+const mockContext = require('aws-lambda-mock-context');
+const iopipe = require('./iopipe');
+/*eslint-disable no-process-exit*/
+/*eslint-disable no-console*/
+
+const invocations = [];
+
+const handler = iopipe({
+  token: 'test',
+  plugins: [
+    inv => {
+      invocations.push(inv);
+    }
+  ]
+})((event, context) => {
+  const prom = new Promise(() => {
+    throw new Error('An error from a promise');
+  });
+  prom.then(context.succeed);
+});
+
+const ctx = mockContext({ timeout: 1 });
+handler({}, ctx);
+
+process.on('unhandledRejection', () => {
+  setTimeout(() => {
+    if (
+      invocations[0].report.report.labels.has(
+        '@iopipe/unhandled-promise-rejection'
+      )
+    ) {
+      process.exit(0);
+    }
+    console.error('Promise Error was NOT caught by iopipe.');
+    process.exit(1);
+  }, 10);
+});
diff --git a/testProjects/promiseRejection/yarn.lock b/testProjects/promiseRejection/yarn.lock
new file mode 100644
index 0000000..b82ce80
--- /dev/null
+++ b/testProjects/promiseRejection/yarn.lock
@@ -0,0 +1,23 @@
+# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
+# yarn lockfile v1
+
+
+aws-lambda-mock-context@^3.1.1:
+  version "3.1.1"
+  resolved "https://registry.yarnpkg.com/aws-lambda-mock-context/-/aws-lambda-mock-context-3.1.1.tgz#29b2d00bf9dc8bc461f749f8585691ee205573d5"
+  dependencies:
+    moment "^2.10.5"
+    pinkie-defer "^1.0.0"
+    uuid "^3.0.1"
+
+moment@^2.10.5:
+  version "2.22.1"
+  resolved "https://registry.yarnpkg.com/moment/-/moment-2.22.1.tgz#529a2e9bf973f259c9643d237fda84de3a26e8ad"
+
+pinkie-defer@^1.0.0:
+  version "1.0.0"
+  resolved "https://registry.yarnpkg.com/pinkie-defer/-/pinkie-defer-1.0.0.tgz#78fcc22116c0da890ac92a808ad370888ded9421"
+
+uuid@^3.0.1:
+  version "3.2.1"
+  resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.2.1.tgz#12c528bb9d58d0b9265d9a2f6f0fe8be17ff1f14"