2023-09-26 03:39:17 -06:00
|
|
|
export type PermissionError = 'permission/microphone-permission-denied' | 'permission/camera-permission-denied'
|
2021-02-19 08:07:53 -07:00
|
|
|
export type ParameterError =
|
2021-02-20 09:07:10 -07:00
|
|
|
| 'parameter/invalid-parameter'
|
|
|
|
| 'parameter/unsupported-output'
|
|
|
|
| 'parameter/unsupported-input'
|
2023-09-26 03:39:17 -06:00
|
|
|
| 'parameter/invalid-combination'
|
2021-02-19 08:07:53 -07:00
|
|
|
export type DeviceError =
|
2021-02-20 09:07:10 -07:00
|
|
|
| 'device/configuration-error'
|
|
|
|
| 'device/no-device'
|
|
|
|
| 'device/invalid-device'
|
|
|
|
| 'device/microphone-unavailable'
|
2023-08-21 04:50:14 -06:00
|
|
|
| 'device/pixel-format-not-supported'
|
2021-02-20 09:07:10 -07:00
|
|
|
| 'device/low-light-boost-not-supported'
|
|
|
|
| 'device/focus-not-supported'
|
2023-09-26 03:39:17 -06:00
|
|
|
| 'device/camera-not-available-on-simulator'
|
2021-02-19 08:07:53 -07:00
|
|
|
export type FormatError =
|
2021-02-20 09:07:10 -07:00
|
|
|
| 'format/invalid-fps'
|
2023-11-15 10:33:12 -07:00
|
|
|
| 'format/invalid-video-hdr'
|
2024-01-08 04:13:05 -07:00
|
|
|
| 'format/invalid-video-stabilization-mode'
|
2023-09-21 08:30:05 -06:00
|
|
|
| 'format/incompatible-pixel-format-with-hdr-setting'
|
2021-02-20 09:07:10 -07:00
|
|
|
| 'format/invalid-format'
|
2024-01-08 04:13:05 -07:00
|
|
|
| 'format/format-required'
|
2021-06-03 06:16:02 -06:00
|
|
|
export type SessionError =
|
|
|
|
| 'session/camera-not-ready'
|
2023-08-21 04:50:14 -06:00
|
|
|
| 'session/camera-cannot-be-opened'
|
|
|
|
| 'session/camera-has-been-disconnected'
|
2021-06-03 06:16:02 -06:00
|
|
|
| 'session/audio-in-use-by-other-app'
|
2024-02-06 06:19:25 -07:00
|
|
|
| 'session/no-outputs'
|
2023-09-26 03:39:17 -06:00
|
|
|
| 'session/audio-session-failed-to-activate'
|
2023-10-04 04:53:52 -06:00
|
|
|
export type CodeScannerError =
|
|
|
|
| 'code-scanner/not-compatible-with-outputs'
|
|
|
|
| 'code-scanner/code-type-not-supported'
|
|
|
|
| 'code-scanner/cannot-load-model'
|
2021-02-19 08:07:53 -07:00
|
|
|
export type CaptureError =
|
2021-02-20 09:07:10 -07:00
|
|
|
| 'capture/recording-in-progress'
|
|
|
|
| 'capture/no-recording-in-progress'
|
|
|
|
| 'capture/file-io-error'
|
|
|
|
| 'capture/create-temp-file-error'
|
2021-05-06 06:11:55 -06:00
|
|
|
| 'capture/create-recorder-error'
|
2024-01-24 03:48:38 -07:00
|
|
|
| 'capture/insufficient-storage'
|
2022-06-14 02:54:33 -06:00
|
|
|
| 'capture/recorder-error'
|
2021-06-07 05:08:40 -06:00
|
|
|
| 'capture/video-not-enabled'
|
|
|
|
| 'capture/photo-not-enabled'
|
2024-02-05 04:34:32 -07:00
|
|
|
| 'capture/frame-invalid'
|
2021-06-09 06:56:56 -06:00
|
|
|
| 'capture/aborted'
|
2023-09-26 03:39:17 -06:00
|
|
|
| 'capture/unknown'
|
2023-07-20 07:30:04 -06:00
|
|
|
export type SystemError =
|
|
|
|
| 'system/camera-module-not-found'
|
|
|
|
| 'system/no-camera-manager'
|
|
|
|
| 'system/frame-processors-unavailable'
|
2023-09-26 03:39:17 -06:00
|
|
|
| 'system/view-not-found'
|
|
|
|
export type UnknownError = 'unknown/unknown'
|
2021-02-19 08:07:53 -07:00
|
|
|
|
2021-03-03 04:37:43 -07:00
|
|
|
/**
|
2021-03-08 10:51:53 -07:00
|
|
|
* Represents a JSON-style error cause. This contains native `NSError`/`Throwable` information, and can have recursive {@linkcode ErrorWithCause.cause | .cause} properties until the ultimate cause has been found.
|
2021-03-03 04:37:43 -07:00
|
|
|
*/
|
2021-02-19 08:07:53 -07:00
|
|
|
export interface ErrorWithCause {
|
2021-02-23 07:59:07 -07:00
|
|
|
/**
|
|
|
|
* The native error's code.
|
|
|
|
*
|
|
|
|
* * iOS: `NSError.code`
|
|
|
|
* * Android: N/A
|
|
|
|
*/
|
2023-09-26 03:39:17 -06:00
|
|
|
code?: number
|
2021-02-23 07:59:07 -07:00
|
|
|
/**
|
|
|
|
* The native error's domain.
|
|
|
|
*
|
|
|
|
* * iOS: `NSError.domain`
|
|
|
|
* * Android: N/A
|
|
|
|
*/
|
2023-09-26 03:39:17 -06:00
|
|
|
domain?: string
|
2021-02-19 08:07:53 -07:00
|
|
|
/**
|
2021-03-26 08:54:27 -06:00
|
|
|
* The native error description
|
2021-02-19 08:07:53 -07:00
|
|
|
*
|
|
|
|
* * iOS: `NSError.message`
|
|
|
|
* * Android: `Throwable.message`
|
|
|
|
*/
|
2023-09-26 03:39:17 -06:00
|
|
|
message: string
|
2021-02-19 08:07:53 -07:00
|
|
|
/**
|
|
|
|
* Optional additional details
|
|
|
|
*
|
|
|
|
* * iOS: `NSError.userInfo`
|
|
|
|
* * Android: N/A
|
|
|
|
*/
|
2023-09-26 03:39:17 -06:00
|
|
|
details?: Record<string, unknown>
|
2021-02-19 08:07:53 -07:00
|
|
|
/**
|
2021-03-08 10:21:30 -07:00
|
|
|
* Optional Java stacktrace
|
2021-02-19 08:07:53 -07:00
|
|
|
*
|
|
|
|
* * iOS: N/A
|
|
|
|
* * Android: `Throwable.stacktrace.toString()`
|
|
|
|
*/
|
2023-09-26 03:39:17 -06:00
|
|
|
stacktrace?: string
|
2021-02-19 08:07:53 -07:00
|
|
|
/**
|
|
|
|
* Optional additional cause for nested errors
|
|
|
|
*
|
|
|
|
* * iOS: N/A
|
|
|
|
* * Android: `Throwable.cause`
|
|
|
|
*/
|
2023-09-26 03:39:17 -06:00
|
|
|
cause?: ErrorWithCause
|
2021-02-19 08:07:53 -07:00
|
|
|
}
|
|
|
|
|
2021-03-31 07:43:29 -06:00
|
|
|
type CameraErrorCode =
|
|
|
|
| PermissionError
|
|
|
|
| ParameterError
|
|
|
|
| DeviceError
|
|
|
|
| FormatError
|
|
|
|
| SessionError
|
|
|
|
| CaptureError
|
|
|
|
| SystemError
|
2023-09-26 03:39:17 -06:00
|
|
|
| UnknownError
|
2021-02-19 08:07:53 -07:00
|
|
|
|
|
|
|
/**
|
2021-03-08 10:51:53 -07:00
|
|
|
* Represents any kind of error that occured in the {@linkcode Camera} View Module.
|
2021-02-19 08:07:53 -07:00
|
|
|
*/
|
|
|
|
class CameraError<TCode extends CameraErrorCode> extends Error {
|
2023-09-26 03:39:17 -06:00
|
|
|
private readonly _code: TCode
|
|
|
|
private readonly _message: string
|
|
|
|
private readonly _cause?: ErrorWithCause
|
2021-02-19 08:07:53 -07:00
|
|
|
|
|
|
|
public get code(): TCode {
|
2023-09-26 03:39:17 -06:00
|
|
|
return this._code
|
2021-02-19 08:07:53 -07:00
|
|
|
}
|
|
|
|
public get message(): string {
|
2023-09-26 03:39:17 -06:00
|
|
|
return this._message
|
2021-02-19 08:07:53 -07:00
|
|
|
}
|
2022-03-22 04:15:27 -06:00
|
|
|
public get cause(): Error | undefined {
|
2023-09-26 03:39:17 -06:00
|
|
|
const c = this._cause
|
|
|
|
if (c == null) return undefined
|
|
|
|
return new Error(`[${c.code}]: ${c.message}`)
|
2021-02-19 08:07:53 -07:00
|
|
|
}
|
|
|
|
|
2021-03-07 06:26:21 -07:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2021-02-19 08:07:53 -07:00
|
|
|
constructor(code: TCode, message: string, cause?: ErrorWithCause) {
|
2023-09-26 03:39:17 -06:00
|
|
|
super(`[${code}]: ${message}${cause != null ? ` (Cause: ${cause.message})` : ''}`)
|
|
|
|
super.name = code
|
|
|
|
super.message = message
|
|
|
|
this._code = code
|
|
|
|
this._message = message
|
|
|
|
this._cause = cause
|
2021-02-19 08:07:53 -07:00
|
|
|
}
|
2021-03-31 08:01:46 -06:00
|
|
|
|
|
|
|
public toString(): string {
|
2023-09-26 03:39:17 -06:00
|
|
|
return `[${this.code}]: ${this.message}`
|
2021-03-31 08:01:46 -06:00
|
|
|
}
|
2021-02-19 08:07:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents any kind of error that occured while trying to capture a video or photo.
|
2021-03-08 10:51:53 -07:00
|
|
|
*
|
2023-03-06 03:03:40 -07:00
|
|
|
* See the ["Camera Errors" documentation](https://react-native-vision-camera.com/docs/guides/errors) for more information about Camera Errors.
|
2021-02-19 08:07:53 -07:00
|
|
|
*/
|
|
|
|
export class CameraCaptureError extends CameraError<CaptureError> {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents any kind of error that occured in the Camera View Module.
|
2021-03-08 10:51:53 -07:00
|
|
|
*
|
2023-03-06 03:03:40 -07:00
|
|
|
* See the ["Camera Errors" documentation](https://react-native-vision-camera.com/docs/guides/errors) for more information about Camera Errors.
|
2021-02-19 08:07:53 -07:00
|
|
|
*/
|
|
|
|
export class CameraRuntimeError extends CameraError<
|
2023-07-20 07:30:04 -06:00
|
|
|
PermissionError | ParameterError | DeviceError | FormatError | SessionError | SystemError | UnknownError
|
2021-02-19 08:07:53 -07:00
|
|
|
> {}
|
|
|
|
|
2021-03-07 06:26:21 -07:00
|
|
|
/**
|
2021-03-08 10:51:53 -07:00
|
|
|
* Checks if the given `error` is of type {@linkcode ErrorWithCause}
|
2021-03-08 10:21:30 -07:00
|
|
|
* @param {unknown} error Any unknown object to validate
|
2021-03-08 10:51:53 -07:00
|
|
|
* @returns `true` if the given `error` is of type {@linkcode ErrorWithCause}
|
2021-03-07 06:26:21 -07:00
|
|
|
*/
|
2021-02-19 08:07:53 -07:00
|
|
|
export const isErrorWithCause = (error: unknown): error is ErrorWithCause =>
|
2021-02-20 09:07:10 -07:00
|
|
|
typeof error === 'object' &&
|
2021-02-19 08:07:53 -07:00
|
|
|
error != null &&
|
|
|
|
// @ts-expect-error error is still unknown
|
2021-02-20 09:07:10 -07:00
|
|
|
typeof error.message === 'string' &&
|
2021-02-19 08:07:53 -07:00
|
|
|
// @ts-expect-error error is still unknown
|
2021-02-20 09:07:10 -07:00
|
|
|
(typeof error.stacktrace === 'string' || error.stacktrace == null) &&
|
2021-02-19 08:07:53 -07:00
|
|
|
// @ts-expect-error error is still unknown
|
2023-09-26 03:39:17 -06:00
|
|
|
(isErrorWithCause(error.cause) || error.cause == null)
|
2021-02-19 08:07:53 -07:00
|
|
|
|
2021-02-20 09:07:10 -07:00
|
|
|
const isCameraErrorJson = (error: unknown): error is { code: string; message: string; cause?: ErrorWithCause } =>
|
|
|
|
typeof error === 'object' &&
|
2021-02-19 08:07:53 -07:00
|
|
|
error != null &&
|
|
|
|
// @ts-expect-error error is still unknown
|
2021-02-20 09:07:10 -07:00
|
|
|
typeof error.code === 'string' &&
|
2021-02-19 08:07:53 -07:00
|
|
|
// @ts-expect-error error is still unknown
|
2021-02-20 09:07:10 -07:00
|
|
|
typeof error.message === 'string' &&
|
2021-02-19 08:07:53 -07:00
|
|
|
// @ts-expect-error error is still unknown
|
2023-09-26 03:39:17 -06:00
|
|
|
(typeof error.cause === 'object' || error.cause == null)
|
2021-02-19 08:07:53 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tries to parse an error coming from native to a typed JS camera error.
|
2021-03-03 04:37:43 -07:00
|
|
|
* @param {CameraError} nativeError The native error instance. This is a JSON in the legacy native module architecture.
|
2021-03-08 10:51:53 -07:00
|
|
|
* @returns A {@linkcode CameraRuntimeError} or {@linkcode CameraCaptureError}, or the `nativeError` itself if it's not parsable
|
2021-03-03 04:37:43 -07:00
|
|
|
* @method
|
2021-02-19 08:07:53 -07:00
|
|
|
*/
|
2021-02-20 09:07:10 -07:00
|
|
|
export const tryParseNativeCameraError = <T>(nativeError: T): (CameraRuntimeError | CameraCaptureError) | T => {
|
2021-02-19 08:07:53 -07:00
|
|
|
if (isCameraErrorJson(nativeError)) {
|
2021-02-20 09:07:10 -07:00
|
|
|
if (nativeError.code.startsWith('capture')) {
|
2023-09-26 03:39:17 -06:00
|
|
|
return new CameraCaptureError(nativeError.code as CaptureError, nativeError.message, nativeError.cause)
|
2021-02-19 08:07:53 -07:00
|
|
|
} else {
|
|
|
|
return new CameraRuntimeError(
|
|
|
|
// @ts-expect-error the code is string, we narrow it down to TS union.
|
|
|
|
nativeError.code,
|
|
|
|
nativeError.message,
|
2021-02-20 09:07:10 -07:00
|
|
|
nativeError.cause,
|
2023-09-26 03:39:17 -06:00
|
|
|
)
|
2021-02-19 08:07:53 -07:00
|
|
|
}
|
|
|
|
} else {
|
2023-09-26 03:39:17 -06:00
|
|
|
return nativeError
|
2021-02-19 08:07:53 -07:00
|
|
|
}
|
2023-09-26 03:39:17 -06:00
|
|
|
}
|