@marcel91200 / caddy Mirror · read-only / commits / 51f125bd
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

caddyfile: Better error message for missing site block braces (#4301)

Some new users mistakenly try to define two sites without braces around each. Doing this can yield a confusing error message saying that their site address is an "unknown directive". We can do better by keeping track of whether the current site block was parsed with or without a brace, then changing the error message later based on that. For example, now this invalid config: ``` foo.example.com respond "foo" bar.example.com respond "bar" ``` Will yield this error message: ``` $ caddy adapt 2021/08/22 19:21:31.028 INFO using adjacent Caddyfile adapt: Caddyfile:4: unrecognized directive: bar.example.com Did you mean to define a second site? If so, you must use curly braces around each site to separate their configurations. ```

Francis Lavoie committed Aug 23, 2021 at 13:53 UTC 51f125bd44be41d6220f0c134b6a402a85f80ad6
2 files changed +14 -3
caddyconfig/caddyfile/parse.go
+9 -2
@@ -211,6 +211,12 @@ func (p *parser) addresses() error {
211 if expectingAnother {
212 return p.Errf("Expected another address but had '%s' - check for extra comma", tkn)
213 }
214 + // Mark this server block as being defined with braces.
215 + // This is used to provide a better error message when
216 + // the user may have tried to define two server blocks
217 + // without having used braces, which are required in
218 + // that case.
219 + p.block.HasBraces = true
220 break
221 }
222
@@ -571,8 +577,9 @@ func (p *parser) snippetTokens() ([]Token, error) {
577 // head of the server block with tokens, which are
578 // grouped by segments.
579 type ServerBlock struct {
574 - Keys []string
575 - Segments []Segment
580 + HasBraces bool
581 + Keys []string
582 + Segments []Segment
583 }
584
585 // DispenseDirective returns a dispenser that contains
caddyconfig/httpcaddyfile/httptype.go
+5 -1
@@ -170,7 +170,11 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
170 dirFunc, ok := registeredDirectives[dir]
171 if !ok {
172 tkn := segment[0]
173 - return nil, warnings, fmt.Errorf("%s:%d: unrecognized directive: %s", tkn.File, tkn.Line, dir)
173 + message := "%s:%d: unrecognized directive: %s"
174 + if !sb.block.HasBraces {
175 + message += "\nDid you mean to define a second site? If so, you must use curly braces around each site to separate their configurations."
176 + }
177 + return nil, warnings, fmt.Errorf(message, tkn.File, tkn.Line, dir)
178 }
179
180 h := Helper{