@@ -12,6 +12,11 @@ import type {
12
12
import type { HardhatPlugin } from "../types/plugins.js" ;
13
13
import type { LastParameter , Return } from "../types/utils.js" ;
14
14
15
+ import {
16
+ HardhatError ,
17
+ assertHardhatInvariant ,
18
+ } from "@nomicfoundation/hardhat-errors" ;
19
+
15
20
export class HookManagerImplementation implements HookManager {
16
21
readonly #pluginsInReverseOrder: HardhatPlugin [ ] ;
17
22
@@ -108,10 +113,10 @@ export class HookManagerImplementation implements HookManager {
108
113
109
114
let handlerParams : Parameters < typeof defaultImplementation > ;
110
115
if ( hookCategoryName !== "config" ) {
111
- // TODO: assert that this.#context is not undefinded
112
- if ( this . #context === undefined ) {
113
- throw new Error ( ` Context must be set before running non-config hooks` ) ;
114
- }
116
+ assertHardhatInvariant (
117
+ this . #context !== undefined ,
118
+ " Context must be set before running non-config hooks" ,
119
+ ) ;
115
120
116
121
handlerParams = [ this . #context, ...params ] as any ;
117
122
} else {
@@ -147,10 +152,10 @@ export class HookManagerImplementation implements HookManager {
147
152
148
153
let handlerParams : any ;
149
154
if ( hookCategoryName !== "config" ) {
150
- // TODO: assert that this.#context is not undefinded
151
- if ( this . #context === undefined ) {
152
- throw new Error ( ` Context must be set before running non-config hooks` ) ;
153
- }
155
+ assertHardhatInvariant (
156
+ this . #context !== undefined ,
157
+ " Context must be set before running non-config hooks" ,
158
+ ) ;
154
159
155
160
handlerParams = [ this . #context, ...params ] ;
156
161
} else {
@@ -180,10 +185,10 @@ export class HookManagerImplementation implements HookManager {
180
185
181
186
let handlerParams : any ;
182
187
if ( hookCategoryName !== "config" ) {
183
- // TODO: assert that this.#context is not undefinded
184
- if ( this . #context === undefined ) {
185
- throw new Error ( ` Context must be set before running non-config hooks` ) ;
186
- }
188
+ assertHardhatInvariant (
189
+ this . #context !== undefined ,
190
+ " Context must be set before running non-config hooks" ,
191
+ ) ;
187
192
188
193
handlerParams = [ this . #context, ...params ] ;
189
194
} else {
@@ -288,30 +293,31 @@ export class HookManagerImplementation implements HookManager {
288
293
path : string ,
289
294
) : Promise < Partial < HardhatHooks [ HookCategoryNameT ] > > {
290
295
if ( ! path . startsWith ( "file://" ) ) {
291
- throw new Error (
292
- `Plugin ${ pluginId } hook factory for ${ hookCategoryName } is not a valid file:// URL: ${ path } ` ,
296
+ throw new HardhatError (
297
+ HardhatError . ERRORS . HOOKS . INVALID_HOOK_FACTORY_PATH ,
298
+ {
299
+ pluginId,
300
+ hookCategoryName,
301
+ path,
302
+ } ,
293
303
) ;
294
304
}
295
305
296
306
const mod = await import ( path ) ;
297
307
298
308
const factory = mod . default ;
299
309
300
- // TODO: Assert that the factory is a function
301
- if ( typeof factory !== "function" ) {
302
- throw new Error (
303
- `Plugin ${ pluginId } doesn't export a hook factory for category ${ hookCategoryName } in ${ path } ` ,
304
- ) ;
305
- }
310
+ assertHardhatInvariant (
311
+ typeof factory === "function" ,
312
+ `Plugin ${ pluginId } doesn't export a hook factory for category ${ hookCategoryName } in ${ path } ` ,
313
+ ) ;
306
314
307
315
const category = await factory ( ) ;
308
316
309
- // TODO: Assert that category is not undefined and it's an object -- should use !isObject(category)
310
- if ( typeof category !== "object" || category === null ) {
311
- throw new Error (
312
- `Plugin ${ pluginId } doesn't export a valid factory for category ${ hookCategoryName } in ${ path } , it didn't return an object` ,
313
- ) ;
314
- }
317
+ assertHardhatInvariant (
318
+ category !== null && typeof category === "object" ,
319
+ `Plugin ${ pluginId } doesn't export a valid factory for category ${ hookCategoryName } in ${ path } , it didn't return an object` ,
320
+ ) ;
315
321
316
322
return category ;
317
323
}
0 commit comments