feat: support catching all errors, #220 (#710)

This commit is contained in:
Jun Yang
2024-06-17 22:48:57 +08:00
committed by GitHub
parent 612da52275
commit 3b5627b040
6 changed files with 115 additions and 37 deletions
+8 -3
View File
@@ -1,4 +1,4 @@
import { toPromise, RenderError } from '../util'
import { toPromise, RenderError, LiquidErrors, LiquidError } from '../util'
import { Context } from '../context'
import { Template } from '../template'
import { Emitter, KeepingTypeEmitter, StreamedEmitter, SimpleEmitter } from '../emitters'
@@ -14,6 +14,7 @@ export class Render {
if (!emitter) {
emitter = ctx.opts.keepOutputType ? new KeepingTypeEmitter() : new SimpleEmitter()
}
const errors = []
for (const tpl of templates) {
try {
// if tpl.render supports emitter, it'll return empty `html`
@@ -22,10 +23,14 @@ export class Render {
html && emitter.write(html)
if (emitter['break'] || emitter['continue']) break
} catch (e) {
const err = RenderError.is(e) ? e : new RenderError(e as Error, tpl)
throw err
const err = LiquidError.is(e) ? e : new RenderError(e as Error, tpl)
if (ctx.opts.catchAllErrors) errors.push(err)
else throw err
}
}
if (errors.length) {
throw new LiquidErrors(errors)
}
return emitter.buffer
}
}