summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Tanlak <philipp.tanlak@gmail.com>2023-08-27 18:04:07 +0200
committerPhilipp Tanlak <philipp.tanlak@gmail.com>2023-08-27 18:04:07 +0200
commitf4408fe641e4b0e27c168bcc9bbc4f69937cefc8 (patch)
treeb3213a6fd0301c8e6046e250be8253aeeb9985ce
parent87c1136438b5f24fcb886b9771dd0245999c3e8a (diff)
improve error logging
-rw-r--r--cmd/flyscrape/watch.go17
-rw-r--r--js.go17
2 files changed, 30 insertions, 4 deletions
diff --git a/cmd/flyscrape/watch.go b/cmd/flyscrape/watch.go
index 46da25e..3d2cf0d 100644
--- a/cmd/flyscrape/watch.go
+++ b/cmd/flyscrape/watch.go
@@ -30,7 +30,17 @@ func (c *WatchCommand) Run(args []string) error {
err := flyscrape.Watch(script, func(s string) error {
opts, scrape, err := flyscrape.Compile(s)
if err != nil {
- log.Println(err)
+ screen.Clear()
+ screen.MoveTopLeft()
+
+ if errs, ok := err.(interface{ Unwrap() []error }); ok {
+ for _, err := range errs.Unwrap() {
+ log.Printf("%s:%v\n", script, err)
+ }
+ } else {
+ log.Println(err)
+ }
+
// ignore compilation errors
return nil
}
@@ -43,13 +53,14 @@ func (c *WatchCommand) Run(args []string) error {
}
result := <-scr.Scrape()
+ screen.Clear()
+ screen.MoveTopLeft()
+
if result.Error != nil {
log.Println(result.Error)
return nil
}
- screen.Clear()
- screen.MoveTopLeft()
fmt.Println(flyscrape.PrettyPrint(result, ""))
return nil
})
diff --git a/js.go b/js.go
index 242f15f..6be1461 100644
--- a/js.go
+++ b/js.go
@@ -14,6 +14,16 @@ import (
v8 "rogchap.com/v8go"
)
+type TransformError struct {
+ Line int
+ Column int
+ Text string
+}
+
+func (err TransformError) Error() string {
+ return fmt.Sprintf("%d:%d: %s", err.Line, err.Column, err.Text)
+}
+
func init() {
rand.Seed(time.Now().UnixNano())
}
@@ -34,7 +44,12 @@ func build(src string) (string, error) {
var errs []error
for _, msg := range res.Errors {
- errs = append(errs, fmt.Errorf("%s", msg.Text))
+ err := TransformError{Text: msg.Text}
+ if msg.Location != nil {
+ err.Line = msg.Location.Line
+ err.Column = msg.Location.Column
+ }
+ errs = append(errs, err)
}
if len(res.Errors) > 0 {
return "", errors.Join(errs...)