Skip to content

Commit

Permalink
fix(expo-offline-support): Add Handling network errors section (#1806)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexis Aguilar <[email protected]>
  • Loading branch information
anagstef and alexisintech authored Dec 20, 2024
1 parent 0c43cde commit d27b673
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions docs/references/expo/offline-support.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,50 @@ To enable offline support in your Expo app, follow these steps:
}
```
</Steps>

## How to handle network errors

When there is no internet connection, Clerk's custom flows (e.g., `signIn.create()`) will throw a network error.

To handle network errors in your Expo app, you can use the `isClerkRuntimeError()` function to check if the error is a Clerk-related error. Clerk-related errors are returned as an array of [`ClerkAPIError`](/docs/references/javascript/types/clerk-api-error) objects. These errors contain a `code` property that you can use to identify the specific error. See the following example.

```tsx {{ mark: [1, [22, 30]] }}
import { useSignIn, isClerkRuntimeError } from '@clerk/clerk-expo'
import { Link, useRouter } from 'expo-router'
import { Text, TextInput, Button, View } from 'react-native'
import React from 'react'

export default function SignInPage() {
const { signIn, setActive, isLoaded } = useSignIn()
const router = useRouter()

const [emailAddress, setEmailAddress] = React.useState('')
const [password, setPassword] = React.useState('')

const onSignInPress = React.useCallback(async () => {
if (!isLoaded) return
try {
const signInAttempt = await signIn.create({
identifier: emailAddress,
password,
})

// The rest of your custom flow logic
} catch (err) {
// See https://clerk.com/docs/custom-flows/error-handling
// for more info on error handling
if (isClerkRuntimeError(err) && err.code === 'network_error') {
console.error('Network error occurred!')
}

console.error(JSON.stringify(err, null, 2))
}
}, [isLoaded, emailAddress, password])

return (
<View>
<Button title="Sign in" onPress={onSignInPress} />
</View>
)
}
```

0 comments on commit d27b673

Please sign in to comment.