Skip to content

Commit f8ea48c

Browse files
committed
chore(caddy): better error handling
1 parent 1fbd619 commit f8ea48c

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

caddy/app.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,22 +194,22 @@ func (f *FrankenPHPApp) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
194194

195195
v, err := time.ParseDuration(d.Val())
196196
if err != nil {
197-
return errors.New("max_wait_time must be a valid duration (example: 10s)")
197+
return d.Err("max_wait_time must be a valid duration (example: 10s)")
198198
}
199199

200200
f.MaxWaitTime = v
201201
case "php_ini":
202202
parseIniLine := func(d *caddyfile.Dispenser) error {
203203
key := d.Val()
204204
if !d.NextArg() {
205-
return iniError
205+
return d.WrapErr(iniError)
206206
}
207207
if f.PhpIni == nil {
208208
f.PhpIni = make(map[string]string)
209209
}
210210
f.PhpIni[key] = d.Val()
211211
if d.NextArg() {
212-
return iniError
212+
return d.WrapErr(iniError)
213213
}
214214

215215
return nil
@@ -226,7 +226,7 @@ func (f *FrankenPHPApp) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
226226

227227
if !isBlock {
228228
if !d.NextArg() {
229-
return iniError
229+
return d.WrapErr(iniError)
230230
}
231231
err := parseIniLine(d)
232232
if err != nil {
@@ -243,12 +243,12 @@ func (f *FrankenPHPApp) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
243243
wc.FileName = filepath.Join(frankenphp.EmbeddedAppPath, wc.FileName)
244244
}
245245
if strings.HasPrefix(wc.Name, "m#") {
246-
return fmt.Errorf(`global worker names must not start with "m#": %q`, wc.Name)
246+
return d.Errf(`global worker names must not start with "m#": %q`, wc.Name)
247247
}
248248
// check for duplicate workers
249249
for _, existingWorker := range f.Workers {
250250
if existingWorker.FileName == wc.FileName {
251-
return fmt.Errorf("global workers must not have duplicate filenames: %q", wc.FileName)
251+
return d.Errf("global workers must not have duplicate filenames: %q", wc.FileName)
252252
}
253253
}
254254

@@ -261,7 +261,7 @@ func (f *FrankenPHPApp) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
261261
}
262262

263263
if f.MaxThreads > 0 && f.NumThreads > 0 && f.MaxThreads < f.NumThreads {
264-
return errors.New(`"max_threads"" must be greater than or equal to "num_threads"`)
264+
return d.Err(`"max_threads"" must be greater than or equal to "num_threads"`)
265265
}
266266

267267
return nil
@@ -279,3 +279,8 @@ func parseGlobalOption(d *caddyfile.Dispenser, _ any) (any, error) {
279279
Value: caddyconfig.JSON(app, nil),
280280
}, nil
281281
}
282+
283+
var (
284+
_ caddy.App = (*FrankenPHPApp)(nil)
285+
_ caddy.Provisioner = (*FrankenPHPApp)(nil)
286+
)

caddy/caddy.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import (
77
"fmt"
88

99
"github.com/caddyserver/caddy/v2"
10-
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
1110
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
12-
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
1311
)
1412

1513
const (
@@ -35,12 +33,3 @@ func init() {
3533
func wrongSubDirectiveError(module string, allowedDriectives string, wrongValue string) error {
3634
return fmt.Errorf("unknown '%s' subdirective: '%s' (allowed directives are: %s)", module, wrongValue, allowedDriectives)
3735
}
38-
39-
// Interface guards
40-
var (
41-
_ caddy.App = (*FrankenPHPApp)(nil)
42-
_ caddy.Provisioner = (*FrankenPHPApp)(nil)
43-
_ caddy.Provisioner = (*FrankenPHPModule)(nil)
44-
_ caddyhttp.MiddlewareHandler = (*FrankenPHPModule)(nil)
45-
_ caddyfile.Unmarshaler = (*FrankenPHPModule)(nil)
46-
)

caddy/module.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,8 @@ func (f *FrankenPHPModule) Provision(ctx caddy.Context) error {
7171
}
7272

7373
for i, wc := range f.Workers {
74-
7574
// make the file path absolute from the public directory
76-
// this can only be done if the root is definied inside php_server
75+
// this can only be done if the root is defined inside php_server
7776
if !filepath.IsAbs(wc.FileName) && f.Root != "" {
7877
wc.FileName = filepath.Join(f.Root, wc.FileName)
7978
}
@@ -602,7 +601,7 @@ func prependWorkerRoutes(routes caddyhttp.RouteList, h httpcaddyfile.Helper, f F
602601
// forward matching routes to the PHP handler
603602
routes = append(routes, caddyhttp.Route{
604603
MatcherSetsRaw: []caddy.ModuleMap{
605-
caddy.ModuleMap{"path": h.JSON(allWorkerMatches)},
604+
{"path": h.JSON(allWorkerMatches)},
606605
},
607606
HandlersRaw: []json.RawMessage{
608607
caddyconfig.JSONModuleObject(f, "handler", "php", nil),
@@ -611,3 +610,10 @@ func prependWorkerRoutes(routes caddyhttp.RouteList, h httpcaddyfile.Helper, f F
611610

612611
return routes
613612
}
613+
614+
// Interface guards
615+
var (
616+
_ caddy.Provisioner = (*FrankenPHPModule)(nil)
617+
_ caddyhttp.MiddlewareHandler = (*FrankenPHPModule)(nil)
618+
_ caddyfile.Unmarshaler = (*FrankenPHPModule)(nil)
619+
)

caddy/workerconfig.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func parseWorkerConfig(d *caddyfile.Dispenser) (workerConfig, error) {
5959
}
6060

6161
if d.NextArg() {
62-
return wc, errors.New(`FrankenPHP: too many "worker" arguments: ` + d.Val())
62+
return wc, d.Errf(`FrankenPHP: too many "worker" arguments: %s`, d.Val())
6363
}
6464

6565
for d.NextBlock(1) {

0 commit comments

Comments
 (0)