Skip to content

Commit 4e55a32

Browse files
committed
Merge with format changes
2 parents bc4ebb4 + 7b2054d commit 4e55a32

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { FetchError } from '../types';
2+
import { MeiliSearchError } from './meilisearch-error';
3+
4+
class MeiliSearchCommunicationError extends MeiliSearchError {
5+
statusCode?: number;
6+
errno?: string;
7+
code?: string;
8+
stack?: string;
9+
10+
constructor(
11+
message: string,
12+
body: Response | FetchError,
13+
url?: string,
14+
stack?: string,
15+
) {
16+
super(message);
17+
18+
// Make errors comparison possible. ex: error instanceof MeiliSearchCommunicationError.
19+
Object.setPrototypeOf(this, MeiliSearchCommunicationError.prototype);
20+
21+
this.name = 'MeiliSearchCommunicationError';
22+
23+
if (body instanceof Response) {
24+
this.message = body.statusText;
25+
this.statusCode = body.status;
26+
}
27+
if (body instanceof Error) {
28+
this.errno = body.errno;
29+
this.code = body.code;
30+
}
31+
if (stack) {
32+
this.stack = stack;
33+
this.stack = this.stack?.replace(/(TypeError|FetchError)/, this.name);
34+
this.stack = this.stack?.replace(
35+
'Failed to fetch',
36+
`request to ${url} failed, reason: connect ECONNREFUSED`,
37+
);
38+
this.stack = this.stack?.replace('Not Found', `Not Found: ${url}`);
39+
} else {
40+
if (Error.captureStackTrace) {
41+
Error.captureStackTrace(this, MeiliSearchCommunicationError);
42+
}
43+
}
44+
}
45+
}
46+
47+
export { MeiliSearchCommunicationError };

0 commit comments

Comments
 (0)