react-native-vision-camera/docs/ERRORS.md

100 lines
3.1 KiB
Markdown
Raw Normal View History

2021-02-19 08:07:53 -07:00
<table>
<tr>
2021-02-23 01:39:52 -07:00
<th><a href="../README.md">README</a></th>
2021-02-24 14:19:28 -07:00
<th><a href="./SETUP.md">SETUP</a></th>
2021-02-23 01:39:52 -07:00
<th><a href="./DEVICES.md">DEVICES</a></th>
<th><a href="./FORMATS.md">FORMATS</a></th>
<th><a href="./FRAME_PROCESSORS.md">FRAME_PROCESSORS</a></th>
<th><a href="./ANIMATED.md">ANIMATED</a></th>
<th>ERRORS</th>
2021-02-19 08:07:53 -07:00
</tr>
</table>
2021-02-22 06:32:45 -07:00
<br/>
2021-02-19 08:07:53 -07:00
2021-02-22 06:32:45 -07:00
<h1 align="center">Errors</h1>
2021-02-23 00:51:02 -07:00
<div>
<img align="right" width="35%" src="../img/example_error.png">
</div>
2021-02-22 06:32:45 -07:00
## Why?
Since the Camera library is quite big, there is a lot that can "go wrong". The react-native-vision-camera library provides thoroughly typed errors to help you quickly identify the cause and fix the problem.
```ts
switch (error.code) {
case "device/configuration-error":
console.log("Failed to configure the camera device.")
break
case "device/microphone-unavailable":
console.log("This camera device does not have a microphone.")
break
2021-02-22 06:33:55 -07:00
case "capture/recording-in-progress":
console.log("Another recording is already in progress!")
break
2021-02-22 06:32:45 -07:00
default:
console.error(error)
break
}
```
## The Error types
2021-02-23 02:59:12 -07:00
The `CameraError` type is a baseclass type for all other errors and provides the following properties:
2021-02-22 06:32:45 -07:00
2021-02-23 02:58:00 -07:00
* `code`: A typed code in the form of `{domain}/{code}` that can be used to quickly identify and group errors
2021-02-22 06:32:45 -07:00
* `message`: A non-localized message text that provides a more information and context about the error and possibly problematic values.
2021-02-23 10:47:37 -07:00
* `cause?`: An `ErrorWithCause` instance that provides information about the cause of the error. (Optional)
2021-02-22 06:32:45 -07:00
* `cause.message`: The message of the error that caused the camera error. This is localized on iOS.
2021-02-23 10:46:58 -07:00
* `cause.code?`: The native error's error-code. (iOS only)
* `cause.domain?`: The native error's domain. (iOS only)
* `cause.details?`: More dictionary-style information about the cause. (iOS only)
* `cause.stacktrace?`: A native Java stacktrace for the cause. (Android only)
2021-02-24 13:44:06 -07:00
* `cause.cause?`: The cause that caused this cause. (Recursive) (Optional)
2021-02-22 06:32:45 -07:00
### Runtime Errors
The `CameraRuntimeError` represents any kind of error that occured while mounting the Camera view, or an error that occured during the runtime.
The `<Camera />` UI Component provides an `onError` function that will be invoked every time an unexpected runtime error occured.
```tsx
function App() {
const onError = useCallback((error: CameraRuntimeError) => {
console.error(error)
}, [])
2021-02-22 06:35:59 -07:00
return <Camera ref={camera} {...cameraProps} />
2021-02-22 06:32:45 -07:00
}
```
### Capture Errors
The `CameraCaptureError` represents any kind of error that occured only while capturing a photo or recording a video.
```tsx
function App() {
const camera = useRef<Camera>(null)
const onPress = useCallback(() => {
try {
const photo = await camera.current.takePhoto()
} catch (e) {
if (e instanceof CameraCaptureError) {
2021-02-22 06:33:55 -07:00
switch (e.code) {
2021-02-24 13:44:06 -07:00
case "capture/file-io-error":
2021-02-22 06:33:55 -07:00
console.error("Failed to write photo to disk!")
break
default:
console.error(e)
break
}
2021-02-22 06:32:45 -07:00
}
}
}, [camera]);
2021-02-22 06:35:59 -07:00
return <Camera ref={camera} {...cameraProps} />
2021-02-22 06:32:45 -07:00
}
```