diff --git a/Tusker/Models/CompositionAttachmentData.swift b/Tusker/Models/CompositionAttachmentData.swift index 577e70f7..71223c02 100644 --- a/Tusker/Models/CompositionAttachmentData.swift +++ b/Tusker/Models/CompositionAttachmentData.swift @@ -88,8 +88,13 @@ enum CompositionAttachmentData { options.isNetworkAccessAllowed = true options.version = .current PHImageManager.default().requestExportSession(forVideo: asset, options: options, exportPreset: AVAssetExportPresetHighestQuality) { (exportSession, info) in - guard let exportSession = exportSession else { fatalError("failed to create export session") } - CompositionAttachmentData.exportVideoData(session: exportSession, completion: completion) + if let exportSession = exportSession { + CompositionAttachmentData.exportVideoData(session: exportSession, completion: completion) + } else if let error = info?[PHImageErrorKey] as? Error { + completion(.failure(.videoExport(error))) + } else { + completion(.failure(.noVideoExportSession)) + } } } else { fatalError("assetType must be either image or video") @@ -97,7 +102,8 @@ enum CompositionAttachmentData { case let .video(url): let asset = AVURLAsset(url: url) guard let session = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality) else { - fatalError("failed to create export session") + completion(.failure(.noVideoExportSession)) + return } CompositionAttachmentData.exportVideoData(session: session, completion: completion) @@ -112,14 +118,14 @@ enum CompositionAttachmentData { session.outputURL = FileManager.default.temporaryDirectory.appendingPathComponent("exported_video_\(UUID())").appendingPathExtension("mp4") session.exportAsynchronously { guard session.status == .completed else { - completion(.failure(.export(session.error!))) + completion(.failure(.videoExport(session.error!))) return } do { let data = try Data(contentsOf: session.outputURL!) completion(.success((data, "video/mp4"))) } catch { - completion(.failure(.export(error))) + completion(.failure(.videoExport(error))) } } } @@ -128,9 +134,21 @@ enum CompositionAttachmentData { case image, video } - enum Error: Swift.Error { + enum Error: Swift.Error, LocalizedError { case missingData - case export(Swift.Error) + case videoExport(Swift.Error) + case noVideoExportSession + + var localizedDescription: String { + switch self { + case .missingData: + return "Missing Data" + case .videoExport(let error): + return "Exporting video: \(error)" + case .noVideoExportSession: + return "Couldn't create video export session" + } + } } } diff --git a/Tusker/Screens/Compose/ComposeView.swift b/Tusker/Screens/Compose/ComposeView.swift index 07f96b24..cc8b04b8 100644 --- a/Tusker/Screens/Compose/ComposeView.swift +++ b/Tusker/Screens/Compose/ComposeView.swift @@ -350,6 +350,8 @@ fileprivate struct AttachmentUploadError: PostError { // need to downcast to use more specific localizedDescription impl from Pachyderm if let error = error as? Client.Error { description = error.localizedDescription + } else if let error = error as? CompositionAttachmentData.Error { + description = error.localizedDescription } else { description = error.localizedDescription }