Skip to content

Commit

Permalink
Merge pull request #1242 from lhokktyn/fix-async-aftereach
Browse files Browse the repository at this point in the history
fix: support async afterEach hook
  • Loading branch information
YOU54F authored Oct 8, 2024
2 parents 1a3815e + d368c80 commit 96c5e54
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions src/dsl/verifier/proxy/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ export const registerBeforeHook = (
config: ProxyOptions,
stateSetupPath: string
): void => {
if (config.beforeEach) logger.trace("registered 'beforeEach' hook");
app.use(async (req, res, next) => {
if (config.beforeEach !== undefined) {
logger.trace("registered 'beforeEach' hook");
if (req.path === stateSetupPath) {
logger.debug("executing 'beforeEach' hook");
try {
await config.beforeEach();
} catch (e) {
logger.error(`error executing 'beforeEach' hook: ${e.message}`);
logger.debug(`Stack trace was: ${e.stack}`);
next(new Error(`error executing 'beforeEach' hook: ${e.message}`));
}
if (req.path === stateSetupPath && config.beforeEach) {
logger.debug("executing 'beforeEach' hook");
try {
await config.beforeEach();
next();
} catch (e) {
logger.error(`error executing 'beforeEach' hook: ${e.message}`);
logger.debug(`Stack trace was: ${e.stack}`);
next(new Error(`error executing 'beforeEach' hook: ${e.message}`));
}
} else {
next();
}
next();
});
};

Expand All @@ -31,19 +31,17 @@ export const registerAfterHook = (
config: ProxyOptions,
stateSetupPath: string
): void => {
if (config.afterEach) logger.trace("registered 'afterEach' hook");
app.use(async (req, res, next) => {
if (config.afterEach !== undefined) {
logger.trace("registered 'afterEach' hook");
next();
if (req.path !== stateSetupPath) {
logger.debug("executing 'afterEach' hook");
try {
await config.afterEach();
} catch (e) {
logger.error(`error executing 'afterEach' hook: ${e.message}`);
logger.debug(`Stack trace was: ${e.stack}`);
next(new Error(`error executing 'afterEach' hook: ${e.message}`));
}
if (req.path !== stateSetupPath && config.afterEach) {
logger.debug("executing 'afterEach' hook");
try {
await config.afterEach();
next();
} catch (e) {
logger.error(`error executing 'afterEach' hook: ${e.message}`);
logger.debug(`Stack trace was: ${e.stack}`);
next(new Error(`error executing 'afterEach' hook: ${e.message}`));
}
} else {
next();
Expand Down

0 comments on commit 96c5e54

Please sign in to comment.