8000 why is panic used instead of returning errors? · Issue #92 · antchfx/xpath · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
why is panic used instead of returning errors? #92
Closed
@vzalevsky

Description

@vzalevsky

In build.go in the func (b *builder) processFunctionNode (root *function Node) (query, error) method in the cases: starts-with,ends-with,contains,matches,substring,sum, callbacks are recorded, which then cause panic when called. Why was this done as a panic and is refactoring planned to return errors instead of panic? In our code, we build xpath in advance, and then use it. It is very unpleasant when the service crashes due to errors in callbacks that are called inside xpath. At the moment we are temporarily using recover()

on the example of the "matches" case:

case "matches":
//matches(string , pattern)
if len(root.Args) != 2 {
return nil, errors.New("xpath: matches function must have two parameters")
}
var (
arg1, arg2 query
err error
)
if arg1, err = b.processNode(root.Args[0]); err != nil {
return nil, err
}
if arg2, err = b.processNode(root.Args[1]); err != nil {
return nil, err
}
qyOutput = &functionQuery{Input: b.firstInput, Func: matchesFunc(arg1, arg2)}

func matchesFunc(arg1, arg2 query) func(query, iterator) interface{} {
return func(q query, t iterator) interface{} {
var s string
switch typ := functionArgs(arg1).Evaluate(t).(type) {
case string:
s = typ
case query:
node := typ.Select(t)
if node == nil {
return ""
}
s = node.Value()
}
var pattern string
var ok bool
if pattern, ok = functionArgs(arg2).Evaluate(t).(string); !ok {
panic(errors.New("matches() function second argument type must be string"))
}
re, err := getRegexp(pattern)
if err != nil {
panic(fmt.Errorf("matches() function second argument is not a valid regexp pattern, err: %s", err 536E .Error()))
}
return re.MatchString(s)
}
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0