Skip to content

Commit f56cd36

Browse files
cjihrigBridgeAR
authored andcommitted
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: #29288 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent a5049c0 commit f56cd36

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

lib/internal/errors.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ function lazyUv() {
327327
return uvBinding;
328328
}
329329

330+
const uvUnmappedError = ['UNKNOWN', 'unknown error'];
331+
330332
function uvErrmapGet(name) {
331333
uvBinding = lazyUv();
332334
if (!uvBinding.errmap) {
@@ -346,7 +348,7 @@ function uvErrmapGet(name) {
346348
* @returns {Error}
347349
*/
348350
function uvException(ctx) {
349-
const [ code, uvmsg ] = uvErrmapGet(ctx.errno);
351+
const [ code, uvmsg ] = uvErrmapGet(ctx.errno) || uvUnmappedError;
350352
let message = `${code}: ${ctx.message || uvmsg}, ${ctx.syscall}`;
351353

352354
let path;
@@ -404,7 +406,7 @@ function uvException(ctx) {
404406
* @returns {Error}
405407
*/
406408
function uvExceptionWithHostPort(err, syscall, address, port) {
407-
const [ code, uvmsg ] = uvErrmapGet(err);
409+
const [ code, uvmsg ] = uvErrmapGet(err) || uvUnmappedError;
408410
const message = `${syscall} ${code}: ${uvmsg}`;
409411
let details = '';
410412

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
require('../common');
4+
const assert = require('assert');
5+
const { uvException, uvExceptionWithHostPort } = require('internal/errors');
6+
7+
{
8+
const exception = uvException({ errno: 100, syscall: 'open' });
9+
10+
assert.strictEqual(exception.message, 'UNKNOWN: unknown error, open');
11+
assert.strictEqual(exception.errno, 100);
12+
assert.strictEqual(exception.syscall, 'open');
13+
assert.strictEqual(exception.code, 'UNKNOWN');
14+
}
15+
16+
{
17+
const exception = uvExceptionWithHostPort(100, 'listen', '127.0.0.1', 80);
18+
19+
assert.strictEqual(exception.message,
20+
'listen UNKNOWN: unknown error 127.0.0.1:80');
21+
assert.strictEqual(exception.code, 'UNKNOWN');
22+
assert.strictEqual(exception.syscall, 'listen');
23+
assert.strictEqual(exception.address, '127.0.0.1');
24+
assert.strictEqual(exception.port, 80);
25+
}

0 commit comments

Comments
 (0)