0
0
Fork 0

registry: improve error message upon forgotten "type" property

Registering a custom tokenizer while forgetting its "type" used to
return:

  error: unable to determine type

It now says:

  error: cannot resolve 'foo' tokenizer type: 'type' property is not defined
This commit is contained in:
Patrick Mezard 2015-10-27 16:52:26 +01:00
parent 8b17787a65
commit 54a85fa96a
1 changed files with 9 additions and 5 deletions

View File

@ -61,11 +61,15 @@ func NewCache() *Cache {
}
func typeFromConfig(config map[string]interface{}) (string, error) {
typ, ok := config["type"].(string)
if ok {
return typ, nil
prop, ok := config["type"]
if !ok {
return "", fmt.Errorf("'type' property is not defined")
}
return "", fmt.Errorf("unable to determine type")
typ, ok := prop.(string)
if !ok {
return "", fmt.Errorf("'type' property must be a string, not %T", prop)
}
return typ, nil
}
func (c *Cache) CharFilterNamed(name string) (analysis.CharFilter, error) {
@ -87,7 +91,7 @@ func (c *Cache) TokenizerNamed(name string) (analysis.Tokenizer, error) {
func (c *Cache) DefineTokenizer(name string, config map[string]interface{}) (analysis.Tokenizer, error) {
typ, err := typeFromConfig(config)
if err != nil {
return nil, err
return nil, fmt.Errorf("cannot resolve '%s' tokenizer type: %s", name, err)
}
return c.Tokenizers.DefineTokenizer(name, typ, config, c)
}