From c2ad3ef8fd486bb58edc225f74152dda51f9f2fe Mon Sep 17 00:00:00 2001 From: Simon Woolf Date: Fri, 10 Jan 2020 23:14:06 +0000 Subject: [PATCH] Stop setting potentially non-string name properties on Errors (#2963) For example, extractError in query.js, when given a bare (bodyless) response, will pass the (numeric) httpResponse statusCode as the `code` option to util.error. util.error then sets the `name` property of the Error from this. The result is that property ends up as a number, but is required to a string; nodejs calls endsWith() on it https://github.com/nodejs/node/blob/e5d3c8121dd0bcc4dbf52c7b3a0521e359363a05/lib/internal/util/inspect.js#L922 , so will throw an exception if an Error with a non-string name is ever inspected. --- .changes/next-release/bugfix-util-358e56d3.json | 5 +++++ lib/util.js | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 .changes/next-release/bugfix-util-358e56d3.json diff --git a/.changes/next-release/bugfix-util-358e56d3.json b/.changes/next-release/bugfix-util-358e56d3.json new file mode 100644 index 0000000000..4ad28ce444 --- /dev/null +++ b/.changes/next-release/bugfix-util-358e56d3.json @@ -0,0 +1,5 @@ +{ + "type": "bugfix", + "category": "util", + "description": "Fix `name.endsWith is not a function` exception when inspecting some generated errors with some versions of nodejs" +} \ No newline at end of file diff --git a/lib/util.js b/lib/util.js index 3234171521..e2f9e6cbeb 100644 --- a/lib/util.js +++ b/lib/util.js @@ -594,7 +594,7 @@ var util = { Object.defineProperty(err, 'message', {enumerable: true}); } - err.name = options && options.name || err.name || err.code || 'Error'; + err.name = String(options && options.name || err.name || err.code || 'Error'); err.time = new Date(); if (originalError) err.originalError = originalError;