mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 21:00:40 -07:00
add and use error classes for undefined variables
cleans up funky message-matching code in expression.ts for lenient.
This commit is contained in:
committed by
Jun Yang
parent
ca18a579a1
commit
d8f9091a12
@@ -12,6 +12,7 @@ import { Context } from '../context/context'
|
||||
import { range, toValue } from '../util/underscore'
|
||||
import { Tokenizer } from '../parser/tokenizer'
|
||||
import { operatorImpls } from '../render/operator'
|
||||
import { UndefinedVariableError, InternalUndefinedVariableError } from '../util/error'
|
||||
|
||||
export class Expression {
|
||||
private operands: any[] = []
|
||||
@@ -49,12 +50,10 @@ export function evalToken (token: Token | undefined, ctx: Context, lenient: bool
|
||||
try {
|
||||
return ctx.get([variable, ...props])
|
||||
} catch (e) {
|
||||
// for lenient, we catch the error thrown by Context.getFromScope() for undefined vars.
|
||||
// Alt, we could make this more robust by setting a flag or using a separate error class.
|
||||
if (lenient && e instanceof TypeError && e.message.startsWith("undefined variable:")) {
|
||||
if (lenient && e instanceof InternalUndefinedVariableError) {
|
||||
return null
|
||||
} else {
|
||||
throw(e)
|
||||
throw(new UndefinedVariableError(e, token))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user