From 30b80e5d7ca65828b9132e46f69193b226a1716e Mon Sep 17 00:00:00 2001 From: cjihrig Date: Fri, 23 Aug 2019 16:43:17 -0400 Subject: [PATCH] errors: provide defaults for unmapped uv errors libuv does not map 100% of errors. When an unmapped error is encountered, the Map returns undefined, which is then unsuccessfully destructured, causing an exception. This commit adds a default value in the event of an unmapped error. PR-URL: https://github.com/nodejs/node/pull/29288 Reviewed-By: Luigi Pinca Reviewed-By: Ben Noordhuis --- lib/internal/errors.js | 6 +++-- test/parallel/test-uv-unmapped-exception.js | 25 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-uv-unmapped-exception.js diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 1a788a6e4bb626..85fe9c9e8e093b 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -327,6 +327,8 @@ function lazyUv() { return uvBinding; } +const uvUnmappedError = ['UNKNOWN', 'unknown error']; + function uvErrmapGet(name) { uvBinding = lazyUv(); if (!uvBinding.errmap) { @@ -346,7 +348,7 @@ function uvErrmapGet(name) { * @returns {Error} */ function uvException(ctx) { - const [ code, uvmsg ] = uvErrmapGet(ctx.errno); + const [ code, uvmsg ] = uvErrmapGet(ctx.errno) || uvUnmappedError; let message = `${code}: ${ctx.message || uvmsg}, ${ctx.syscall}`; let path; @@ -404,7 +406,7 @@ function uvException(ctx) { * @returns {Error} */ function uvExceptionWithHostPort(err, syscall, address, port) { - const [ code, uvmsg ] = uvErrmapGet(err); + const [ code, uvmsg ] = uvErrmapGet(err) || uvUnmappedError; const message = `${syscall} ${code}: ${uvmsg}`; let details = ''; diff --git a/test/parallel/test-uv-unmapped-exception.js b/test/parallel/test-uv-unmapped-exception.js new file mode 100644 index 00000000000000..26a38b01cfbfe2 --- /dev/null +++ b/test/parallel/test-uv-unmapped-exception.js @@ -0,0 +1,25 @@ +// Flags: --expose-internals +'use strict'; +require('../common'); +const assert = require('assert'); +const { uvException, uvExceptionWithHostPort } = require('internal/errors'); + +{ + const exception = uvException({ errno: 100, syscall: 'open' }); + + assert.strictEqual(exception.message, 'UNKNOWN: unknown error, open'); + assert.strictEqual(exception.errno, 100); + assert.strictEqual(exception.syscall, 'open'); + assert.strictEqual(exception.code, 'UNKNOWN'); +} + +{ + const exception = uvExceptionWithHostPort(100, 'listen', '127.0.0.1', 80); + + assert.strictEqual(exception.message, + 'listen UNKNOWN: unknown error 127.0.0.1:80'); + assert.strictEqual(exception.code, 'UNKNOWN'); + assert.strictEqual(exception.syscall, 'listen'); + assert.strictEqual(exception.address, '127.0.0.1'); + assert.strictEqual(exception.port, 80); +}