Skip to content

Commit 2ce5c65

Browse files
committed
core: Fix bug in AppIfConfigured (fix #6336)
1 parent 61917c3 commit 2ce5c65

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

context.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,20 +455,27 @@ func (ctx Context) App(name string) (any, error) {
455455

456456
// AppIfConfigured is like App, but it returns an error if the
457457
// 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
459459
// 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.
461462
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+
}
462466
if app, ok := ctx.cfg.apps[name]; ok {
463467
return app, nil
464468
}
465469
appRaw := ctx.cfg.AppsRaw[name]
466470
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)
468472
}
469473
return ctx.App(name)
470474
}
471475

476+
// ErrNotConfigured indicates a module is not configured.
477+
var ErrNotConfigured = fmt.Errorf("module not configured")
478+
472479
// Storage returns the configured Caddy storage implementation.
473480
func (ctx Context) Storage() certmagic.Storage {
474481
return ctx.cfg.storage

0 commit comments

Comments
 (0)