Skip to content

Commit 516b1da

Browse files
committed
Migration code of lib to ESM.
1 parent 5433613 commit 516b1da

2 files changed

Lines changed: 13 additions & 14 deletions

File tree

src/chain-error.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import * as assert from 'assert-plus';
1+
import assert from 'assert-plus';
22
import { types } from 'util';
33

4-
import { ChainErrorOptions, ObjectAny } from './types';
4+
import { ChainErrorOptions, ObjectAny } from './types.js';
55

66
export class ChainError<T extends ObjectAny = ObjectAny> extends Error {
7-
readonly skipCauseMessage: boolean;
7+
readonly skipCauseMessage?: boolean;
88
/**
99
* Indicates that the new error was caused by `cause`. See `getCause()` below.
1010
* If unspecified, the cause will be `null`.
1111
*/
12-
readonly cause: Error;
12+
override readonly cause?: Error;
1313
/**
1414
* Specifies arbitrary informational properties that
1515
* are available through the `ChainError.getInfo(err)` static class method.
@@ -23,7 +23,7 @@ export class ChainError<T extends ObjectAny = ObjectAny> extends Error {
2323
*/
2424
readonly currentMessage: string;
2525

26-
constructor(message?: string, optsOrError?: ChainErrorOptions<T> | Error, skipCauseMessage?: boolean) {
26+
constructor(message?: string | null, optsOrError?: ChainErrorOptions<T> | Error | null, skipCauseMessage?: boolean) {
2727
super();
2828

2929
this.skipCauseMessage = skipCauseMessage;
@@ -85,7 +85,7 @@ export class ChainError<T extends ObjectAny = ObjectAny> extends Error {
8585
* You can walk the `cause` chain by invoking `ChainError.getCause(err)`
8686
* on each subsequent return value.
8787
*/
88-
static getCause(err: Error): Error | null {
88+
static getCause(err: Error): Error | null | undefined {
8989
assert.ok(types.isNativeError(err), 'err must be an Error');
9090
return types.isNativeError((err as ChainError).cause) ? (err as ChainError).cause : null;
9191
}
@@ -106,10 +106,9 @@ export class ChainError<T extends ObjectAny = ObjectAny> extends Error {
106106
static getInfo<T extends ObjectAny = ObjectAny>(error: Error): T {
107107
const err = error as ChainError;
108108
let info = {} as T;
109-
let cause: Error;
110109

111-
cause = this.getCause(err);
112-
if (cause !== null) {
110+
const cause = this.getCause(err);
111+
if (cause) {
113112
info = this.getInfo(cause);
114113
}
115114

@@ -137,7 +136,7 @@ export class ChainError<T extends ObjectAny = ObjectAny> extends Error {
137136
assert.string(name, 'name');
138137
assert.ok(name.length > 0, 'name cannot be empty');
139138

140-
for (let cause = err; cause !== null; cause = this.getCause(cause)) {
139+
for (let cause: Error | null | undefined = err; cause; cause = this.getCause(cause)) {
141140
if (cause.name == name) {
142141
return cause;
143142
}
@@ -159,7 +158,7 @@ export class ChainError<T extends ObjectAny = ObjectAny> extends Error {
159158
* Returns a string containing the full stack trace, with all nested errors recursively
160159
* reported as `'caused by:' + err.stack`.
161160
*/
162-
static getFullStack(err: Error): string {
161+
static getFullStack(err: Error): string | undefined {
163162
const cause = this.getCause(err);
164163
if (cause) {
165164
return err.stack + '\ncaused by: ' + this.getFullStack(cause);
@@ -168,7 +167,7 @@ export class ChainError<T extends ObjectAny = ObjectAny> extends Error {
168167
return err.stack;
169168
}
170169

171-
toString() {
170+
override toString() {
172171
let str = (this.hasOwnProperty('name') && this.name) || this.constructor.name || this.constructor.prototype.name;
173172
if (this.message) {
174173
str += ': ' + this.message;

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export * from './chain-error';
2-
export { ChainErrorOptions } from './types';
1+
export * from './chain-error.js';
2+
export { ChainErrorOptions } from './types.js';

0 commit comments

Comments
 (0)