@marcel91200 / caddy Mirror · read-only / commits / ec456811
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 14, 05:15 UTC

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 🤞

Matthew Holt committed Apr 13, 2020 at 09:48 UTC ec456811bb6d61ce32dbe6e4b7580d383f8a4adf
4 files changed +16 -22
caddyconfig/configadapters.go
+5 -4
@@ -101,13 +101,14 @@ func JSONIndent(val interface{}) ([]byte, error) {
101 }
102
103 // 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) {
107 if _, ok := configAdapters[name]; ok {
107 - return fmt.Errorf("%s: already registered", name)
108 + panic(fmt.Errorf("%s: already registered", name))
109 }
110 configAdapters[name] = adapter
110 - return caddy.RegisterModule(adapterModule{name, adapter})
111 + caddy.RegisterModule(adapterModule{name, adapter})
112 }
113
114 // GetAdapter returns the adapter with the given name,
modules.go
+9 -10
@@ -125,33 +125,32 @@ type ModuleMap map[string]json.RawMessage
125 // be properly recorded, this should be called in the
126 // init phase of runtime. Typically, the module package
127 // 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) {
132 mod := instance.CaddyModule()
133
134 if mod.ID == "" {
135 - return fmt.Errorf("module ID missing")
135 + panic("module ID missing")
136 }
137 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))
139 }
140 if mod.New == nil {
141 - return fmt.Errorf("missing ModuleInfo.New")
141 + panic("missing ModuleInfo.New")
142 }
143 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")
145 }
146
147 modulesMu.Lock()
148 defer modulesMu.Unlock()
149
150 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))
152 }
153 modules[string(mod.ID)] = mod
154 - return nil
154 }
155
156 // GetModule returns module information from its ID (full name).
modules/caddyhttp/app.go
+1 -4
@@ -30,10 +30,7 @@ import (
30 )
31
32 func init() {
33 - err := caddy.RegisterModule(App{})
34 - if err != nil {
35 - caddy.Log().Fatal(err.Error())
36 - }
33 + caddy.RegisterModule(App{})
34 }
35
36 // App is a robust, production-ready HTTP server.
modules/caddyhttp/caddyhttp.go
+1 -4
@@ -30,10 +30,7 @@ import (
30 func init() {
31 weakrand.Seed(time.Now().UnixNano())
32
33 - err := caddy.RegisterModule(tlsPlaceholderWrapper{})
34 - if err != nil {
35 - caddy.Log().Fatal(err.Error())
36 - }
33 + caddy.RegisterModule(tlsPlaceholderWrapper{})
34 }
35
36 // RequestMatcher is a type that can match to a request.