Skip to content

Commit 1fc3d03

Browse files
committed
chore: Create a custom error for resource fetcher adapter initialization
1 parent f4b7578 commit 1fc3d03

5 files changed

Lines changed: 44 additions & 17 deletions

File tree

packages/react-native-executorch/common/rnexecutorch/ErrorCodes.h

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,51 +9,60 @@ namespace rnexecutorch {
99

1010
enum class RnExecutorchErrorCode : int32_t {
1111
/**
12-
* An umbrella-error that is thrown usually when something unexpected happens, for example a 3rd-party library error.
12+
* An umbrella-error that is thrown usually when something unexpected happens,
13+
* for example a 3rd-party library error.
1314
*/
1415
UnknownError = 101,
1516
/**
16-
* Thrown when a user tries to run a model that is not yet downloaded or loaded into memory.
17+
* Thrown when a user tries to run a model that is not yet downloaded or
18+
* loaded into memory.
1719
*/
1820
ModuleNotLoaded = 102,
1921
/**
20-
* An error ocurred when saving a file. This could be, for instance a result image from an image model.
22+
* An error ocurred when saving a file. This could be, for instance a result
23+
* image from an image model.
2124
*/
2225
FileWriteFailed = 103,
2326
/**
24-
* Thrown when a user tries to run a model that is currently processing. It is only allowed to run a single model prediction at a time.
27+
* Thrown when a user tries to run a model that is currently processing. It is
28+
* only allowed to run a single model prediction at a time.
2529
*/
2630
ModelGenerating = 104,
2731
/**
28-
* Thrown when a language is passed to a multi-language model that is not supported. For example OCR or Speech To Text.
32+
* Thrown when a language is passed to a multi-language model that is not
33+
* supported. For example OCR or Speech To Text.
2934
*/
3035
LanguageNotSupported = 105,
3136
/**
32-
* Thrown when config parameters passed to a model are invalid. For example, when LLM's topp is outside of range [0, 1].
37+
* Thrown when config parameters passed to a model are invalid. For example,
38+
* when LLM's topp is outside of range [0, 1].
3339
*/
3440
InvalidConfig = 112,
3541
/**
3642
* Thrown when the type of model source passed by the user is invalid.
3743
*/
3844
InvalidModelSource = 255,
3945
/**
40-
* Thrown when the number of passed inputs to the model is different than the model metadata specifies.
46+
* Thrown when the number of passed inputs to the model is different than the
47+
* model metadata specifies.
4148
*/
4249
UnexpectedNumInputs = 97,
4350
/**
4451
* Thrown when React Native ExecuTorch threadpool problem occurs.
4552
*/
4653
ThreadPoolError = 113,
4754
/**
48-
* Thrown when a file read operation failed. This could be invalid image url passed to image models, or unsupported format.
55+
* Thrown when a file read operation failed. This could be invalid image url
56+
* passed to image models, or unsupported format.
4957
*/
5058
FileReadFailed = 114,
5159
/**
5260
* Thrown when the size of model output is unexpected.
5361
*/
5462
InvalidModelOutput = 115,
5563
/**
56-
* Thrown when the dimensions of input tensors don't match the model's expected dimensions.
64+
* Thrown when the dimensions of input tensors don't match the model's
65+
* expected dimensions.
5766
*/
5867
WrongDimensions = 116,
5968
/**
@@ -62,7 +71,8 @@ enum class RnExecutorchErrorCode : int32_t {
6271
*/
6372
InvalidUserInput = 117,
6473
/**
65-
* Thrown when the number of downloaded files is unexpected, due to download interruptions.
74+
* Thrown when the number of downloaded files is unexpected, due to download
75+
* interruptions.
6676
*/
6777
DownloadInterrupted = 118,
6878
/**
@@ -75,19 +85,23 @@ enum class RnExecutorchErrorCode : int32_t {
7585
*/
7686
MultilingualConfiguration = 160,
7787
/**
78-
* Thrown when streaming transcription is attempted but audio data chunk is missing.
88+
* Thrown when streaming transcription is attempted but audio data chunk is
89+
* missing.
7990
*/
8091
MissingDataChunk = 161,
8192
/**
82-
* Thrown when trying to stop or insert data into a stream that hasn't been started.
93+
* Thrown when trying to stop or insert data into a stream that hasn't been
94+
* started.
8395
*/
8496
StreamingNotStarted = 162,
8597
/**
86-
* Thrown when trying to start a new streaming session while another is already in progress.
98+
* Thrown when trying to start a new streaming session while another is
99+
* already in progress.
87100
*/
88101
StreamingInProgress = 163,
89102
/**
90-
* Thrown when a resource fails to download. This could be due to invalid URL, or for example a network problem.
103+
* Thrown when a resource fails to download. This could be due to invalid URL,
104+
* or for example a network problem.
91105
*/
92106
ResourceFetcherDownloadFailed = 180,
93107
/**
@@ -103,13 +117,18 @@ enum class RnExecutorchErrorCode : int32_t {
103117
*/
104118
ResourceFetcherAlreadyOngoing = 183,
105119
/**
106-
* Thrown when trying to pause, resume, or cancel a download that is not active.
120+
* Thrown when trying to pause, resume, or cancel a download that is not
121+
* active.
107122
*/
108123
ResourceFetcherNotActive = 184,
109124
/**
110125
* Thrown when required URI information is missing for a download operation.
111126
*/
112127
ResourceFetcherMissingUri = 185,
128+
/**
129+
* Thrown when trying to load resources without fetcher initialization.
130+
*/
131+
ResourceFetcherAdapterNotInitialized = 186,
113132
};
114133

115134
} // namespace rnexecutorch

packages/react-native-executorch/src/controllers/BaseOCRController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export abstract class BaseOCRController {
7676
e &&
7777
typeof e === 'object' &&
7878
'code' in e &&
79-
e.code === RnExecutorchErrorCode.NotImplemented
79+
e.code === RnExecutorchErrorCode.ResourceFetcherAdapterNotInitialized
8080
) {
8181
Logger.error('Load failed:', e);
8282
} else if (this.errorCallback) {

packages/react-native-executorch/src/errors/ErrorCodes.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ export enum RnExecutorchErrorCode {
102102
* Thrown when required URI information is missing for a download operation.
103103
*/
104104
ResourceFetcherMissingUri = 185,
105+
/**
106+
* Thrown when trying to load resources without fetcher initialization.
107+
*/
108+
ResourceFetcherAdapterNotInitialized = 186,
105109
/**
106110
* Status indicating a successful operation.
107111
*/

packages/react-native-executorch/src/utils/ResourceFetcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export class ResourceFetcher {
8686
static getAdapter(): ResourceFetcherAdapter {
8787
if (!this.adapter) {
8888
throw new RnExecutorchError(
89-
RnExecutorchErrorCode.NotImplemented,
89+
RnExecutorchErrorCode.ResourceFetcherAdapterNotInitialized,
9090
'ResourceFetcher adapter is not initialized. Please call initExecutorch({ resourceFetcher: ... }) with a valid adapter, e.g., from @react-native-executorch/expo-resource-fetcher or @react-native-executorch/bare-resource-fetcher. For more details please refer: https://docs.swmansion.com/react-native-executorch/docs/next/fundamentals/loading-models'
9191
);
9292
}

scripts/errors.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ export const errorDefinitions = {
107107
* Thrown when required URI information is missing for a download operation.
108108
*/
109109
ResourceFetcherMissingUri: 0xb9,
110+
/**
111+
* Thrown when trying to load resources without fetcher initialization.
112+
*/
113+
ResourceFetcherAdapterNotInitialized: 0xba,
110114

111115
// ExecuTorch mapped errors
112116
// Based on: https://github.com/pytorch/executorch/blob/main/runtime/core/error.h

0 commit comments

Comments
 (0)