File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change 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 ( / ( T y p e E r r o r | F e t c h E r r o r ) / , 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 } ;
You can’t perform that action at this time.
0 commit comments