File tree Expand file tree Collapse file tree 2 files changed +19
-13
lines changed
packages/@vuepress/core/lib/node Expand file tree Collapse file tree 2 files changed +19
-13
lines changed Original file line number Diff line number Diff line change @@ -289,14 +289,19 @@ module.exports = class Page {
289
289
const enhancerPromises = [ ]
290
290
291
291
for ( const { name : pluginName , value : enhancer } of enhancers ) {
292
- try {
293
- enhancerPromises . push ( enhancer ( this ) )
294
- } catch ( error ) {
295
- console . log ( error )
296
- throw new Error ( `[${ pluginName } ] execute extendPageData failed.` )
297
- }
292
+ const enhancerResult = enhancer ( this )
293
+ const promise = enhancerResult instanceof Promise ? enhancerResult : Promise . resolve ( enhancerResult )
294
+
295
+ enhancerPromises . push ( promise . catch ( e => {
296
+ console . log ( e )
297
+ return new Error ( `[${ pluginName } ] execute extendPageData failed.` )
298
+ } ) )
298
299
}
299
300
300
- return Promise . all ( enhancerPromises )
301
+ const results = await Promise . all ( enhancerPromises )
302
+ const enhancerError = results . find ( result => result instanceof Error )
303
+ if ( enhancerError ) throw enhancerError
304
+
305
+ return results
301
306
}
302
307
}
Original file line number Diff line number Diff line change @@ -111,11 +111,11 @@ describe('Page', () => {
111
111
page = new Page ( { path : '/' } , app )
112
112
enhancers = [
113
113
{
114
- pluginName : 'foo' ,
114
+ name : 'foo' ,
115
115
value : jest . fn ( )
116
116
} ,
117
117
{
118
- pluginName : 'foo ' ,
118
+ name : 'bar ' ,
119
119
value : jest . fn ( )
120
120
}
121
121
]
@@ -130,21 +130,22 @@ describe('Page', () => {
130
130
131
131
test ( 'should loop over sync and async enhancers' , async ( ) => {
132
132
const mixedEnhancers = [ ...enhancers , {
133
- pluginName : 'blog' ,
133
+ name : 'blog' ,
134
134
value : jest . fn ( ) . mockResolvedValue ( { } )
135
135
} ]
136
136
await page . enhance ( mixedEnhancers )
137
137
138
138
return mixedEnhancers . map ( enhancer => expect ( enhancer . value ) . toHaveBeenCalled ( ) )
139
139
} )
140
140
141
- test ( 'should log when enhancing when failing ' , async ( ) => {
141
+ test ( 'should log and throw an error when enhancing fails ' , async ( ) => {
142
142
const error = { errorMessage : 'this is an error message' }
143
+ const pluginName = 'error-plugin'
143
144
144
145
await expect ( page . enhance ( [ {
145
- pluginName : 'error-plugin' ,
146
+ name : pluginName ,
146
147
value : jest . fn ( ) . mockRejectedValue ( error )
147
- } ] ) ) . rejects . toThrow ( )
148
+ } ] ) ) . rejects . toThrowError ( `[ ${ pluginName } ] execute extendPageData failed.` )
148
149
149
150
expect ( console . log ) . toHaveBeenCalledWith ( error )
150
151
} )
You can’t perform that action at this time.
0 commit comments