Skip to content

Commit 5da75b6

Browse files
committed
Added tests to the handle_error directive
1 parent 8b471e4 commit 5da75b6

File tree

6 files changed

+546
-5
lines changed

6 files changed

+546
-5
lines changed

caddyconfig/httpcaddyfile/builtins.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,6 @@ func parseHandle(h Helper) (caddyhttp.MiddlewareHandler, error) {
754754
}
755755

756756
func parseHandleErrors(h Helper) ([]ConfigValue, error) {
757-
758757
h.Next()
759758
args := h.RemainingArgs()
760759
expression := ""
@@ -775,17 +774,16 @@ func parseHandleErrors(h Helper) ([]ConfigValue, error) {
775774
if len(codes) > 0 {
776775
if expression != "" {
777776
expression += " || "
778-
} else {
779-
expression += "{http.error.status_code} in [" + strings.Join(codes, ", ") + "]"
780777
}
778+
expression += "{http.error.status_code} in [" + strings.Join(codes, ", ") + "]"
781779
}
782-
//Reset cursor position to get ready for ParseSegmentAsSubroute
780+
// Reset cursor position to get ready for ParseSegmentAsSubroute
783781
h.Reset()
784782
h.Next()
785783
h.RemainingArgs()
786784
h.Prev()
787785
} else {
788-
//If no arguments present reset the cursor position to get ready for ParseSegmentAsSubroute
786+
// If no arguments present reset the cursor position to get ready for ParseSegmentAsSubroute
789787
h.Prev()
790788
}
791789

caddyconfig/httpcaddyfile/httptype.go

+3
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,9 @@ func (st *ServerType) serversFromPairings(
780780
sr := val.Value.(*caddyhttp.Subroute)
781781
routeMatcherSet := sr.Routes[0].MatcherSetsRaw
782782
sr.Routes[0].MatcherSetsRaw = []caddy.ModuleMap{}
783+
if routeMatcherSet == nil {
784+
routeMatcherSet = matcherSetsEnc
785+
}
783786
srv.Errors.Routes = appendSubrouteToRouteList(srv.Errors.Routes, sr, routeMatcherSet, p, warnings)
784787
}
785788
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
{
2+
http_port 3010
3+
}
4+
localhost:3010 {
5+
root * /srv
6+
error /private* "Unauthorized" 410
7+
error /hidden* "Not found" 404
8+
9+
handle_errors 4xx {
10+
respond "Error in the [400 .. 499] range"
11+
}
12+
}
13+
----------
14+
{
15+
"apps": {
16+
"http": {
17+
"http_port": 3010,
18+
"servers": {
19+
"srv0": {
20+
"listen": [
21+
":3010"
22+
],
23+
"routes": [
24+
{
25+
"match": [
26+
{
27+
"host": [
28+
"localhost"
29+
]
30+
}
31+
],
32+
"handle": [
33+
{
34+
"handler": "subroute",
35+
"routes": [
36+
{
37+
"handle": [
38+
{
39+
"handler": "vars",
40+
"root": "/srv"
41+
}
42+
]
43+
},
44+
{
45+
"handle": [
46+
{
47+
"error": "Unauthorized",
48+
"handler": "error",
49+
"status_code": 410
50+
}
51+
],
52+
"match": [
53+
{
54+
"path": [
55+
"/private*"
56+
]
57+
}
58+
]
59+
},
60+
{
61+
"handle": [
62+
{
63+
"error": "Not found",
64+
"handler": "error",
65+
"status_code": 404
66+
}
67+
],
68+
"match": [
69+
{
70+
"path": [
71+
"/hidden*"
72+
]
73+
}
74+
]
75+
}
76+
]
77+
}
78+
],
79+
"terminal": true
80+
}
81+
],
82+
"errors": {
83+
"routes": [
84+
{
85+
"match": [
86+
{
87+
"expression": "{http.error.status_code} \u003e= 400 \u0026\u0026 {http.error.status_code} \u003c= 499"
88+
}
89+
],
90+
"handle": [
91+
{
92+
"handler": "subroute",
93+
"routes": [
94+
{
95+
"handle": [
96+
{
97+
"body": "Error in the [400 .. 499] range",
98+
"handler": "static_response"
99+
}
100+
]
101+
}
102+
]
103+
}
104+
],
105+
"terminal": true
106+
}
107+
]
108+
}
109+
}
110+
}
111+
}
112+
}
113+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
{
2+
http_port 2099
3+
}
4+
localhost:2099 {
5+
root * /srv
6+
error /private* "Unauthorized" 410
7+
error /threehundred* "Moved Permanently" 301
8+
error /internalerr* "Internal Server Error" 500
9+
10+
handle_errors 500 3xx {
11+
respond "Error code is equal to 500 or in the [300..399] range"
12+
}
13+
handle_errors 4xx {
14+
respond "Error in the [400 .. 499] range"
15+
}
16+
}
17+
----------
18+
{
19+
"apps": {
20+
"http": {
21+
"http_port": 2099,
22+
"servers": {
23+
"srv0": {
24+
"listen": [
25+
":2099"
26+
],
27+
"routes": [
28+
{
29+
"match": [
30+
{
31+
"host": [
32+
"localhost"
33+
]
34+
}
35+
],
36+
"handle": [
37+
{
38+
"handler": "subroute",
39+
"routes": [
40+
{
41+
"handle": [
42+
{
43+
"handler": "vars",
44+
"root": "/srv"
45+
}
46+
]
47+
},
48+
{
49+
"handle": [
50+
{
51+
"error": "Moved Permanently",
52+
"handler": "error",
53+
"status_code": 301
54+
}
55+
],
56+
"match": [
57+
{
58+
"path": [
59+
"/threehundred*"
60+
]
61+
}
62+
]
63+
},
64+
{
65+
"handle": [
66+
{
67+
"error": "Internal Server Error",
68+
"handler": "error",
69+
"status_code": 500
70+
}
71+
],
72+
"match": [
73+
{
74+
"path": [
75+
"/internalerr*"
76+
]
77+
}
78+
]
79+
},
80+
{
81+
"handle": [
82+
{
83+
"error": "Unauthorized",
84+
"handler": "error",
85+
"status_code": 410
86+
}
87+
],
88+
"match": [
89+
{
90+
"path": [
91+
"/private*"
92+
]
93+
}
94+
]
95+
}
96+
]
97+
}
98+
],
99+
"terminal": true
100+
}
101+
],
102+
"errors": {
103+
"routes": [
104+
{
105+
"match": [
106+
{
107+
"expression": "{http.error.status_code} \u003e= 400 \u0026\u0026 {http.error.status_code} \u003c= 499"
108+
}
109+
],
110+
"handle": [
111+
{
112+
"handler": "subroute",
113+
"routes": [
114+
{
115+
"handle": [
116+
{
117+
"body": "Error in the [400 .. 499] range",
118+
"handler": "static_response"
119+
}
120+
]
121+
}
122+
]
123+
}
124+
],
125+
"terminal": true
126+
},
127+
{
128+
"match": [
129+
{
130+
"expression": "{http.error.status_code} \u003e= 300 \u0026\u0026 {http.error.status_code} \u003c= 399 || {http.error.status_code} in [500]"
131+
}
132+
],
133+
"handle": [
134+
{
135+
"handler": "subroute",
136+
"routes": [
137+
{
138+
"handle": [
139+
{
140+
"body": "Error code is equal to 500 or in the [300..399] range",
141+
"handler": "static_response"
142+
}
143+
]
144+
}
145+
]
146+
}
147+
],
148+
"terminal": true
149+
}
150+
]
151+
}
152+
}
153+
}
154+
}
155+
}
156+
}

0 commit comments

Comments
 (0)