Your throws
is in the wrong place:
static func markdownReader() throws -> Self {
markdownReader()
cannot throw. It just returns a Reader. The throwing, if any is going to happen, will happen in try file.readAsString
, which is inside the closure. The throw does not magically somehow filter up out of that closure definition to the place where it is defined!
So basically you have two choices. One is to declare that the closure type can in fact throw:
var convert: (File) throws -> Page
The other is: don't let the throw percolate up out of the closure. Catch it:
do {
let contents = try file.readAsString(encodedAs: .utf8)
return Page(slug: "", title: "", content: contents, html: "")
} catch {
// ???
}
The problem then is that in the catch
block you are still obligated to return a Page (unless you'd like to fatalError
at this point, on the grounds that the file can't be read so we may as well die). So you'd have to make up a page with fake content
, since you failed to obtain the contents from the File.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…