Skip to content

Commit 90f395d

Browse files
jasnelltargos
authored andcommitted
errors: add support for cause in aborterror
Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #41008 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 6f9c622 commit 90f395d

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

lib/internal/errors.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -834,8 +834,11 @@ function hideInternalStackFrames(error) {
834834
// to make usage of the error in userland and readable-stream easier.
835835
// It is a regular error with `.code` and `.name`.
836836
class AbortError extends Error {
837-
constructor() {
838-
super('The operation was aborted');
837+
constructor(message = 'The operation was aborted', options = undefined) {
838+
if (options !== undefined && typeof options !== 'object') {
839+
throw new codes.ERR_INVALID_ARG_TYPE('options', 'Object', options);
840+
}
841+
super(message, options);
839842
this.code = 'ABORT_ERR';
840843
this.name = 'AbortError';
841844
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
4+
require('../common');
5+
const {
6+
strictEqual,
7+
throws,
8+
} = require('assert');
9+
const { AbortError } = require('internal/errors');
10+
11+
{
12+
const err = new AbortError();
13+
strictEqual(err.message, 'The operation was aborted');
14+
strictEqual(err.cause, undefined);
15+
}
16+
17+
{
18+
const cause = new Error('boom');
19+
const err = new AbortError('bang', { cause });
20+
strictEqual(err.message, 'bang');
21+
strictEqual(err.cause, cause);
22+
}
23+
24+
{
25+
throws(() => new AbortError('', false), {
26+
code: 'ERR_INVALID_ARG_TYPE'
27+
});
28+
throws(() => new AbortError('', ''), {
29+
code: 'ERR_INVALID_ARG_TYPE'
30+
});
31+
}

0 commit comments

Comments
 (0)