From 9182d58a5d3e9d6210f4f12144e56cc407510440 Mon Sep 17 00:00:00 2001 From: Craig Date: Thu, 4 Aug 2016 14:20:33 -0700 Subject: [PATCH] chore(error): throwing strings (#3436) - fix throw string to throw new error - check typeof error is string to create a new error --- lib/util.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/util.ts b/lib/util.ts index a25d7ee3d..ee99da15d 100644 --- a/lib/util.ts +++ b/lib/util.ts @@ -41,19 +41,26 @@ export function runFilenameOrFn_( if (filenameOrFn && !(typeof filenameOrFn === 'string' || typeof filenameOrFn === 'function')) { - throw 'filenameOrFn must be a string or function'; + throw new Error('filenameOrFn must be a string or function'); } if (typeof filenameOrFn === 'string') { filenameOrFn = require(resolve(configDir, filenameOrFn)); } if (typeof filenameOrFn === 'function') { - let results = when(filenameOrFn.apply(null, args), null, (err) => { - if (err.stack) { - err.stack = exports.filterStackTrace(err.stack); - } - throw err; - }); + let results = + when(filenameOrFn.apply(null, args), null, (err: string | Error) => { + if (typeof err === 'string') { + err = new Error(err); + } else { + err = err as Error; + if (err.stack) { + err.stack = new Error().stack; + } + } + err.stack = exports.filterStackTrace(err.stack); + throw err; + }); resolvePromise(results); } else { resolvePromise(undefined);