Skip to content
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ android/keystores/debug.keystore

# generated by bob
lib/


# gh-md-toc
docs/gh-md-toc
17 changes: 10 additions & 7 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function App() {
console.log(JSON.stringify(result, null, 2))
}, [result])

const handleError = (err: Error) => {
const handleError = (err: unknown) => {
if (DocumentPicker.isCancel(err)) {
console.warn('cancelled')
// User cancelled the picker, exit any dialogs or menus and move on
Expand All @@ -33,12 +33,15 @@ export default function App() {
<View style={styles.container}>
<Button
title="open picker for single file selection"
onPress={() => {
DocumentPicker.pickSingle({
presentationStyle: 'fullScreen',
})
.then((pickerResult) => setResult([pickerResult]))
.catch(handleError)
onPress={async () => {
try {
const pickerResult = await DocumentPicker.pickSingle({
presentationStyle: 'fullScreen',
})
setResult([pickerResult])
} catch (e) {
handleError(e)
}
}}
/>
<Button
Expand Down
19 changes: 15 additions & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,22 @@ export function releaseSecureAccess(uris: Array<string>): Promise<void> {
const E_DOCUMENT_PICKER_CANCELED = 'DOCUMENT_PICKER_CANCELED'
const E_DOCUMENT_PICKER_IN_PROGRESS = 'ASYNC_OP_IN_PROGRESS'

export function isCancel(err: Error & { code?: string }): boolean {
return err?.code === E_DOCUMENT_PICKER_CANCELED
export type NativeModuleErrorShape = Error & { code?: string }

export function isCancel(err: unknown): boolean {
return isErrorWithCode(err, E_DOCUMENT_PICKER_CANCELED)
}

export function isInProgress(err: unknown): boolean {
return isErrorWithCode(err, E_DOCUMENT_PICKER_IN_PROGRESS)
}
export function isInProgress(err: Error & { code?: string }): boolean {
return err?.code === E_DOCUMENT_PICKER_IN_PROGRESS

function isErrorWithCode(err: unknown, errorCode: string): boolean {
if (err instanceof Error && 'code' in err) {
const nativeModuleErrorInstance = err as NativeModuleErrorShape
return nativeModuleErrorInstance?.code === errorCode
}
return false
}

export default {
Expand Down