2021-02-20 09:07:10 -07: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-os'
| 'parameter/unsupported-output'
| 'parameter/unsupported-input'
| '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'
2021-06-27 05:47:35 -06:00
| 'device/parallel-video-processing-not-supported'
2021-02-20 09:07:10 -07:00
| 'device/torch-unavailable'
| 'device/microphone-unavailable'
| 'device/low-light-boost-not-supported'
| 'device/focus-not-supported'
| 'device/camera-not-available-on-simulator' ;
2021-09-27 06:44:09 -06:00
export type FrameProcessorError = 'frame-processor/unavailable' ;
2021-02-19 08:07:53 -07:00
export type FormatError =
2021-02-20 09:07:10 -07:00
| 'format/invalid-fps'
| 'format/invalid-hdr'
| 'format/invalid-low-light-boost'
| 'format/invalid-format'
2021-06-12 03:21:26 -06:00
| 'format/invalid-color-space'
2021-02-20 09:07:10 -07:00
| 'format/invalid-preset' ;
2021-06-03 06:16:02 -06:00
export type SessionError =
| 'session/camera-not-ready'
| 'session/audio-session-setup-failed'
| 'session/audio-in-use-by-other-app'
2022-05-10 07:22:32 -06:00
| 'session/audio-session-failed-to-activate' ;
2021-02-19 08:07:53 -07:00
export type CaptureError =
2021-02-20 09:07:10 -07:00
| 'capture/invalid-photo-format'
| 'capture/encoder-error'
| 'capture/muxer-error'
| 'capture/recording-in-progress'
| 'capture/no-recording-in-progress'
| 'capture/file-io-error'
| 'capture/create-temp-file-error'
2022-06-14 02:54:33 -06:00
| 'capture/invalid-video-options'
2021-05-06 06:11:55 -06:00
| 'capture/create-recorder-error'
2022-06-14 02:54:33 -06:00
| 'capture/recorder-error'
| 'capture/no-valid-data'
| 'capture/inactive-source'
| 'capture/insufficient-storage'
| 'capture/file-size-limit-reached'
2021-02-20 09:07:10 -07:00
| 'capture/invalid-photo-codec'
| 'capture/not-bound-error'
| 'capture/capture-type-not-supported'
2021-06-07 05:08:40 -06:00
| 'capture/video-not-enabled'
| 'capture/photo-not-enabled'
2021-06-09 06:56:56 -06:00
| 'capture/aborted'
2021-02-20 09:07:10 -07:00
| 'capture/unknown' ;
2023-03-13 07:21:08 -06:00
export type SystemError = 'system/camera-module-not-found' | 'system/no-camera-manager' | 'system/view-not-found' ;
2021-02-20 09:07:10 -07:00
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
* /
code? : number ;
/ * *
* The native error ' s domain .
*
* * iOS : ` NSError.domain `
* * Android : N / A
* /
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 `
* /
message : string ;
/ * *
* Optional additional details
*
* * iOS : ` NSError.userInfo `
* * Android : N / A
* /
details? : Record < string , unknown > ;
/ * *
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() `
* /
stacktrace? : string ;
/ * *
* Optional additional cause for nested errors
*
* * iOS : N / A
* * Android : ` Throwable.cause `
* /
cause? : ErrorWithCause ;
}
2021-03-31 07:43:29 -06:00
type CameraErrorCode =
| PermissionError
| ParameterError
| DeviceError
2021-09-27 06:44:09 -06:00
| FrameProcessorError
2021-03-31 07:43:29 -06:00
| FormatError
| SessionError
| CaptureError
| SystemError
| 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 {
private readonly _code : TCode ;
private readonly _message : string ;
private readonly _cause? : ErrorWithCause ;
public get code ( ) : TCode {
return this . _code ;
}
public get message ( ) : string {
return this . _message ;
}
2022-03-22 04:15:27 -06:00
public get cause ( ) : Error | undefined {
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 ) {
2021-03-31 08:01:46 -06:00
super ( ` [ ${ code } ]: ${ message } ${ cause != null ? ` (Cause: ${ cause . message } ) ` : '' } ` ) ;
super . name = code ;
super . message = message ;
2021-02-19 08:07:53 -07:00
this . _code = code ;
this . _message = message ;
this . _cause = cause ;
}
2021-03-31 08:01:46 -06:00
public toString ( ) : string {
return ` [ ${ this . code } ]: ${ this . message } ` ;
}
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
*
2021-06-21 14:42:46 -06:00
* See the [ "Camera Errors" documentation ] ( https : //mrousavy.github.io/react-native-vision-camera/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
*
2021-06-21 14:42:46 -06:00
* See the [ "Camera Errors" documentation ] ( https : //mrousavy.github.io/react-native-vision-camera/docs/guides/errors) for more information about Camera Errors.
2021-02-19 08:07:53 -07:00
* /
export class CameraRuntimeError extends CameraError <
2021-09-27 06:44:09 -06:00
PermissionError | ParameterError | DeviceError | FormatError | FrameProcessorError | 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
( isErrorWithCause ( error . cause ) || error . cause == null ) ;
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
2021-02-20 09:07:10 -07: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' ) ) {
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 ,
2021-02-19 08:07:53 -07:00
) ;
}
} else {
return nativeError ;
}
} ;