@@ -7,7 +7,6 @@ package httprouter
7
7
import (
8
8
"strings"
9
9
"unicode"
10
- "fmt"
11
10
)
12
11
13
12
func min (a , b int ) int {
@@ -79,6 +78,7 @@ func (n *node) incrementChildPrio(pos int) int {
79
78
// addRoute adds a node with the given handle to the path.
80
79
// Not concurrency-safe!
81
80
func (n * node ) addRoute (path string , handle Handle ) {
81
+ fullPath := path
82
82
n .priority ++
83
83
numParams := countParams (path )
84
84
@@ -148,7 +148,9 @@ func (n *node) addRoute(path string, handle Handle) {
148
148
}
149
149
}
150
150
151
- panic (fmt .Sprintf ("conflict with wildcard route. has %q got %q" , path , n .path ))
151
+ panic ("path segment '" + path +
152
+ "' conflicts with existing wildcard '" + n .path +
153
+ "' in path '" + fullPath + "'" )
152
154
}
153
155
154
156
c := path [0 ]
@@ -180,23 +182,23 @@ func (n *node) addRoute(path string, handle Handle) {
180
182
n .incrementChildPrio (len (n .indices ) - 1 )
181
183
n = child
182
184
}
183
- n .insertChild (numParams , path , handle )
185
+ n .insertChild (numParams , path , fullPath , handle )
184
186
return
185
187
186
188
} else if i == len (path ) { // Make node a (in-path) leaf
187
189
if n .handle != nil {
188
- panic (fmt . Sprintf ( "a Handle is already registered for this path (%q)" , path ) )
190
+ panic ("a handle is already registered for path ''" + fullPath + "'" )
189
191
}
190
192
n .handle = handle
191
193
}
192
194
return
193
195
}
194
196
} else { // Empty tree
195
- n .insertChild (numParams , path , handle )
197
+ n .insertChild (numParams , path , fullPath , handle )
196
198
}
197
199
}
198
200
199
- func (n * node ) insertChild (numParams uint8 , path string , handle Handle ) {
201
+ func (n * node ) insertChild (numParams uint8 , path , fullPath string , handle Handle ) {
200
202
var offset int // already handled bytes of the path
201
203
202
204
// find prefix until first wildcard (beginning with ':'' or '*'')
@@ -206,27 +208,29 @@ func (n *node) insertChild(numParams uint8, path string, handle Handle) {
206
208
continue
207
209
}
208
210
209
- // check if this Node existing children which would be
210
- // unreachable if we insert the wildcard here
211
- if len (n .children ) > 0 {
212
- panic ("wildcard route conflicts with existing children" )
213
- }
214
-
215
211
// find wildcard end (either '/' or path end)
216
212
end := i + 1
217
213
for end < max && path [end ] != '/' {
218
214
switch path [end ] {
219
215
// the wildcard name must not contain ':' and '*'
220
216
case ':' , '*' :
221
- panic ("only one wildcard per path segment is allowed" )
217
+ panic ("only one wildcard per path segment is allowed, has: '" +
218
+ path [i :] + "' in path '" + fullPath + "'" )
222
219
default :
223
220
end ++
224
221
}
225
222
}
226
223
224
+ // check if this Node existing children which would be
225
+ // unreachable if we insert the wildcard here
226
+ if len (n .children ) > 0 {
227
+ panic ("wildcard route '" + path [i :end ] +
228
+ "' conflicts with existing children in path '" + fullPath + "'" )
229
+ }
230
+
227
231
// check if the wildcard has a name
228
232
if end - i < 2 {
229
- panic ("wildcards must be named with a non-empty name" )
233
+ panic ("wildcards must be named with a non-empty name in path '" + fullPath + "' " )
230
234
}
231
235
232
236
if c == ':' { // param
@@ -262,17 +266,17 @@ func (n *node) insertChild(numParams uint8, path string, handle Handle) {
262
266
263
267
} else { // catchAll
264
268
if end != max || numParams > 1 {
265
- panic ("catch-all routes are only allowed at the end of the path" )
269
+ panic ("catch-all routes are only allowed at the end of the path in path '" + fullPath + "' " )
266
270
}
267
271
268
272
if len (n .path ) > 0 && n .path [len (n .path )- 1 ] == '/' {
269
- panic ("catch-all conflicts with existing handle for the path segment root" )
273
+ panic ("catch-all conflicts with existing handle for the path segment root in path '" + fullPath + "' " )
270
274
}
271
275
272
276
// currently fixed width 1 for '/'
273
277
i --
274
278
if path [i ] != '/' {
275
- panic ("no / before catch-all" )
279
+ panic ("no / before catch-all in path '" + fullPath + "' " )
276
280
}
277
281
278
282
n .path = path [offset :i ]
@@ -397,7 +401,7 @@ walk: // Outer loop for walking the tree
397
401
return
398
402
399
403
default :
400
- panic ("Invalid node type" )
404
+ panic ("invalid node type" )
401
405
}
402
406
}
403
407
} else if path == n .path {
@@ -508,7 +512,7 @@ func (n *node) findCaseInsensitivePath(path string, fixTrailingSlash bool) (ciPa
508
512
return append (ciPath , path ... ), true
509
513
510
514
default :
511
- panic ("Invalid node type" )
515
+ panic ("invalid node type" )
512
516
}
513
517
} else {
514
518
// We should have reached the node containing the handle.
0 commit comments