core: Don't return error on RegisterModule() and RegisterAdapter()

These functions are called at init-time, and their inputs are hard-coded
so there are no environmental or user factors that could make it fail
or succeed; the error return values are often ignored, and when they're
not, they are usually a fatal error anyway. To ensure that a programmer
mistake is not missed, we now panic instead.

Last breaking change 🤞
This commit is contained in:
Matthew Holt 2020-04-13 09:48:54 -06:00
parent 68cebb28d0
commit ec456811bb
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5
4 changed files with 16 additions and 22 deletions

View file

@ -101,13 +101,14 @@ func JSONIndent(val interface{}) ([]byte, error) {
} }
// RegisterAdapter registers a config adapter with the given name. // RegisterAdapter registers a config adapter with the given name.
// This should usually be done at init-time. // This should usually be done at init-time. It panics if the
func RegisterAdapter(name string, adapter Adapter) error { // adapter cannot be registered successfully.
func RegisterAdapter(name string, adapter Adapter) {
if _, ok := configAdapters[name]; ok { if _, ok := configAdapters[name]; ok {
return fmt.Errorf("%s: already registered", name) panic(fmt.Errorf("%s: already registered", name))
} }
configAdapters[name] = adapter configAdapters[name] = adapter
return caddy.RegisterModule(adapterModule{name, adapter}) caddy.RegisterModule(adapterModule{name, adapter})
} }
// GetAdapter returns the adapter with the given name, // GetAdapter returns the adapter with the given name,

View file

@ -125,33 +125,32 @@ type ModuleMap map[string]json.RawMessage
// be properly recorded, this should be called in the // be properly recorded, this should be called in the
// init phase of runtime. Typically, the module package // init phase of runtime. Typically, the module package
// will do this as a side-effect of being imported. // will do this as a side-effect of being imported.
// This function returns an error if the module's info // This function panics if the module's info is
// is incomplete or invalid, or if the module is // incomplete or invalid, or if the module is already
// already registered. // registered.
func RegisterModule(instance Module) error { func RegisterModule(instance Module) {
mod := instance.CaddyModule() mod := instance.CaddyModule()
if mod.ID == "" { if mod.ID == "" {
return fmt.Errorf("module ID missing") panic("module ID missing")
} }
if mod.ID == "caddy" || mod.ID == "admin" { if mod.ID == "caddy" || mod.ID == "admin" {
return fmt.Errorf("module ID '%s' is reserved", mod.ID) panic(fmt.Sprintf("module ID '%s' is reserved", mod.ID))
} }
if mod.New == nil { if mod.New == nil {
return fmt.Errorf("missing ModuleInfo.New") panic("missing ModuleInfo.New")
} }
if val := mod.New(); val == nil { if val := mod.New(); val == nil {
return fmt.Errorf("ModuleInfo.New must return a non-nil module instance") panic("ModuleInfo.New must return a non-nil module instance")
} }
modulesMu.Lock() modulesMu.Lock()
defer modulesMu.Unlock() defer modulesMu.Unlock()
if _, ok := modules[string(mod.ID)]; ok { if _, ok := modules[string(mod.ID)]; ok {
return fmt.Errorf("module already registered: %s", mod.ID) panic(fmt.Sprintf("module already registered: %s", mod.ID))
} }
modules[string(mod.ID)] = mod modules[string(mod.ID)] = mod
return nil
} }
// GetModule returns module information from its ID (full name). // GetModule returns module information from its ID (full name).

View file

@ -30,10 +30,7 @@ import (
) )
func init() { func init() {
err := caddy.RegisterModule(App{}) caddy.RegisterModule(App{})
if err != nil {
caddy.Log().Fatal(err.Error())
}
} }
// App is a robust, production-ready HTTP server. // App is a robust, production-ready HTTP server.

View file

@ -30,10 +30,7 @@ import (
func init() { func init() {
weakrand.Seed(time.Now().UnixNano()) weakrand.Seed(time.Now().UnixNano())
err := caddy.RegisterModule(tlsPlaceholderWrapper{}) caddy.RegisterModule(tlsPlaceholderWrapper{})
if err != nil {
caddy.Log().Fatal(err.Error())
}
} }
// RequestMatcher is a type that can match to a request. // RequestMatcher is a type that can match to a request.