Skip to content

Commit

Permalink
Create setErrorMessage (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
castrodd authored Jul 10, 2024
1 parent 79ca69b commit 7cf663d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
31 changes: 16 additions & 15 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ export interface AzFuncError {
isAzureFunctionsSystemError: boolean;
}

export interface ValidatedError extends Error, Partial<AzFuncError> {
/**
* Use `trySetErrorMessage` to set the error message
*/
readonly message: string;
}

export class AzFuncSystemError extends Error {
isAzureFunctionsSystemError = true;
}
Expand All @@ -27,22 +34,8 @@ export class ReadOnlyError extends AzFuncTypeError {
}
}

export function ensureErrorType(err: unknown): Error & Partial<AzFuncError> {
export function ensureErrorType(err: unknown): ValidatedError {
if (err instanceof Error) {
const writable = Object.getOwnPropertyDescriptor(err, 'message')?.writable;
if (!writable) {
// The motivation for this branch can be found in the below issue:
// https://github.com/Azure/azure-functions-nodejs-library/issues/205
let readableMessage = err.message;
Object.defineProperty(err, 'message', {
get() {
return readableMessage;
},
set(val: string) {
readableMessage = val;
},
});
}
return err;
} else {
let message: string;
Expand All @@ -59,6 +52,14 @@ export function ensureErrorType(err: unknown): Error & Partial<AzFuncError> {
}
}

export function trySetErrorMessage(err: Error, message: string): void {
try {
err.message = message;
} catch {
// If we can't set the message, we'll keep the error as is
}
}

/**
* This is mostly for callbacks where `null` or `undefined` indicates there is no error
* By contrast, anything thrown/caught is assumed to be an error regardless of what it is
Expand Down
16 changes: 12 additions & 4 deletions test/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import 'mocha';
import { expect } from 'chai';
import { ensureErrorType } from '../src/errors';
import { ensureErrorType, trySetErrorMessage } from '../src/errors';

describe('ensureErrorType', () => {
it('null', () => {
Expand Down Expand Up @@ -42,6 +42,13 @@ describe('ensureErrorType', () => {
expect(ensureErrorType(actualError)).to.equal(actualError);
});

it('modify error message', () => {
const actualError = new Error('test2');
trySetErrorMessage(actualError, 'modified message');

expect(actualError.message).to.equal('modified message');
});

it('readonly error', () => {
class ReadOnlyError extends Error {
get message(): string {
Expand All @@ -55,10 +62,11 @@ describe('ensureErrorType', () => {
expect(() => (actualError.message = 'exception')).to.throw();

const wrappedError = ensureErrorType(actualError);
wrappedError.message = 'Readonly error has been modified';
const message = 'Readonly error has not been modified';
trySetErrorMessage(wrappedError, message);

expect(wrappedError.message).to.equal('Readonly error has been modified');
expect(wrappedError.stack).to.contain('Readonly error has been modified');
expect(wrappedError.message).to.equal('a readonly message');
expect(wrappedError.stack).to.not.contain('Readonly error has been modified');
});

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

0 comments on commit 7cf663d

Please sign in to comment.