Skip to content

Commit ec45681

Browse files
committed
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 🤞
1 parent 68cebb2 commit ec45681

File tree

4 files changed

+16
-22
lines changed

4 files changed

+16
-22
lines changed

caddyconfig/configadapters.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,14 @@ func JSONIndent(val interface{}) ([]byte, error) {
101101
}
102102

103103
// RegisterAdapter registers a config adapter with the given name.
104-
// This should usually be done at init-time.
105-
func RegisterAdapter(name string, adapter Adapter) error {
104+
// This should usually be done at init-time. It panics if the
105+
// adapter cannot be registered successfully.
106+
func RegisterAdapter(name string, adapter Adapter) {
106107
if _, ok := configAdapters[name]; ok {
107-
return fmt.Errorf("%s: already registered", name)
108+
panic(fmt.Errorf("%s: already registered", name))
108109
}
109110
configAdapters[name] = adapter
110-
return caddy.RegisterModule(adapterModule{name, adapter})
111+
caddy.RegisterModule(adapterModule{name, adapter})
111112
}
112113

113114
// GetAdapter returns the adapter with the given name,

modules.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,33 +125,32 @@ type ModuleMap map[string]json.RawMessage
125125
// be properly recorded, this should be called in the
126126
// init phase of runtime. Typically, the module package
127127
// will do this as a side-effect of being imported.
128-
// This function returns an error if the module's info
129-
// is incomplete or invalid, or if the module is
130-
// already registered.
131-
func RegisterModule(instance Module) error {
128+
// This function panics if the module's info is
129+
// incomplete or invalid, or if the module is already
130+
// registered.
131+
func RegisterModule(instance Module) {
132132
mod := instance.CaddyModule()
133133

134134
if mod.ID == "" {
135-
return fmt.Errorf("module ID missing")
135+
panic("module ID missing")
136136
}
137137
if mod.ID == "caddy" || mod.ID == "admin" {
138-
return fmt.Errorf("module ID '%s' is reserved", mod.ID)
138+
panic(fmt.Sprintf("module ID '%s' is reserved", mod.ID))
139139
}
140140
if mod.New == nil {
141-
return fmt.Errorf("missing ModuleInfo.New")
141+
panic("missing ModuleInfo.New")
142142
}
143143
if val := mod.New(); val == nil {
144-
return fmt.Errorf("ModuleInfo.New must return a non-nil module instance")
144+
panic("ModuleInfo.New must return a non-nil module instance")
145145
}
146146

147147
modulesMu.Lock()
148148
defer modulesMu.Unlock()
149149

150150
if _, ok := modules[string(mod.ID)]; ok {
151-
return fmt.Errorf("module already registered: %s", mod.ID)
151+
panic(fmt.Sprintf("module already registered: %s", mod.ID))
152152
}
153153
modules[string(mod.ID)] = mod
154-
return nil
155154
}
156155

157156
// GetModule returns module information from its ID (full name).

modules/caddyhttp/app.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ import (
3030
)
3131

3232
func init() {
33-
err := caddy.RegisterModule(App{})
34-
if err != nil {
35-
caddy.Log().Fatal(err.Error())
36-
}
33+
caddy.RegisterModule(App{})
3734
}
3835

3936
// App is a robust, production-ready HTTP server.

modules/caddyhttp/caddyhttp.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ import (
3030
func init() {
3131
weakrand.Seed(time.Now().UnixNano())
3232

33-
err := caddy.RegisterModule(tlsPlaceholderWrapper{})
34-
if err != nil {
35-
caddy.Log().Fatal(err.Error())
36-
}
33+
caddy.RegisterModule(tlsPlaceholderWrapper{})
3734
}
3835

3936
// RequestMatcher is a type that can match to a request.

0 commit comments

Comments
 (0)