@@ -22,7 +22,9 @@ import SwiftSoup
2222import os. log
2323
2424public enum BookmarksImportError : Error {
25- case invalidHtml
25+ case invalidHtmlNoDLTag
26+ case invalidHtmlNoBodyTag
27+ case safariTransformFailure
2628 case saveFailure
2729 case unknown
2830}
@@ -58,40 +60,47 @@ final public class BookmarksImporter {
5860 try await saveBookmarks ( importedBookmarks)
5961 NotificationCenter . default. post ( name: Notifications . importDidEnd, object: nil )
6062 return . success( importedBookmarks)
61- } catch BookmarksImportError . invalidHtml {
63+ } catch BookmarksImportError . invalidHtmlNoDLTag {
6264 NotificationCenter . default. post ( name: Notifications . importDidEnd, object: nil )
63- return . failure( . invalidHtml)
65+ Pixel . fire ( pixel: . bookmarkImportFailureParsingDL)
66+ return . failure( . invalidHtmlNoDLTag)
67+ } catch BookmarksImportError . invalidHtmlNoBodyTag {
68+ NotificationCenter . default. post ( name: Notifications . importDidEnd, object: nil )
69+ Pixel . fire ( pixel: . bookmarkImportFailureParsingBody)
70+ return . failure( . invalidHtmlNoBodyTag)
71+ } catch BookmarksImportError . safariTransformFailure {
72+ NotificationCenter . default. post ( name: Notifications . importDidEnd, object: nil )
73+ Pixel . fire ( pixel: . bookmarkImportFailureTransformingSafari)
74+ return . failure( . safariTransformFailure)
6475 } catch BookmarksImportError . saveFailure {
6576 NotificationCenter . default. post ( name: Notifications . importDidEnd, object: nil )
77+ Pixel . fire ( pixel: . bookmarkImportFailureSaving)
6678 return . failure( . saveFailure)
6779 } catch {
6880 NotificationCenter . default. post ( name: Notifications . importDidEnd, object: nil )
81+ Pixel . fire ( pixel: . bookmarkImportFailureUnknown)
6982 return . failure( . unknown)
7083 }
7184 }
7285
7386 func parseHtml( _ htmlContent: String ) async throws {
7487 let document : Document = try SwiftSoup . parse ( htmlContent)
7588
76- do {
77- if isDocumentInSafariFormat ( document) {
78- guard let newDocument = try transformSafariDocument ( document: document) else {
79- os_log ( " Safari format could not be handled " , type: . debug)
80- throw BookmarksImportError . invalidHtml
81- }
82- try parse ( documentElement: newDocument, importedBookmark: nil )
83- } else {
84- try parse ( documentElement: document, importedBookmark: nil )
89+ if isDocumentInSafariFormat ( document) {
90+ guard let newDocument = try transformSafariDocument ( document: document) else {
91+ os_log ( " Safari format could not be handled " , type: . debug)
92+ throw BookmarksImportError . safariTransformFailure
8593 }
86- } catch {
87- throw BookmarksImportError . invalidHtml
94+ try parse ( documentElement: newDocument, importedBookmark: nil )
95+ } else {
96+ try parse ( documentElement: document, importedBookmark: nil )
8897 }
8998 }
9099
91100 /// transform Safari document into a standard bookmark html format
92101 func transformSafariDocument( document: Document ) throws -> Document ? {
93102 guard let body = try document. select ( " body " ) . first ( ) else {
94- throw BookmarksImportError . invalidHtml
103+ throw BookmarksImportError . invalidHtmlNoBodyTag
95104 }
96105
97106 let newDocument : Document = Document ( " " )
@@ -123,7 +132,7 @@ final public class BookmarksImporter {
123132
124133 func parse( documentElement: Element , importedBookmark: BookmarkOrFolder ? , inFavorite: Bool = false ) throws {
125134 guard let firstDL = try documentElement. select ( " DL " ) . first ( ) else {
126- throw BookmarksImportError . invalidHtml
135+ throw BookmarksImportError . invalidHtmlNoDLTag
127136 }
128137 try firstDL. children ( )
129138 . filter ( { try $0. select ( " DT " ) . hasText ( ) } )
0 commit comments