Skip to content

Commit

Permalink
[3.0.0-alpha.2] Second try to upgrade to Programming Model V4
Browse files Browse the repository at this point in the history
  • Loading branch information
maaaNu committed Apr 17, 2024
1 parent 4e38904 commit 21819de
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 31 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@senacor/azure-function-middleware",
"version": "3.0.0-alpha.1",
"version": "3.0.0-alpha.2",
"description": "Middleware for azure functions to handle authentication, authorization, error handling and logging",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
42 changes: 21 additions & 21 deletions src/error/errorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,32 @@ export const errorHandler = (
opts?: Options,
// TODO: Can we remove any?
): any => {
if (error instanceof ApplicationError) {
context.error(`Received application error with message ${error.message}`);
const errorAsString = stringify(error);
context.error(errorAsString);

if (typeof error.body === 'object') {
return {
status: error.status,
jsonBody: error.body,
};
if (opts?.errorResponseHandler === undefined) {
if (error instanceof ApplicationError) {
context.error(`Received application error with message ${error.message}`);

if (typeof error.body === 'object') {
return {
status: error.status,
jsonBody: error.body,
};
} else {
return {
status: error.status,
body: stringify(error.body),
};
}
} else {
return {
status: error.status,
body: stringify(error.body),
status: 500,
jsonBody: {
message: 'Internal server error',
},
};
}
}

const errorAsString = stringify(error);
context.error(errorAsString);

if (opts?.errorResponseHandler === undefined) {
return {
status: 500,
jsonBody: {
message: 'Internal server error',
},
};
} else {
return opts.errorResponseHandler(error, context);
}
Expand Down
2 changes: 1 addition & 1 deletion src/util/stringify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('stringify', () => {

it('returns object.toString() for an empty object', () => {
const input = {};
expect(stringify(input)).toBe('[object Object]');
expect(stringify(input)).toBe('{}');
});

it('returns error message for null', () => {
Expand Down
7 changes: 1 addition & 6 deletions src/util/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ const stringifyObject = (error: object | null): string => {
return JSON.stringify({ message: error.message, stack: error.stack });
} else {
try {
const errorAsJson = JSON.stringify(error);
if (errorAsJson === '{}') {
return error.toString();
} else {
return errorAsJson;
}
return JSON.stringify(error);
} catch (_) {
//Fallback in case there's an error stringify
return error.toString();
Expand Down

0 comments on commit 21819de

Please sign in to comment.