Skip to content

Commit

Permalink
refactor: panic 内容从 error 改为 string
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed May 26, 2024
1 parent 2e98033 commit d728a9b
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 5 deletions.
2 changes: 1 addition & 1 deletion internal/syntax/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (i *Interceptors) Add(f InterceptorFunc, name ...string) {

for _, n := range name {
if _, found := i.funcs[n]; found {
panic(fmt.Errorf("%s 已经存在", n))
panic(fmt.Sprintf("%s 已经存在", n))
}
i.funcs[n] = f
}
Expand Down
2 changes: 1 addition & 1 deletion internal/tree/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (n *node[T]) priority() int {
}

// 获取指定路径下的节点,若节点不存在,则添加。
// segments 为被 syntax.Split 拆分之后的字符串数组。
// segments 为被 [syntax.Split] 拆分之后的字符串数组。
func (n *node[T]) getNode(segments []*syntax.Segment) (*node[T], error) {
child, err := n.addSegment(segments[0])
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions internal/tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ func (tree *Tree[T]) checkAmbiguous(pattern string) error {
s = n.segment.Value + s
n = n.parent
}

return fmt.Errorf("存在有歧义的节点:%s", s)
return fmt.Errorf("%s 与已有的节点 %s 存在歧义", pattern, s)
}

// Clean 清除路由项
Expand Down
2 changes: 1 addition & 1 deletion internal/tree/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (t *tester) add(method, pattern string, code int) {
func (t *tester) addAmbiguous(pattern string) {
t.a.TB().Helper()
b := rest.BuildHandler(t.a, http.StatusOK, "", nil)
t.a.ErrorString(t.tree.Add(pattern, b, nil, http.MethodGet), "存在有歧义的节点")
t.a.ErrorString(t.tree.Add(pattern, b, nil, http.MethodGet), pattern+" 与已有的节点")
}

// 验证按照指定的 method 和 path 访问,是否会返回相同的 code 值,
Expand Down

0 comments on commit d728a9b

Please sign in to comment.