Skip to content

Commit 3d67547

Browse files
committed
fix(async-storage): prefer enum to namespace
1 parent d9516ec commit 3d67547

File tree

1 file changed

+43
-38
lines changed

1 file changed

+43
-38
lines changed
Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,53 @@
1+
enum AsyncStorageErrorType {
2+
/**
3+
* Related to RN Native module itself, ex. not initialized or null at app boot
4+
*/
5+
NativeModuleError = "NativeModuleError",
6+
7+
/**
8+
* Error thrown from Sqlite itself
9+
* https://www.sqlite.org/rescode.html
10+
*/
11+
SqliteStorageError = "SqliteStorageError",
12+
13+
/**
14+
* Other errors related to Native Shared Storage
15+
* ex. Storage could not be initialized
16+
*/
17+
OtherStorageError = "OtherStorageError",
18+
19+
/**
20+
* Catch-all case, where we can't really tell what went wrong
21+
*/
22+
UnknownError = "UnknownError",
23+
}
24+
125
export class AsyncStorageError extends Error {
226
private constructor(
327
public errorMessage: string,
4-
public type: AsyncStorageError.Type
28+
public type: AsyncStorageErrorType
529
) {
630
super(errorMessage);
731
this.name = this.constructor.name;
832
}
933

10-
static nativeError(e: any): AsyncStorageError {
34+
static nativeError(e: unknown): AsyncStorageError {
1135
const error = getNativeError(e);
1236
if (!error) {
1337
return new AsyncStorageError(
14-
e?.message ?? `Unknown error ${e}`,
15-
AsyncStorageError.Type.UnknownError
38+
(e as { message?: string })?.message ?? `Unknown error ${e}`,
39+
AsyncStorageErrorType.UnknownError
1640
);
1741
}
1842

19-
let errorType: AsyncStorageError.Type = AsyncStorageError.Type.UnknownError;
43+
let errorType: AsyncStorageErrorType = AsyncStorageErrorType.UnknownError;
2044

2145
switch (error.type) {
2246
case "SqliteException":
23-
errorType = AsyncStorageError.Type.SqliteStorageError;
47+
errorType = AsyncStorageErrorType.SqliteStorageError;
2448
break;
2549
case "OtherException":
26-
errorType = AsyncStorageError.Type.OtherStorageError;
50+
errorType = AsyncStorageErrorType.OtherStorageError;
2751
break;
2852
}
2953

@@ -32,49 +56,30 @@ export class AsyncStorageError extends Error {
3256

3357
static jsError(
3458
error: string,
35-
type: AsyncStorageError.Type
59+
type: AsyncStorageErrorType
3660
): AsyncStorageError {
3761
return new AsyncStorageError(error, type);
3862
}
39-
}
4063

41-
export namespace AsyncStorageError {
42-
export enum Type {
43-
/**
44-
* Related to RN Native module itself, ex. not initialized or null at app boot
45-
*/
46-
NativeModuleError = "NativeModuleError",
47-
48-
/**
49-
* Error thrown from Sqlite itself
50-
* https://www.sqlite.org/rescode.html
51-
*/
52-
SqliteStorageError = "SqliteStorageError",
53-
54-
/**
55-
* Other errors related to Native Shared Storage
56-
* ex. Storage could not be initialized
57-
*/
58-
OtherStorageError = "OtherStorageError",
59-
60-
/**
61-
* Catch-all case, where we can't really tell what went wrong
62-
*/
63-
UnknownError = "UnknownError",
64-
}
64+
static Type = AsyncStorageErrorType;
6565
}
6666

6767
// Native module reject promises with special code
68-
function isNativeError(e: any): e is PotentialNativeError {
69-
return "message" in e && "code" in e && e?.code === "AsyncStorageError";
68+
function isNativeError(e: unknown): e is PotentialNativeError {
69+
if (typeof e !== "object") {
70+
return false;
71+
}
72+
73+
const err = e as Record<string, string>;
74+
return !!err.message && err?.code === "AsyncStorageError";
7075
}
7176

72-
function getNativeError(e: any): AsyncStorageNativeError | null {
77+
function getNativeError(e: unknown): AsyncStorageNativeError | null {
7378
if (!isNativeError(e)) {
7479
return null;
7580
}
7681

77-
let errorType = e.userInfo ? e.userInfo["type"] : null;
82+
const errorType = e.userInfo ? e.userInfo["type"] : null;
7883

7984
if (errorType === "SqliteException") {
8085
return {
@@ -99,5 +104,5 @@ type AsyncStorageNativeError = {
99104
type PotentialNativeError = {
100105
message: string;
101106
code: "AsyncStorageError";
102-
userInfo: Record<string, any> | null;
107+
userInfo: Record<string, unknown> | null;
103108
};

0 commit comments

Comments
 (0)