Skip to content

Commit

Permalink
Merge pull request #10 from aegenet/feat-belt-error-mutate-options
Browse files Browse the repository at this point in the history
feat(mutate-error-with-ref): setAsEnumerable options
  • Loading branch information
aegenet committed Jul 28, 2023
2 parents 6150b17 + bdeb8f5 commit 8e454ec
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/belt-error/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ const error = new Error('An error!');

(error as RefError).refError; // E-XXXXXXX
(error as RefError).message; // E-XXXXXXX - [message]
// Beware `message` and `stack` are not enumerable in Error object
Object.keys(error); // ['refError']


// You can set (danger zone) the properties as enumerable:
const error = new Error('An error!', {
setAsEnumerable: true,
});
(error as RefError).refError; // E-XXXXXXX
(error as RefError).message; // E-XXXXXXX - [message]

Object.keys(error); // ['stack', 'message', 'refError']
```

## getErrorMessage
Expand Down
20 changes: 20 additions & 0 deletions packages/belt-error/src/mutate-error-with-ref.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ describe('mutate-error-with-ref', () => {
assert.strictEqual(mutateError.message, 'Toto');
assert.strictEqual(mutateError, error);
assert.ok(mutateError.refError);
assert.deepStrictEqual(Object.keys(mutateError), ['refError']);
});

it('Set message/stack enumerable', () => {
const error = new Error('Toto');
const mutateError = mutateErrorWithRef(error, {
setAsEnumerable: true,
});
assert.strictEqual(error.message, 'Toto');
assert.strictEqual(mutateError.message, 'Toto');
assert.strictEqual(mutateError, error);
assert.ok(mutateError.refError);
assert.deepStrictEqual(Object.keys(mutateError), ['stack', 'message', 'refError']);
});

it('Mutate an object', () => {
Expand All @@ -17,6 +30,7 @@ describe('mutate-error-with-ref', () => {
assert.strictEqual(error.message, 'Toto');
assert.strictEqual(mutateError.message, 'Toto');
assert.ok(mutateError.refError);
assert.deepStrictEqual(Object.keys(mutateError), ['refError']);
});

it('Mutate with identifier', () => {
Expand All @@ -28,6 +42,7 @@ describe('mutate-error-with-ref', () => {
assert.ok(mutateError.refError);
assert.ok(mutateError.refError.startsWith('E-'));
assert.ok(mutateError.refError.endsWith('5c'));
assert.deepStrictEqual(Object.keys(mutateError), ['refError']);
});

it('Mutate, prefix ref in message', () => {
Expand All @@ -38,6 +53,7 @@ describe('mutate-error-with-ref', () => {
assert.strictEqual(mutateError, error);
assert.ok(mutateError.refError);
assert.ok(mutateError.message.startsWith(mutateError.refError));
assert.deepStrictEqual(Object.keys(mutateError), ['refError']);
});

it('Mutate with empty message', () => {
Expand All @@ -48,6 +64,7 @@ describe('mutate-error-with-ref', () => {
assert.strictEqual(mutateError, error);
assert.ok(mutateError.refError);
assert.ok(mutateError.message.startsWith(mutateError.refError));
assert.deepStrictEqual(Object.keys(mutateError), ['refError']);
});

it('Mutate two times', () => {
Expand All @@ -60,6 +77,7 @@ describe('mutate-error-with-ref', () => {
assert.strictEqual(refError, mutateError2.refError);
assert.ok(mutateError.refError);
assert.ok(mutateError.message.startsWith(mutateError.refError));
assert.deepStrictEqual(Object.keys(mutateError), ['refError']);
});

it('Mutate and add properties', () => {
Expand All @@ -78,6 +96,7 @@ describe('mutate-error-with-ref', () => {
assert.strictEqual(mutateError.tenant, 'yolo');
assert.strictEqual(mutateError.user, 'maurice');
assert.ok(mutateError.message.startsWith(mutateError.refError));
assert.deepStrictEqual(Object.keys(mutateError), ['refError', 'tenant', 'user']);
});

it('More than 4095 errors', () => {
Expand All @@ -97,6 +116,7 @@ describe('mutate-error-with-ref', () => {
assert.strictEqual(mutateError.tenant, 'yolo');
assert.strictEqual(mutateError.user, 'maurice');
assert.ok(mutateError.message.startsWith(mutateError.refError));
assert.deepStrictEqual(Object.keys(mutateError), ['refError', 'tenant', 'user']);
}
});
});
16 changes: 16 additions & 0 deletions packages/belt-error/src/mutate-error-with-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export function mutateErrorWithRef<T, D extends Record<string, unknown>>(
prefixWithRef?: boolean;
/** Data that will be injected into the error object */
data?: D;
/** Set `message` and `stack` properties to enumerable */
setAsEnumerable?: boolean;
} = {}
): RefError<T & D> {
const err: RefError<T & D> = asError(error) as RefError<T & D>;
Expand All @@ -39,5 +41,19 @@ export function mutateErrorWithRef<T, D extends Record<string, unknown>>(
}
}

if (options.setAsEnumerable) {
if (err.message) {
Object.defineProperty(err, 'message', {
value: err.message,
enumerable: true,
});
}
if (err.stack) {
Object.defineProperty(err, 'stack', {
value: err.stack,
enumerable: true,
});
}
}
return err;
}

0 comments on commit 8e454ec

Please sign in to comment.