From 0c015e23276f392b17838c0dd5d2ab7212cfd3b5 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Mon, 14 Feb 2022 12:36:08 -0400 Subject: [PATCH] Throw error when not a valid NSKeyedArchive --- SilicaViewer/Document.swift | 50 ++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/SilicaViewer/Document.swift b/SilicaViewer/Document.swift index 797a241..a7d3a60 100644 --- a/SilicaViewer/Document.swift +++ b/SilicaViewer/Document.swift @@ -77,6 +77,28 @@ class Document: NSDocument { var remainderWidth: Int = 0 var remainderHeight: Int = 0 + struct SilicaParsingError: Error, LocalizedError { + enum Kind { + case invalid + } + + let kind: Kind + let filename: URL? + + public var errorDescription: String? { + switch self.kind { + case .invalid: + return filename!.lastPathComponent + " is an invalid Silica Document." + } + } + } + + func throwError(_ error: SilicaParsingError.Kind) { + DispatchQueue.main.sync { + let _ = presentError(SilicaParsingError(kind: error, filename: fileURL)) + } + } + override init() { super.init() } @@ -447,9 +469,7 @@ class Document: NSDocument { let layersClass = objectsArray[layersClassID] as! NSDictionary let array = layersClass["NS.objects"] as! NSArray - - //dump(dict, indent: 5) - + for object in array { let layerClassID = getClassID(id: object) let layerClass = objectsArray[layerClassID] as! NSDictionary @@ -464,7 +484,7 @@ class Document: NSDocument { // double check if this archive is really correct if let value = dict["$version"] { if (value as! Int) != NSKeyedArchiveVersion { - Swift.print("This is not a valid document!") + throwError(.invalid) return } @@ -480,28 +500,6 @@ class Document: NSDocument { parseSilicaDocument(archive: archive, dict: topObjectClass) } } - - struct SilicaParsingError: Error, LocalizedError { - enum Kind { - case invalid - } - - let kind: Kind - let filename: URL? - - public var errorDescription: String? { - switch self.kind { - case .invalid: - return filename!.lastPathComponent + " is an invalid Silica Document." - } - } - } - - func throwError(_ error: SilicaParsingError.Kind) { - DispatchQueue.main.sync { - let _ = presentError(SilicaParsingError(kind: error, filename: fileURL)) - } - } override func read(from data: Data, ofType typeName: String) throws { self.data = data