@marcel91200 / caddy Mirror · read-only / commits / 2ce5c652
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 12, 12:00 UTC

core: Fix bug in AppIfConfigured (fix #6336)

Matthew Holt committed May 22, 2024 at 18:47 UTC 2ce5c6526938b604586c987fa3d31117721e40a6
1 file changed +10 -3
context.go
+10 -3
@@ -455,20 +455,27 @@ func (ctx Context) App(name string) (any, error) {
455
456 // AppIfConfigured is like App, but it returns an error if the
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 // 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.
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 + }
466 if app, ok := ctx.cfg.apps[name]; ok {
467 return app, nil
468 }
469 appRaw := ctx.cfg.AppsRaw[name]
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)
472 }
473 return ctx.App(name)
474 }
475
476 +// ErrNotConfigured indicates a module is not configured.
477 +var ErrNotConfigured = fmt.Errorf("module not configured")
478 +
479 // Storage returns the configured Caddy storage implementation.
480 func (ctx Context) Storage() certmagic.Storage {
481 return ctx.cfg.storage