From 2151cf1b906f6e168ce6d2198bb7237c4ef4a3b2 Mon Sep 17 00:00:00 2001 From: Gabe Gorelick Date: Wed, 14 Nov 2018 04:36:35 -0600 Subject: [PATCH] Throw Error objects instead of strings (#497) Unlike strings, Error objects allow you to get stack traces. Also, a lot of libraries will complain if they encounter a thrown non-Error object. This is in line with standard Javascript best practices. Per https://eslint.org/docs/rules/no-throw-literal: --- .eslintrc.js | 2 +- papaparse.js | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index f63bc09..c3ed7c5 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -178,7 +178,7 @@ module.exports = { "no-tabs": "off", "no-template-curly-in-string": "error", "no-ternary": "off", - "no-throw-literal": "off", + "no-throw-literal": "error", "no-trailing-spaces": "error", "no-undef-init": "error", "no-undefined": "off", diff --git a/papaparse.js b/papaparse.js index ababc70..0edec76 100755 --- a/papaparse.js +++ b/papaparse.js @@ -331,7 +331,7 @@ if (!Array.isArray) } // Default (any valid paths should return before this) - throw 'exception: Unable to serialize unrecognized input'; + throw new Error('Unable to serialize unrecognized input'); function unpackConfig() @@ -1368,7 +1368,7 @@ if (!Array.isArray) // Comment character must be valid if (comments === delim) - throw 'Comment character same as delimiter'; + throw new Error('Comment character same as delimiter'); else if (comments === true) comments = '#'; else if (typeof comments !== 'string' @@ -1387,7 +1387,7 @@ if (!Array.isArray) { // For some reason, in Chrome, this speeds things up (!?) if (typeof input !== 'string') - throw 'Input must be a string'; + throw new Error('Input must be a string'); // We don't need to compute some of these every time parse() is called, // but having them in a more local scope seems to perform better @@ -1770,7 +1770,7 @@ if (!Array.isArray) } function notImplemented() { - throw 'Not implemented.'; + throw new Error('Not implemented.'); } /** Callback when worker thread receives a message */