Skip to content

Commit 7cf663d

Browse files
authored
Create setErrorMessage (#280)
1 parent 79ca69b commit 7cf663d

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

src/errors.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ export interface AzFuncError {
99
isAzureFunctionsSystemError: boolean;
1010
}
1111

12+
export interface ValidatedError extends Error, Partial<AzFuncError> {
13+
/**
14+
* Use `trySetErrorMessage` to set the error message
15+
*/
16+
readonly message: string;
17+
}
18+
1219
export class AzFuncSystemError extends Error {
1320
isAzureFunctionsSystemError = true;
1421
}
@@ -27,22 +34,8 @@ export class ReadOnlyError extends AzFuncTypeError {
2734
}
2835
}
2936

30-
export function ensureErrorType(err: unknown): Error & Partial<AzFuncError> {
37+
export function ensureErrorType(err: unknown): ValidatedError {
3138
if (err instanceof Error) {
32-
const writable = Object.getOwnPropertyDescriptor(err, 'message')?.writable;
33-
if (!writable) {
34-
// The motivation for this branch can be found in the below issue:
35-
// https://github.com/Azure/azure-functions-nodejs-library/issues/205
36-
let readableMessage = err.message;
37-
Object.defineProperty(err, 'message', {
38-
get() {
39-
return readableMessage;
40-
},
41-
set(val: string) {
42-
readableMessage = val;
43-
},
44-
});
45-
}
4639
return err;
4740
} else {
4841
let message: string;
@@ -59,6 +52,14 @@ export function ensureErrorType(err: unknown): Error & Partial<AzFuncError> {
5952
}
6053
}
6154

55+
export function trySetErrorMessage(err: Error, message: string): void {
56+
try {
57+
err.message = message;
58+
} catch {
59+
// If we can't set the message, we'll keep the error as is
60+
}
61+
}
62+
6263
/**
6364
* This is mostly for callbacks where `null` or `undefined` indicates there is no error
6465
* By contrast, anything thrown/caught is assumed to be an error regardless of what it is

test/errors.test.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import 'mocha';
55
import { expect } from 'chai';
6-
import { ensureErrorType } from '../src/errors';
6+
import { ensureErrorType, trySetErrorMessage } from '../src/errors';
77

88
describe('ensureErrorType', () => {
99
it('null', () => {
@@ -42,6 +42,13 @@ describe('ensureErrorType', () => {
4242
expect(ensureErrorType(actualError)).to.equal(actualError);
4343
});
4444

45+
it('modify error message', () => {
46+
const actualError = new Error('test2');
47+
trySetErrorMessage(actualError, 'modified message');
48+
49+
expect(actualError.message).to.equal('modified message');
50+
});
51+
4552
it('readonly error', () => {
4653
class ReadOnlyError extends Error {
4754
get message(): string {
@@ -55,10 +62,11 @@ describe('ensureErrorType', () => {
5562
expect(() => (actualError.message = 'exception')).to.throw();
5663

5764
const wrappedError = ensureErrorType(actualError);
58-
wrappedError.message = 'Readonly error has been modified';
65+
const message = 'Readonly error has not been modified';
66+
trySetErrorMessage(wrappedError, message);
5967

60-
expect(wrappedError.message).to.equal('Readonly error has been modified');
61-
expect(wrappedError.stack).to.contain('Readonly error has been modified');
68+
expect(wrappedError.message).to.equal('a readonly message');
69+
expect(wrappedError.stack).to.not.contain('Readonly error has been modified');
6270
});
6371

6472
function validateError(actual: Error, expectedMessage: string): void {

0 commit comments

Comments
 (0)