config: don't crash when there is an error parsing interpolation

[GH-282]
This commit is contained in:
Mitchell Hashimoto 2014-09-09 14:28:32 -07:00
parent 62e2743dcc
commit ffcacca191
2 changed files with 12 additions and 3 deletions

View File

@ -2,6 +2,7 @@ package config
import (
"bytes"
"fmt"
"log"
"unicode"
"unicode/utf8"
@ -13,6 +14,8 @@ const lexEOF = 0
// The parser uses the type <prefix>Lex as a lexer. It must provide
// the methods Lex(*<prefix>SymType) int and Error(string).
type exprLex struct {
Err error
input string
pos int
width int
@ -127,5 +130,5 @@ func (x *exprLex) backup() {
// The parser calls this method on a parse error.
func (x *exprLex) Error(s string) {
log.Printf("parse error: %s", s)
x.Err = fmt.Errorf("parse error: %s", s)
}

View File

@ -21,12 +21,18 @@ func ExprParse(v string) (Interpolation, error) {
exprResult = nil
// Parse
exprParse(&exprLex{input: v})
lex := &exprLex{input: v}
exprParse(lex)
// Build up the errors
var err error
if lex.Err != nil {
err = multierror.ErrorAppend(err, lex.Err)
}
if len(exprErrors) > 0 {
err = &multierror.Error{Errors: exprErrors}
err = multierror.ErrorAppend(err, exprErrors...)
}
if err != nil {
exprResult = nil
}