Skip to content

Commit

Permalink
Fix #73: Handle invalid XPath queries
Browse files Browse the repository at this point in the history
  • Loading branch information
sibprogrammer committed Aug 29, 2024
1 parent a0c4e1e commit c3e5967
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func NewRootCmd() *cobra.Command {
}

if err != nil {
fmt.Println(err)
fmt.Println("Error:", err)
os.Exit(1)
}
}()
Expand Down
5 changes: 5 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/antchfx/xmlquery"
Expand Down Expand Up @@ -230,6 +231,10 @@ func XPathQuery(reader io.Reader, writer io.Writer, query string, singleNode boo
}
} else {
expr, _ := xpath.Compile(query)
if expr == nil {
return errors.New("unable to parse the XPath query")
}

val := expr.Evaluate(xmlquery.CreateXPathNavigator(doc))

switch typedVal := val.(type) {
Expand Down

0 comments on commit c3e5967

Please sign in to comment.