Skip to content

Commit

Permalink
fix: unecessary error wrapping in browser bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Aug 22, 2023
1 parent 96f136c commit 9bbfa3c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class Parser {
}
return new HTML(token)
} catch (e) {
if (e instanceof LiquidError) throw e
if (LiquidError.is(e)) throw e
throw new ParseError(e as Error, token)
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/util/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import * as _ from './underscore'
import { Token } from '../tokens/token'
import { Template } from '../template/template'

/**
* targeting ES5, extends Error won't create a proper prototype chain, need a trait to kee track of classes
*/
const TRAIT = '__liquidClass__'

export abstract class LiquidError extends Error {
private token!: Token
public context = ''
Expand All @@ -14,6 +19,7 @@ export abstract class LiquidError extends Error {
super(typeof err === 'string' ? err : err.message)
if (typeof err !== 'string') Object.defineProperty(this, 'originalError', { value: err, enumerable: false })
Object.defineProperty(this, 'token', { value: token, enumerable: false })
Object.defineProperty(this, TRAIT, { value: 'LiquidError', enumerable: false })
}
protected update () {
Object.defineProperty(this, 'context', { value: mkContext(this.token), enumerable: false })
Expand All @@ -22,6 +28,9 @@ export abstract class LiquidError extends Error {
'\n' + this.stack
if (this.originalError) this.stack += '\nFrom ' + this.originalError.stack
}
static is (obj: unknown): obj is LiquidError {
return obj?.[TRAIT] === 'LiquidError'
}
}

export class TokenizationError extends LiquidError {
Expand Down
14 changes: 14 additions & 0 deletions test/e2e/browser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,18 @@ describe('browser', function () {
message: 'output "{{huh" not closed, line:1, col:1'
})
})
it('should throw tokenization error for invalid filter syntax', async () => {
const engine = new LiquidUMD()
const message = 'expected filter name, line:1, col:10'
const stack = [
'>> 1| {{ foo | ^ }}',
' ^',
`TokenizationError: ${message}`
].join('\n')
await expect(engine.parseAndRender('{{ foo | ^ }}')).rejects.toMatchObject({
message,
stack: expect.stringContaining(stack),
name: 'TokenizationError'
})
})
})

0 comments on commit 9bbfa3c

Please sign in to comment.