Skip to content
Myr

improve panic information when a catch-all wildcard conflict occurs (#1529)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by 田欧 · committed by GitHub · parent f2cd3fc · Browse files · 2 files+51 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+51 added−2 removed

  • other:+51 added−2 removed

2 files+51 added−2 removed

modified:tree_test.go+42 added
3 unmodified lines
45package gin67import (8 "fmt"9 "reflect"10 "regexp"11 "strings"12 "testing"13)14
639 unmodified lines
654 if rs, ok := recv.(string); !ok || rs != panicMsg {655 t.Fatalf("Expected panic '"+panicMsg+"', got '%v'", recv)656 }657}658659func TestTreeWildcardConflictEx(t *testing.T) {660 conflicts := [...]struct {661 route string662 segPath string663 existPath string664 existSegPath string665 }{666 {"/who/are/foo", "/foo", `/who/are/\*you`, `/\*you`},667 {"/who/are/foo/", "/foo/", `/who/are/\*you`, `/\*you`},668 {"/who/are/foo/bar", "/foo/bar", `/who/are/\*you`, `/\*you`},669 {"/conxxx", "xxx", `/con:tact`, `:tact`},670 {"/conooo/xxx", "ooo", `/con:tact`, `:tact`},671 }672673 for _, conflict := range conflicts {674 // I have to re-create a 'tree', because the 'tree' will be675 // in an inconsistent state when the loop recovers from the676 // panic which threw by 'addRoute' function.677 tree := &node{}678 routes := [...]string{679 "/con:tact",680 "/who/are/*you",681 "/who/foo/hello",682 }683684 for _, route := range routes {685 tree.addRoute(route, fakeHandler(route))686 }687688 recv := catchPanic(func() {689 tree.addRoute(conflict.route, fakeHandler(conflict.route))690 })691692 if !regexp.MustCompile(fmt.Sprintf("'%s' in new path .* conflicts with existing wildcard '%s' in existing prefix '%s'",693 conflict.segPath, conflict.existSegPath, conflict.existPath)).MatchString(fmt.Sprint(recv)) {694 t.Fatalf("invalid wildcard conflict error (%v)", recv)695 }696 }697}
modified:tree.go+9 added-2 removed
191 unmodified lines
192 continue walk193 }194 }195196 panic("path segment '" + path +196 pathSeg := path197 if n.nType != catchAll {198 pathSeg = strings.SplitN(path, "/", 2)[0]199 }200 prefix := fullPath[:strings.Index(fullPath, pathSeg)] + n.path201 panic("'" + pathSeg +202 "' in new path '" + fullPath +203 "' conflicts with existing wildcard '" + n.path +198 "' in path '" + fullPath + "'")204 "' in existing prefix '" + prefix +205 "'")206 }207208 c := path[0]209
418 unmodified lines