fix(mobile): handle file picker cancellation
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
export const FILE_SELECTION_CANCELLED = 'FILE_SELECTION_CANCELLED';
|
||||
|
||||
export class FileSelectionCancelledError extends Error {
|
||||
readonly code = FILE_SELECTION_CANCELLED;
|
||||
|
||||
constructor(label: string) {
|
||||
super(`未选择${label}`);
|
||||
this.name = 'FileSelectionCancelledError';
|
||||
}
|
||||
}
|
||||
|
||||
const failureSignal = (failure: unknown): string => {
|
||||
if (typeof failure === 'string') return failure;
|
||||
if (!failure || typeof failure !== 'object') return '';
|
||||
const value = failure as { errMsg?: unknown; message?: unknown };
|
||||
return String(value.errMsg || value.message || '');
|
||||
};
|
||||
|
||||
export const fileSelectionFailure = (failure: unknown, label: string): Error => {
|
||||
const signal = failureSignal(failure).toLowerCase();
|
||||
if (signal.includes('cancel')) return new FileSelectionCancelledError(label);
|
||||
return new Error(`无法选择${label},请重试`);
|
||||
};
|
||||
|
||||
export const isFileSelectionCancelled = (error: unknown): boolean =>
|
||||
error instanceof FileSelectionCancelledError
|
||||
|| Boolean(error && typeof error === 'object'
|
||||
&& (error as { code?: unknown }).code === FILE_SELECTION_CANCELLED);
|
||||
Reference in New Issue
Block a user