Remove custom Result type and throw errors

This commit is contained in:
Kyle Fuller
2015-09-25 12:53:45 -07:00
parent 25f5583542
commit 9c335caeb6
17 changed files with 211 additions and 460 deletions

View File

@@ -1,44 +1,37 @@
import Foundation
import PathKit
extension String : Error {
public var description:String {
return self
}
}
public class IncludeNode : Node {
public class IncludeNode : NodeType {
public let templateName:String
public class func parse(parser:TokenParser, token:Token) -> TokenParser.Result {
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
let bits = token.contents.componentsSeparatedByString("\"")
if bits.count != 3 {
return .Error(error:NodeError(token: token, message: "Tag takes one argument, the template file to be included"))
throw TemplateSyntaxError("'include' tag takes one argument, the template file to be included")
}
return .Success(node:IncludeNode(templateName: bits[1]))
return IncludeNode(templateName: bits[1])
}
public init(templateName:String) {
self.templateName = templateName
}
public func render(context: Context) -> Result {
public func render(context: Context) throws -> String {
if let loader = context["loader"] as? TemplateLoader {
if let template = loader.loadTemplate(templateName) {
return template.render(context)
return try template.render(context)
}
let paths:String = loader.paths.map { path in
return path.description
}.joinWithSeparator(", ")
let error = "Template '\(templateName)' not found in \(paths)"
return .Error(error)
}.joinWithSeparator(", ")
throw TemplateSyntaxError("'\(templateName)' template not found in \(paths)")
}
let error = "Template loader not in context"
return .Error(error)
throw TemplateSyntaxError("Template loader not in context")
}
}