using error reporter from environment to handle syntax errors
This commit is contained in:
@@ -3,9 +3,15 @@ public struct Environment {
|
|||||||
public var extensions: [Extension]
|
public var extensions: [Extension]
|
||||||
|
|
||||||
public var loader: Loader?
|
public var loader: Loader?
|
||||||
|
public var errorReporter: ErrorReporter
|
||||||
|
|
||||||
|
public init(loader: Loader? = nil,
|
||||||
|
extensions: [Extension]? = nil,
|
||||||
|
templateClass: Template.Type = Template.self,
|
||||||
|
errorReporter: ErrorReporter = SimpleErrorReporter()) {
|
||||||
|
|
||||||
public init(loader: Loader? = nil, extensions: [Extension]? = nil, templateClass: Template.Type = Template.self) {
|
|
||||||
self.templateClass = templateClass
|
self.templateClass = templateClass
|
||||||
|
self.errorReporter = errorReporter
|
||||||
self.loader = loader
|
self.loader = loader
|
||||||
self.extensions = (extensions ?? []) + [DefaultExtension()]
|
self.extensions = (extensions ?? []) + [DefaultExtension()]
|
||||||
}
|
}
|
||||||
@@ -28,11 +34,21 @@ public struct Environment {
|
|||||||
|
|
||||||
public func renderTemplate(name: String, context: [String: Any]? = nil) throws -> String {
|
public func renderTemplate(name: String, context: [String: Any]? = nil) throws -> String {
|
||||||
let template = try loadTemplate(name: name)
|
let template = try loadTemplate(name: name)
|
||||||
return try template.render(context)
|
return try render(template: template, context: context)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func renderTemplate(string: String, context: [String: Any]? = nil) throws -> String {
|
public func renderTemplate(string: String, context: [String: Any]? = nil) throws -> String {
|
||||||
let template = templateClass.init(templateString: string, environment: self)
|
let template = templateClass.init(templateString: string, environment: self)
|
||||||
|
return try render(template: template, context: context)
|
||||||
|
}
|
||||||
|
|
||||||
|
func render(template: Template, context: [String: Any]?) throws -> String {
|
||||||
|
errorReporter.context = ErrorReporterContext(template: template)
|
||||||
|
do {
|
||||||
return try template.render(context)
|
return try template.render(context)
|
||||||
|
} catch {
|
||||||
|
try errorReporter.report(error: error)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import Spectre
|
import Spectre
|
||||||
import Stencil
|
@testable import Stencil
|
||||||
|
|
||||||
|
|
||||||
func testEnvironment() {
|
func testEnvironment() {
|
||||||
@@ -32,6 +32,46 @@ func testEnvironment() {
|
|||||||
|
|
||||||
try expect(result) == "here"
|
try expect(result) == "here"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func expectedSyntaxError(token: String, template: Template, description: String) -> TemplateSyntaxError {
|
||||||
|
var error = TemplateSyntaxError(description)
|
||||||
|
error.lexeme = Token.block(value: token, at: template.templateString.range(of: token)!)
|
||||||
|
let context = ErrorReporterContext(template: template)
|
||||||
|
error = environment.errorReporter.contextAwareError(error, context: context) as! TemplateSyntaxError
|
||||||
|
print(error)
|
||||||
|
return error
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("throws syntax error on invalid for tag syntax") {
|
||||||
|
let template: Template = "Hello {% for name in %}{{ name }}, {% endfor %}!"
|
||||||
|
let error = expectedSyntaxError(
|
||||||
|
token: "{% for name in %}",
|
||||||
|
template: template,
|
||||||
|
description: "'for' statements should use the following syntax 'for x in y where condition'."
|
||||||
|
)
|
||||||
|
try expect(try environment.renderTemplate(string: template.templateString, context:["names": ["Bob", "Alice"]])).toThrow(error)
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("throws syntax error on missing endfor") {
|
||||||
|
let template: Template = "{% for name in names %}{{ name }}"
|
||||||
|
let error = expectedSyntaxError(
|
||||||
|
token: "{% for name in names %}",
|
||||||
|
template: template,
|
||||||
|
description: "`endfor` was not found."
|
||||||
|
)
|
||||||
|
try expect(try environment.renderTemplate(string: template.templateString, context: ["names": ["Bob", "Alice"]])).toThrow(error)
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("throws syntax error on unknown tag") {
|
||||||
|
let template: Template = "{% for name in names %}{{ name }}{% end %}"
|
||||||
|
let error = expectedSyntaxError(
|
||||||
|
token: "{% end %}",
|
||||||
|
template: template,
|
||||||
|
description: "Unknown template tag 'end'"
|
||||||
|
)
|
||||||
|
try expect(try environment.renderTemplate(string: template.templateString, context: ["names": ["Bob", "Alice"]])).toThrow(error)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,29 +16,5 @@ func testTemplate() {
|
|||||||
try expect(result) == "Hello World"
|
try expect(result) == "Hello World"
|
||||||
}
|
}
|
||||||
|
|
||||||
$0.it("throws syntax error on invalid for tag syntax") {
|
|
||||||
let template: Template = "Hello {% for name in %}{{ name }}, {% endfor %}!"
|
|
||||||
var error = TemplateSyntaxError("'for' statements should use the following syntax 'for x in y where condition'.")
|
|
||||||
error.token = Token.block(value: "{% for name in %}", at: template.templateString.range(of: "{% for name in %}")!)
|
|
||||||
error = error.contextAwareError(templateName: nil, templateContent: template.templateString)!
|
|
||||||
try expect(try template.render(["names": ["Bob", "Alice"]])).toThrow(error)
|
|
||||||
}
|
|
||||||
|
|
||||||
$0.it("throws syntax error on missing endfor") {
|
|
||||||
let template: Template = "{% for name in names %}{{ name }}"
|
|
||||||
var error = TemplateSyntaxError("`endfor` was not found.")
|
|
||||||
error.token = Token.block(value: "{% for name in names %}", at: template.templateString.range(of: "{% for name in names %}")!)
|
|
||||||
error = error.contextAwareError(templateName: nil, templateContent: template.templateString)!
|
|
||||||
try expect(try template.render(["names": ["Bob", "Alice"]])).toThrow(error)
|
|
||||||
}
|
|
||||||
|
|
||||||
$0.it("throws syntax error on unknown tag") {
|
|
||||||
let template: Template = "{% for name in names %}{{ name }}{% end %}"
|
|
||||||
var error = TemplateSyntaxError("Unknown template tag 'end'")
|
|
||||||
error.token = Token.block(value: "{% end %}", at: template.templateString.range(of: "{% end %}")!)
|
|
||||||
error = error.contextAwareError(templateName: nil, templateContent: template.templateString)!
|
|
||||||
try expect(try template.render(["names": ["Bob", "Alice"]])).toThrow(error)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user