using error reporter from environment to handle syntax errors

This commit is contained in:
Ilya Puchka
2017-10-07 21:01:28 +02:00
parent 0edb38588d
commit d5f0be959f
3 changed files with 60 additions and 28 deletions

View File

@@ -3,9 +3,15 @@ public struct Environment {
public var extensions: [Extension]
public var loader: Loader?
public var errorReporter: ErrorReporter
public init(loader: Loader? = nil, extensions: [Extension]? = nil, templateClass: Template.Type = Template.self) {
public init(loader: Loader? = nil,
extensions: [Extension]? = nil,
templateClass: Template.Type = Template.self,
errorReporter: ErrorReporter = SimpleErrorReporter()) {
self.templateClass = templateClass
self.errorReporter = errorReporter
self.loader = loader
self.extensions = (extensions ?? []) + [DefaultExtension()]
}
@@ -28,11 +34,21 @@ public struct Environment {
public func renderTemplate(name: String, context: [String: Any]? = nil) throws -> String {
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 {
let template = templateClass.init(templateString: string, environment: self)
return try template.render(context)
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)
} catch {
try errorReporter.report(error: error)
return ""
}
}
}

View File

@@ -1,5 +1,5 @@
import Spectre
import Stencil
@testable import Stencil
func testEnvironment() {
@@ -32,6 +32,46 @@ func testEnvironment() {
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)
}
}
}

View File

@@ -15,30 +15,6 @@ func testTemplate() {
let result = try template.render([ "name": "Kyle" ])
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)
}
}
}