-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #333 from senacor/feature/execute-post-function-if…
…-handler-fails [2.1.0] Post-Execution-Functions will now be executed, even if the handler failed
- Loading branch information
Showing
30 changed files
with
494 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
node_modules | ||
dist | ||
*.test.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,11 @@ | ||
module.exports = { | ||
parser: "@typescript-eslint/parser", // Specifies the ESLint parser | ||
parser: '@typescript-eslint/parser', // Specifies the ESLint parser | ||
parserOptions: { | ||
ecmaVersion: 2020, // Allows for the parsing of modern ECMAScript features | ||
sourceType: "module", // Allows for the use of imports | ||
ecmaFeatures: { | ||
} | ||
}, | ||
settings: { | ||
}, | ||
extends: [ | ||
"plugin:@typescript-eslint/recommended", | ||
"plugin:prettier/recommended" | ||
], | ||
rules: { | ||
sourceType: 'module', // Allows for the use of imports | ||
ecmaFeatures: {}, | ||
}, | ||
settings: {}, | ||
extends: ['plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended'], | ||
rules: {}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,4 @@ package-lock.json | |
*.ejs | ||
dist | ||
coverage | ||
dist/ | ||
config/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
example/test-header-authentication-function/test-header-authentication-function.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
example/test-jwt-authorization-function/test-jwt-authorization-function.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@senacor/azure-function-middleware", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "Middleware for azure functions to handle authentication, authorization, error handling and logging", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
|
@@ -25,6 +25,7 @@ | |
"author": "[email protected]", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@trivago/prettier-plugin-sort-imports": "^4.2.0", | ||
"@types/jest": "^27.0.3", | ||
"@typescript-eslint/eslint-plugin": "^5.10.2", | ||
"@typescript-eslint/parser": "^5.10.2", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Context } from '@azure/functions'; | ||
import { mock } from 'jest-mock-extended'; | ||
|
||
import { ApplicationError } from './ApplicationError'; | ||
import { errorHandler as sut } from './errorHandler'; | ||
|
||
describe('Error-Handler should', () => { | ||
const contextMock = mock<Context>(); | ||
|
||
beforeEach(() => { | ||
jest.restoreAllMocks(); | ||
|
||
contextMock.log.error = jest.fn(); | ||
}); | ||
|
||
test('return an provided ApplicationError', () => { | ||
const res = sut(new ApplicationError('', 400, { error: 'critical' }), contextMock); | ||
expect(res.status).toStrictEqual(400); | ||
expect(res.body).toStrictEqual({ error: 'critical' }); | ||
}); | ||
|
||
test('return an default error-message, if no errorResponseHandler was provided', () => { | ||
const res = sut('Error!', contextMock); | ||
expect(res.status).toStrictEqual(500); | ||
expect(res.body).toStrictEqual({ message: 'Internal server error' }); | ||
}); | ||
|
||
test('use the errorResponseHandler, if an errorResponseHandler was provided', () => { | ||
const errorResponseHandler = () => ({ | ||
status: 409, | ||
body: { | ||
message: 'conflict!', | ||
}, | ||
}); | ||
const res = sut('Error!', contextMock, { errorResponseHandler }); | ||
expect(res.status).toStrictEqual(409); | ||
expect(res.body).toStrictEqual({ message: 'conflict!' }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Context } from '@azure/functions'; | ||
|
||
import { Options } from '../middleware'; | ||
import { ApplicationError } from './ApplicationError'; | ||
|
||
type ErrorWithMessage = { | ||
message: string; | ||
stack?: string; | ||
}; | ||
|
||
const isErrorWithMessage = (error: unknown): error is ErrorWithMessage => { | ||
return ( | ||
typeof error === 'object' && | ||
error !== null && | ||
'message' in error && | ||
typeof (error as Record<string, unknown>).message === 'string' | ||
); | ||
}; | ||
|
||
const logErrorObject = (error: object | null, context: Context) => { | ||
if (error === null) { | ||
context.log.error('The provided error was eq to null - unable to log a specific error-message'); | ||
return; | ||
} | ||
|
||
if (isErrorWithMessage(error)) { | ||
context.log.error({ message: error.message, stack: error.stack }); | ||
} else { | ||
try { | ||
const errorAsJson = JSON.stringify(error); | ||
if (errorAsJson === '{}') { | ||
context.log.error(error.toString()); | ||
} else { | ||
context.log.error(errorAsJson); // Log the JSON string of the error object | ||
} | ||
} catch (_) { | ||
//Fallback in case there's an error stringify | ||
context.log.error(error.toString()); | ||
} | ||
} | ||
}; | ||
|
||
export const errorHandler = ( | ||
error: unknown, | ||
context: Context, | ||
opts?: Options, | ||
): { | ||
[key: string]: unknown; | ||
} => { | ||
if (error instanceof ApplicationError) { | ||
context.log.error(`Received application error with message ${error.message}`); | ||
return { | ||
status: error.status, | ||
body: error.body, | ||
}; | ||
} | ||
|
||
switch (typeof error) { | ||
case 'string': | ||
context.log.error(error); | ||
break; | ||
case 'object': | ||
logErrorObject(error, context); | ||
break; | ||
default: | ||
context.log(`The error object has a type, that is not suitable for logging: ${typeof error}`); | ||
} | ||
|
||
if (opts?.errorResponseHandler === undefined) { | ||
return { | ||
status: 500, | ||
body: { | ||
message: 'Internal server error', | ||
}, | ||
}; | ||
} else { | ||
return opts.errorResponseHandler(error); | ||
} | ||
}; |
Oops, something went wrong.