mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-29 07:05:13 -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
@@ -3,6 +3,7 @@ import { __assign } from 'tslib'
|
|||||||
import { NormalizedFullOptions, defaultOptions } from '../liquid-options'
|
import { NormalizedFullOptions, defaultOptions } from '../liquid-options'
|
||||||
import { Scope } from './scope'
|
import { Scope } from './scope'
|
||||||
import { isArray, isNil, isString, isFunction, toLiquid } from '../util/underscore'
|
import { isArray, isNil, isString, isFunction, toLiquid } from '../util/underscore'
|
||||||
|
import { InternalUndefinedVariableError } from '../util/error'
|
||||||
|
|
||||||
export class Context {
|
export class Context {
|
||||||
private scopes: Scope[] = [{}]
|
private scopes: Scope[] = [{}]
|
||||||
@@ -42,7 +43,7 @@ export class Context {
|
|||||||
return paths.reduce((scope, path) => {
|
return paths.reduce((scope, path) => {
|
||||||
scope = readProperty(scope, path)
|
scope = readProperty(scope, path)
|
||||||
if (isNil(scope) && this.opts.strictVariables) {
|
if (isNil(scope) && this.opts.strictVariables) {
|
||||||
throw new TypeError(`undefined variable: ${path}`)
|
throw new InternalUndefinedVariableError(path)
|
||||||
}
|
}
|
||||||
return scope
|
return scope
|
||||||
}, scope)
|
}, scope)
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import { Context } from '../context/context'
|
|||||||
import { range, toValue } from '../util/underscore'
|
import { range, toValue } from '../util/underscore'
|
||||||
import { Tokenizer } from '../parser/tokenizer'
|
import { Tokenizer } from '../parser/tokenizer'
|
||||||
import { operatorImpls } from '../render/operator'
|
import { operatorImpls } from '../render/operator'
|
||||||
|
import { UndefinedVariableError, InternalUndefinedVariableError } from '../util/error'
|
||||||
|
|
||||||
export class Expression {
|
export class Expression {
|
||||||
private operands: any[] = []
|
private operands: any[] = []
|
||||||
@@ -49,12 +50,10 @@ export function evalToken (token: Token | undefined, ctx: Context, lenient: bool
|
|||||||
try {
|
try {
|
||||||
return ctx.get([variable, ...props])
|
return ctx.get([variable, ...props])
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// for lenient, we catch the error thrown by Context.getFromScope() for undefined vars.
|
if (lenient && e instanceof InternalUndefinedVariableError) {
|
||||||
// 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:")) {
|
|
||||||
return null
|
return null
|
||||||
} else {
|
} else {
|
||||||
throw(e)
|
throw(new UndefinedVariableError(e, token))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,6 +48,27 @@ export class RenderError extends LiquidError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class UndefinedVariableError extends LiquidError {
|
||||||
|
public constructor (err: Error, token: Token) {
|
||||||
|
super(err, token)
|
||||||
|
this.name = 'UndefinedVariableError'
|
||||||
|
this.message = err.message
|
||||||
|
super.update()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// only used internally; raised where we don't have token information,
|
||||||
|
// so it can't be an UndefinedVariableError.
|
||||||
|
export class InternalUndefinedVariableError extends Error {
|
||||||
|
variableName: string
|
||||||
|
|
||||||
|
public constructor (variableName: string) {
|
||||||
|
super(`undefined variable: ${variableName}`)
|
||||||
|
this.name = 'InternalUndefinedVariableError'
|
||||||
|
this.variableName = variableName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class AssertionError extends Error {
|
export class AssertionError extends Error {
|
||||||
public constructor (message: string) {
|
public constructor (message: string) {
|
||||||
super(message)
|
super(message)
|
||||||
|
|||||||
Reference in New Issue
Block a user