Better error messages when exporting video fails

This commit is contained in:
Shadowfacts 2022-04-27 23:31:31 -04:00
parent 4dd8c1d692
commit 3f04d74dd6
2 changed files with 27 additions and 7 deletions

View File

@ -88,8 +88,13 @@ enum CompositionAttachmentData {
options.isNetworkAccessAllowed = true options.isNetworkAccessAllowed = true
options.version = .current options.version = .current
PHImageManager.default().requestExportSession(forVideo: asset, options: options, exportPreset: AVAssetExportPresetHighestQuality) { (exportSession, info) in PHImageManager.default().requestExportSession(forVideo: asset, options: options, exportPreset: AVAssetExportPresetHighestQuality) { (exportSession, info) in
guard let exportSession = exportSession else { fatalError("failed to create export session") } if let exportSession = exportSession {
CompositionAttachmentData.exportVideoData(session: exportSession, completion: completion) CompositionAttachmentData.exportVideoData(session: exportSession, completion: completion)
} else if let error = info?[PHImageErrorKey] as? Error {
completion(.failure(.videoExport(error)))
} else {
completion(.failure(.noVideoExportSession))
}
} }
} else { } else {
fatalError("assetType must be either image or video") fatalError("assetType must be either image or video")
@ -97,7 +102,8 @@ enum CompositionAttachmentData {
case let .video(url): case let .video(url):
let asset = AVURLAsset(url: url) let asset = AVURLAsset(url: url)
guard let session = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality) else { 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) CompositionAttachmentData.exportVideoData(session: session, completion: completion)
@ -112,14 +118,14 @@ enum CompositionAttachmentData {
session.outputURL = FileManager.default.temporaryDirectory.appendingPathComponent("exported_video_\(UUID())").appendingPathExtension("mp4") session.outputURL = FileManager.default.temporaryDirectory.appendingPathComponent("exported_video_\(UUID())").appendingPathExtension("mp4")
session.exportAsynchronously { session.exportAsynchronously {
guard session.status == .completed else { guard session.status == .completed else {
completion(.failure(.export(session.error!))) completion(.failure(.videoExport(session.error!)))
return return
} }
do { do {
let data = try Data(contentsOf: session.outputURL!) let data = try Data(contentsOf: session.outputURL!)
completion(.success((data, "video/mp4"))) completion(.success((data, "video/mp4")))
} catch { } catch {
completion(.failure(.export(error))) completion(.failure(.videoExport(error)))
} }
} }
} }
@ -128,9 +134,21 @@ enum CompositionAttachmentData {
case image, video case image, video
} }
enum Error: Swift.Error { enum Error: Swift.Error, LocalizedError {
case missingData 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"
}
}
} }
} }

View File

@ -350,6 +350,8 @@ fileprivate struct AttachmentUploadError: PostError {
// need to downcast to use more specific localizedDescription impl from Pachyderm // need to downcast to use more specific localizedDescription impl from Pachyderm
if let error = error as? Client.Error { if let error = error as? Client.Error {
description = error.localizedDescription description = error.localizedDescription
} else if let error = error as? CompositionAttachmentData.Error {
description = error.localizedDescription
} else { } else {
description = error.localizedDescription description = error.localizedDescription
} }