Skip to content

Commit d148869

Browse files
authored
fix(hooks): Update context.error in error hooks (#107)
1 parent c14d07c commit d148869

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

main/hooks/src/regular.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@ export interface RegularHookMap {
1010
error?: RegularMiddleware[];
1111
}
1212

13-
export const runHook = (hook: RegularMiddleware, context: any, type?: string) => {
13+
export const runHook = (
14+
hook: RegularMiddleware,
15+
context: any,
16+
type?: string,
17+
) => {
1418
const typeBefore = context.type;
1519
if (type) context.type = type;
16-
return Promise.resolve(hook.call(context.self, context))
17-
.then((res: any) => {
18-
if (type) context.type = typeBefore;
19-
if (res && res !== context) {
20-
Object.assign(context, res);
21-
}
22-
});
20+
return Promise.resolve(hook.call(context.self, context)).then((res: any) => {
21+
if (type) context.type = typeBefore;
22+
if (res && res !== context) {
23+
Object.assign(context, res);
24+
}
25+
});
2326
};
2427

2528
export const runHooks = (hooks: RegularMiddleware[]) => (context: any) =>
@@ -49,18 +52,25 @@ export function fromErrorHook(hook: RegularMiddleware) {
4952
delete context.result;
5053
}
5154

52-
return runHook(hook, context, 'error').then(() => {
53-
if (context.result === undefined && context.error !== undefined) {
55+
return runHook(hook, context, 'error')
56+
.then(() => {
57+
if (context.result === undefined && context.error !== undefined) {
58+
throw context.error;
59+
}
60+
})
61+
.catch((error) => {
62+
context.error = error;
5463
throw context.error;
55-
}
56-
});
64+
});
5765
});
5866
};
5967
}
6068

61-
export function collect(
62-
{ before = [], after = [], error = [] }: RegularHookMap,
63-
) {
69+
export function collect({
70+
before = [],
71+
after = [],
72+
error = [],
73+
}: RegularHookMap) {
6474
const beforeHooks = before.map(fromBeforeHook);
6575
const afterHooks = [...after].reverse().map(fromAfterHook);
6676
const errorHooks = error.length ? [fromErrorHook(runHooks(error))] : [];

0 commit comments

Comments
 (0)