Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create setErrorMessage function #280

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -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;
}
@@ -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;
@@ -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
16 changes: 12 additions & 4 deletions test/errors.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
@@ -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 {
@@ -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 {