Skip to content

Commit

Permalink
added optional status code argument to handle_errors directive
Browse files Browse the repository at this point in the history
  • Loading branch information
Aziz Rmadi authored and armadi1809 committed Dec 5, 2023
1 parent 4173e2c commit 3597097
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
50 changes: 49 additions & 1 deletion caddyconfig/httpcaddyfile/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,10 +754,58 @@ func parseHandle(h Helper) (caddyhttp.MiddlewareHandler, error) {
}

func parseHandleErrors(h Helper) ([]ConfigValue, error) {
subroute, err := ParseSegmentAsSubroute(h)

Check failure on line 757 in caddyconfig/httpcaddyfile/builtins.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

File is not `gofumpt`-ed (gofumpt)
h.Next()
args := h.RemainingArgs()
expression := ""
if len(args) > 0 {
expression = ""
codes := []string{}
for _, val := range args {
if strings.HasSuffix(val, "xx") {
val = val[:1]
if expression != "" {
expression += " || "
}
expression += fmt.Sprintf("{http.error.status_code} >= %s00 && {http.error.status_code} <= %s99", val, val)
} else {
codes = append(codes, val)
}
}
if len(codes) > 0 {
if expression != "" {
expression += " || "
} else {
expression += "{http.error.status_code} in [" + strings.Join(codes, ", ") + "]"
}
}
//Reset cursor position to get ready for ParseSegmentAsSubroute

Check failure on line 782 in caddyconfig/httpcaddyfile/builtins.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

File is not `gofumpt`-ed (gofumpt)
h.Reset()
h.Next()
h.RemainingArgs()
h.Prev()
} else {
//If no arguments present reset the cursor position to get ready for ParseSegmentAsSubroute

Check failure on line 788 in caddyconfig/httpcaddyfile/builtins.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

File is not `gofumpt`-ed (gofumpt)
h.Prev()
}

handler, err := ParseSegmentAsSubroute(h)
if err != nil {
return nil, err
}
subroute, ok := handler.(*caddyhttp.Subroute)
if !ok {
return nil, h.Errf("segment was not parsed as a subroute")
}

if expression != "" {
statusMatcher := caddy.ModuleMap{
"expression": h.JSON(caddyhttp.MatchExpression{Expr: expression}),
}
for i := range subroute.Routes {
subroute.Routes[i].MatcherSetsRaw = []caddy.ModuleMap{statusMatcher}
}
}
return []ConfigValue{
{
Class: "error_route",
Expand Down
1 change: 1 addition & 0 deletions caddyconfig/httpcaddyfile/directives.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ var directiveOrder = []string{
"handle",
"handle_path",
"route",
"handle_errors",

// handlers that typically respond to requests
"abort",
Expand Down
11 changes: 10 additions & 1 deletion caddyconfig/httpcaddyfile/httptype.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,9 +769,18 @@ func (st *ServerType) serversFromPairings(
if srv.Errors == nil {
srv.Errors = new(caddyhttp.HTTPErrorConfig)
}
sort.SliceStable(errorSubrouteVals, func(i, j int) bool {
sri, srj := errorSubrouteVals[i].Value.(*caddyhttp.Subroute), errorSubrouteVals[j].Value.(*caddyhttp.Subroute)
if len(sri.Routes[0].MatcherSetsRaw) == 0 && len(srj.Routes[0].MatcherSetsRaw) != 0 {
return false
}
return true
})
for _, val := range errorSubrouteVals {
sr := val.Value.(*caddyhttp.Subroute)
srv.Errors.Routes = appendSubrouteToRouteList(srv.Errors.Routes, sr, matcherSetsEnc, p, warnings)
routeMatcherSet := sr.Routes[0].MatcherSetsRaw
sr.Routes[0].MatcherSetsRaw = []caddy.ModuleMap{}
srv.Errors.Routes = appendSubrouteToRouteList(srv.Errors.Routes, sr, routeMatcherSet, p, warnings)
}
}

Expand Down

0 comments on commit 3597097

Please sign in to comment.