Skip to content

Commit

Permalink
core: Don't return error on RegisterModule() and RegisterAdapter()
Browse files Browse the repository at this point in the history
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 🤞
  • Loading branch information
mholt committed Apr 13, 2020
1 parent 68cebb2 commit ec45681
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 22 deletions.
9 changes: 5 additions & 4 deletions caddyconfig/configadapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,14 @@ func JSONIndent(val interface{}) ([]byte, error) {
}

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

// GetAdapter returns the adapter with the given name,
Expand Down
19 changes: 9 additions & 10 deletions modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,33 +125,32 @@ type ModuleMap map[string]json.RawMessage
// be properly recorded, this should be called in the
// init phase of runtime. Typically, the module package
// will do this as a side-effect of being imported.
// This function returns an error if the module's info
// is incomplete or invalid, or if the module is
// already registered.
func RegisterModule(instance Module) error {
// This function panics if the module's info is
// incomplete or invalid, or if the module is already
// registered.
func RegisterModule(instance Module) {
mod := instance.CaddyModule()

if mod.ID == "" {
return fmt.Errorf("module ID missing")
panic("module ID missing")
}
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 {
return fmt.Errorf("missing ModuleInfo.New")
panic("missing ModuleInfo.New")
}
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()
defer modulesMu.Unlock()

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
return nil
}

// GetModule returns module information from its ID (full name).
Expand Down
5 changes: 1 addition & 4 deletions modules/caddyhttp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ import (
)

func init() {
err := caddy.RegisterModule(App{})
if err != nil {
caddy.Log().Fatal(err.Error())
}
caddy.RegisterModule(App{})
}

// App is a robust, production-ready HTTP server.
Expand Down
5 changes: 1 addition & 4 deletions modules/caddyhttp/caddyhttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ import (
func init() {
weakrand.Seed(time.Now().UnixNano())

err := caddy.RegisterModule(tlsPlaceholderWrapper{})
if err != nil {
caddy.Log().Fatal(err.Error())
}
caddy.RegisterModule(tlsPlaceholderWrapper{})
}

// RequestMatcher is a type that can match to a request.
Expand Down

0 comments on commit ec45681

Please sign in to comment.