AppIfConfigured is like App, but it returns an error if the app has not been configured. This is useful when the app is required and its absence is a configuration error; or when the app is optional and you don't want to instantiate a new one that hasn't been explicitly configured. If the app is not
(name string)
| 528 | // new one that hasn't been explicitly configured. If the app |
| 529 | // is not in the configuration, the error wraps ErrNotConfigured. |
| 530 | func (ctx Context) AppIfConfigured(name string) (any, error) { |
| 531 | if ctx.cfg == nil { |
| 532 | return nil, fmt.Errorf("app module %s: %w", name, ErrNotConfigured) |
| 533 | } |
| 534 | // if the app failed to load before, return the cached error |
| 535 | if err, ok := ctx.cfg.failedApps[name]; ok { |
| 536 | return nil, fmt.Errorf("loading %s app module: %v", name, err) |
| 537 | } |
| 538 | if app, ok := ctx.cfg.apps[name]; ok { |
| 539 | return app, nil |
| 540 | } |
| 541 | appRaw := ctx.cfg.AppsRaw[name] |
| 542 | if appRaw == nil { |
| 543 | return nil, fmt.Errorf("app module %s: %w", name, ErrNotConfigured) |
| 544 | } |
| 545 | return ctx.App(name) |
| 546 | } |
| 547 | |
| 548 | // ErrNotConfigured indicates a module is not configured. |
| 549 | var ErrNotConfigured = fmt.Errorf("module not configured") |