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 <luigipinca@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This commit is contained in:
cjihrig 2019-08-23 16:43:17 -04:00 committed by Rich Trott
parent 5ba3a2c4b6
commit c246f813f8
2 changed files with 30 additions and 2 deletions

View File

@ -327,6 +327,8 @@ function lazyUv() {
return uvBinding; return uvBinding;
} }
const uvUnmappedError = ['UNKNOWN', 'unknown error'];
function uvErrmapGet(name) { function uvErrmapGet(name) {
uvBinding = lazyUv(); uvBinding = lazyUv();
if (!uvBinding.errmap) { if (!uvBinding.errmap) {
@ -346,7 +348,7 @@ function uvErrmapGet(name) {
* @returns {Error} * @returns {Error}
*/ */
function uvException(ctx) { 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 message = `${code}: ${ctx.message || uvmsg}, ${ctx.syscall}`;
let path; let path;
@ -404,7 +406,7 @@ function uvException(ctx) {
* @returns {Error} * @returns {Error}
*/ */
function uvExceptionWithHostPort(err, syscall, address, port) { function uvExceptionWithHostPort(err, syscall, address, port) {
const [ code, uvmsg ] = uvErrmapGet(err); const [ code, uvmsg ] = uvErrmapGet(err) || uvUnmappedError;
const message = `${syscall} ${code}: ${uvmsg}`; const message = `${syscall} ${code}: ${uvmsg}`;
let details = ''; let details = '';

View File

@ -0,0 +1,26 @@
// 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.errno, 100);
assert.strictEqual(exception.syscall, 'listen');
assert.strictEqual(exception.address, '127.0.0.1');
assert.strictEqual(exception.port, 80);
}