forked from pubkey/rxdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrx-error.ts
161 lines (149 loc) · 4.03 KB
/
rx-error.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* here we use custom errors with the additional field 'parameters'
*/
import { overwritable } from './overwritable.ts';
import type {
RxErrorParameters,
RxErrorKey,
RxStorageWriteError,
RxStorageWriteErrorConflict
} from './types/index.d.ts';
/**
* transform an object of parameters to a presentable string
*/
function parametersToString(parameters: any): string {
let ret = '';
if (Object.keys(parameters).length === 0)
return ret;
ret += 'Given parameters: {\n';
ret += Object.keys(parameters)
.map(k => {
let paramStr = '[object Object]';
try {
if (k === 'errors') {
paramStr = parameters[k].map((err: any) => JSON.stringify(err, Object.getOwnPropertyNames(err)));
} else {
paramStr = JSON.stringify(parameters[k], function (_k, v) {
return v === undefined ? null : v;
}, 2);
}
} catch (e) { }
return k + ':' + paramStr;
})
.join('\n');
ret += '}';
return ret;
}
function messageForError(
message: string,
code: string,
parameters: any
): string {
return 'RxError (' + code + '):' + '\n' +
message + '\n' +
parametersToString(parameters);
}
export class RxError extends Error {
public code: RxErrorKey;
public message: string;
public parameters: RxErrorParameters;
// always true, use this to detect if its an rxdb-error
public rxdb: true;
constructor(
code: RxErrorKey,
message: string,
parameters: RxErrorParameters = {}
) {
const mes = messageForError(message, code, parameters);
super(mes);
this.code = code;
this.message = mes;
this.parameters = parameters;
this.rxdb = true; // tag them as internal
}
get name(): string {
return 'RxError (' + this.code + ')';
}
toString(): string {
return this.message;
}
get typeError(): boolean {
return false;
}
}
export class RxTypeError extends TypeError {
public code: RxErrorKey;
public message: string;
public parameters: RxErrorParameters;
// always true, use this to detect if its an rxdb-error
public rxdb: true;
constructor(
code: RxErrorKey,
message: string,
parameters: RxErrorParameters = {}
) {
const mes = messageForError(message, code, parameters);
super(mes);
this.code = code;
this.message = mes;
this.parameters = parameters;
this.rxdb = true; // tag them as internal
}
get name(): string {
return 'RxTypeError (' + this.code + ')';
}
toString(): string {
return this.message;
}
get typeError(): boolean {
return true;
}
}
export function newRxError(
code: RxErrorKey,
parameters?: RxErrorParameters
): RxError {
return new RxError(
code,
overwritable.tunnelErrorMessage(code),
parameters
);
}
export function newRxTypeError(
code: RxErrorKey,
parameters?: RxErrorParameters
): RxTypeError {
return new RxTypeError(
code,
overwritable.tunnelErrorMessage(code),
parameters
);
}
/**
* Returns the error if it is a 409 conflict,
* return false if it is another error.
*/
export function isBulkWriteConflictError<RxDocType>(
err?: RxStorageWriteError<RxDocType> | any
): RxStorageWriteErrorConflict<RxDocType> | false {
if (
err &&
err.status === 409
) {
return err;
} else {
return false;
}
}
const STORAGE_WRITE_ERROR_CODE_TO_MESSAGE: { [k: number]: string; } = {
409: 'document write conflict',
422: 'schema validation error',
510: 'attachment data missing'
};
export function rxStorageWriteErrorToRxError(err: RxStorageWriteError<any>): RxError {
return newRxError('COL20', {
name: STORAGE_WRITE_ERROR_CODE_TO_MESSAGE[err.status],
document: err.documentId,
writeError: err
});
}