alecthomas / participle

A parser library for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Improve error message for custom capture

sievlev opened this issue · comments

If custom capture generate an error, then user will receive an error message with full struct name.

 diff --git a/_examples/ini/main.go b/_examples/ini/main.go
 index be6ec9e..e483923 100644
 --- a/_examples/ini/main.go
 +++ b/_examples/ini/main.go
 @@ -1,6 +1,7 @@
  package main
  
  import (
 +       "errors"
         "os"
  
         "github.com/alecthomas/repr"
 @@ -51,11 +52,17 @@ type String struct {
  func (String) value() {}
  
  type Number struct {
 -       Number float64 `@Float`
 +       Number N `@Float`
  }
  
  func (Number) value() {}
  
 +type N struct{}
 +
 +func (*N) Capture(values []string) error {
 +       return errors.New("problem")
 +}
 +

panic: Number.Number: problem

An error message with type name or token name will be much better:
<float>: problem

Mmm yeah, that's not ideal and doesn't match the rest of the error reporting.

It's not a good idea to expose internal details to external users when this library is used for some sort of service. At least the library should provide a way to customize error messages.

See also #253 - an attempt to resurrect v1 behaviour on other place.