@@ -455,20 +455,27 @@ func (ctx Context) App(name string) (any, error) {
455
455
456
456
// AppIfConfigured is like App, but it returns an error if the
457
457
// app has not been configured. This is useful when the app is
458
- // required and its absence is a configuration error, or when
458
+ // required and its absence is a configuration error; or when
459
459
// the app is optional and you don't want to instantiate a
460
- // new one that hasn't been explicitly configured.
460
+ // new one that hasn't been explicitly configured. If the app
461
+ // is not in the configuration, the error wraps ErrNotConfigured.
461
462
func (ctx Context ) AppIfConfigured (name string ) (any , error ) {
463
+ if ctx .cfg == nil {
464
+ return nil , fmt .Errorf ("app module %s: %w" , name , ErrNotConfigured )
465
+ }
462
466
if app , ok := ctx .cfg .apps [name ]; ok {
463
467
return app , nil
464
468
}
465
469
appRaw := ctx .cfg .AppsRaw [name ]
466
470
if appRaw == nil {
467
- return nil , fmt .Errorf ("app module %s is not configured " , name )
471
+ return nil , fmt .Errorf ("app module %s: %w " , name , ErrNotConfigured )
468
472
}
469
473
return ctx .App (name )
470
474
}
471
475
476
+ // ErrNotConfigured indicates a module is not configured.
477
+ var ErrNotConfigured = fmt .Errorf ("module not configured" )
478
+
472
479
// Storage returns the configured Caddy storage implementation.
473
480
func (ctx Context ) Storage () certmagic.Storage {
474
481
return ctx .cfg .storage
0 commit comments