Repository navigation
Expand file tree
/
Copy patherrors.js
More file actions
50 lines (45 loc) · 1.48 KB
/
Copy patherrors.js
File metadata and controls
50 lines (45 loc) · 1.48 KB
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
/* eslint-disable max-classes-per-file -- two small, tightly related error types */
// Body/message markers Verisure's API uses to signal a rate/quota limit, even
// when the HTTP status code alone doesn't say so (e.g. a 200 wrapping a
// GraphQL error, or a vaguely worded 4xx). Learned from production traffic by
// other Verisure client libraries (e.g. python-verisure).
const RATE_LIMIT_MARKERS = [
'aut_00021',
'acc_00002',
'toomanystepuptokens',
'too many step up tokens',
'request limit',
'rate limit',
'too many requests',
];
const containsRateLimitMarker = (text) => {
if (!text) {
return false;
}
return RATE_LIMIT_MARKERS.some((marker) => String(text).toLowerCase().includes(marker));
};
class GraphqlError extends Error {
constructor(errors) {
super();
this.name = 'GraphqlException';
this.message = `GraphQL response contains ${errors.length} errors`;
this.errors = errors;
this.isRateLimited = errors
.some(({ message, data }) => containsRateLimitMarker(message)
|| (data && containsRateLimitMarker(data.errorCode)));
}
}
class HttpError extends Error {
constructor(status, data) {
super(`Request failed with status code ${status}`);
this.name = 'HttpException';
this.response = { status, data };
this.isRateLimited = status === 429
|| containsRateLimitMarker(typeof data === 'string' ? data : JSON.stringify(data || ''));
}
}
module.exports = {
GraphqlError,
HttpError,
containsRateLimitMarker,
};