added parent context to ErrorReporterContext and handling errors in include and extend nodes

This commit is contained in:
Ilya Puchka
2017-10-07 21:02:27 +02:00
parent e59609f140
commit 079fdf39b8
9 changed files with 77 additions and 15 deletions

View File

@@ -35,26 +35,31 @@ public struct TemplateSyntaxError : Error, Equatable, CustomStringConvertible {
public class ErrorReporterContext {
public let template: Template
public init(template: Template) {
public typealias ParentContext = (context: ErrorReporterContext, token: Token)
public let parent: ParentContext?
public init(template: Template, parent: ParentContext? = nil) {
self.template = template
self.parent = parent
}
}
public protocol ErrorReporter: class {
var context: ErrorReporterContext! { get set }
func report(error: Error) throws
func report(error: Error) throws -> Never
func contextAwareError(_ error: TemplateSyntaxError, context: ErrorReporterContext) -> Error?
}
open class SimpleErrorReporter: ErrorReporter {
public var context: ErrorReporterContext!
open func report(error: Error) throws {
open func report(error: Error) throws -> Never {
guard let syntaxError = error as? TemplateSyntaxError else { throw error }
guard let context = context else { throw error }
throw contextAwareError(syntaxError, context: context) ?? error
}
// TODO: add stack trace using parent context
open func contextAwareError(_ error: TemplateSyntaxError, context: ErrorReporterContext) -> Error? {
guard let lexeme = error.lexeme, lexeme.range != .unknown else { return nil }
let templateName = context.template.name.map({ "\($0):" }) ?? ""