Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved error handling in lower iOS versions #25

Merged
merged 3 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 48 additions & 16 deletions ios/Classes/GalPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,54 @@ public class GalPlugin: NSObject, FlutterPlugin {
private func handleError(error: NSError) -> FlutterError {
let message = error.localizedDescription
let details = Thread.callStackSymbols
if #available(iOS 15, *) {
switch error.code {
case PHPhotosError.accessRestricted.rawValue, PHPhotosError.accessUserDenied.rawValue:
return FlutterError(code: "ACCESS_DENIED", message: message, details: details)
case PHPhotosError.identifierNotFound.rawValue,
PHPhotosError.multipleIdentifiersFound.rawValue,
PHPhotosError.requestNotSupportedForAsset.rawValue,
3302:
return FlutterError(code: "NOT_SUPPORTED_FORMAT", message: message, details: details)
case PHPhotosError.notEnoughSpace.rawValue:
return FlutterError(code: "NOT_ENOUGH_SPACE", message: message, details: details)
default:
return FlutterError(code: "UNEXPECTED", message: message, details: details)
}
} else {
return FlutterError(code: "NOT_HANDLE", message: message, details: details)
switch error.code {
case PHErrorCode.accessRestricted.rawValue, PHErrorCode.accessUserDenied.rawValue:
return FlutterError(code: "ACCESS_DENIED", message: message, details: details)

case PHErrorCode.identifierNotFound.rawValue,
PHErrorCode.multipleIdentifiersFound.rawValue,
PHErrorCode.requestNotSupportedForAsset.rawValue,
PHErrorCode.videoConversionFailed.rawValue,
PHErrorCode.unsupportedVideoCodecs.rawValue:
return FlutterError(code: "NOT_SUPPORTED_FORMAT", message: message, details: details)

case PHErrorCode.notEnoughSpace.rawValue:
return FlutterError(code: "NOT_ENOUGH_SPACE", message: message, details: details)

default:
return FlutterError(code: "UNEXPECTED", message: message, details: details)
}
}
}

/// Low iOS versions do not have an enum defined, so [rawValue] must be used.
/// If [rawValue] is not defined either, no handle is possible.
/// You can check Apple's documentation by replacing the [$caseName] of the following URL
/// Some documents are not provided by Apple.
/// https://developer.apple.com/documentation/photokit/phphotoserror/code/$caseName
enum PHErrorCode: Int {

// [PHPhotosError.identifierNotFound]
case identifierNotFound = 3201

// [PHPhotosError.multipleIdentifiersFound]
case multipleIdentifiersFound = 3202

// Apple has not released documentation.
case videoConversionFailed = 3300

// Apple has not released documentation.
case unsupportedVideoCodecs = 3302

// [PHPhotosError.notEnoughSpace]
case notEnoughSpace = 3305

// [PHPhotosError.requestNotSupportedForAsset]
case requestNotSupportedForAsset = 3306

// [PHPhotosError.accessRestricted]
case accessRestricted = 3310

// [PHPhotosError.accessUserDenied]
case accessUserDenied = 3311
}
9 changes: 4 additions & 5 deletions lib/src/gal_exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class GalException implements Exception {
}

enum GalExceptionType {

/// When Has no permission to access gallery app.
accessDenied,

Expand All @@ -39,23 +38,23 @@ enum GalExceptionType {
/// When an error occurs with unexpected.
unexpected,

/// When an error occurs under iOS15.
/// Under iOS 15, it is not possible to get details of errors on saving.
@Deprecated(
'Use [unexpected] instead. https://github.com/natsuk4ze/gal/pull/25')
notHandle;

String get code => switch (this) {
accessDenied => 'ACCESS_DENIED',
notEnoughSpace => 'NOT_ENOUGH_SPACE',
notSupportedFormat => 'NOT_SUPPORTED_FORMAT',
unexpected => 'UNEXPECTED',
notHandle => 'NOT_HANDLE',
_ => 'NOT_HANDLE',
};

String get message => switch (this) {
accessDenied => 'You do not have permission to access the gallery app.',
notEnoughSpace => 'Not enough space for storage.',
notSupportedFormat => 'Unsupported file formats.',
unexpected => 'An unexpected error has occurred.',
notHandle => 'An unexpected error has occurred.',
_ => 'An unexpected error has occurred.',
};
}