@@ -10,16 +10,19 @@ export interface RegularHookMap {
10
10
error ?: RegularMiddleware [ ] ;
11
11
}
12
12
13
- export const runHook = ( hook : RegularMiddleware , context : any , type ?: string ) => {
13
+ export const runHook = (
14
+ hook : RegularMiddleware ,
15
+ context : any ,
16
+ type ?: string ,
17
+ ) => {
14
18
const typeBefore = context . type ;
15
19
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
+ } ) ;
23
26
} ;
24
27
25
28
export const runHooks = ( hooks : RegularMiddleware [ ] ) => ( context : any ) =>
@@ -49,18 +52,25 @@ export function fromErrorHook(hook: RegularMiddleware) {
49
52
delete context . result ;
50
53
}
51
54
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 ;
54
63
throw context . error ;
55
- }
56
- } ) ;
64
+ } ) ;
57
65
} ) ;
58
66
} ;
59
67
}
60
68
61
- export function collect (
62
- { before = [ ] , after = [ ] , error = [ ] } : RegularHookMap ,
63
- ) {
69
+ export function collect ( {
70
+ before = [ ] ,
71
+ after = [ ] ,
72
+ error = [ ] ,
73
+ } : RegularHookMap ) {
64
74
const beforeHooks = before . map ( fromBeforeHook ) ;
65
75
const afterHooks = [ ...after ] . reverse ( ) . map ( fromAfterHook ) ;
66
76
const errorHooks = error . length ? [ fromErrorHook ( runHooks ( error ) ) ] : [ ] ;
0 commit comments