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.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"
}
}
}
}

View File

@ -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
}