Skip to content

Commit 201b5ad

Browse files
committed
add AlgoliaError
1 parent d09b3e7 commit 201b5ad

File tree

2 files changed

+20
-9
lines changed
  • clients/algoliasearch-client-javascript/packages/client-common/src/transporter
  • playground/javascript/node

2 files changed

+20
-9
lines changed

clients/algoliasearch-client-javascript/packages/client-common/src/transporter/errors.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
import type { Response, StackFrame } from '../types';
22

3-
export class ErrorWithStackTrace extends Error {
4-
stackTrace: StackFrame[];
3+
export class AlgoliaError extends Error {
54
name: string;
65

7-
constructor(message: string, stackTrace: StackFrame[], name: string) {
6+
constructor(message: string, name: string) {
87
super(message);
8+
this.name = name ?? 'AlgoliaError';
9+
}
10+
}
11+
12+
export class ErrorWithStackTrace extends AlgoliaError {
13+
stackTrace: StackFrame[];
14+
15+
constructor(message: string, stackTrace: StackFrame[], name: string) {
16+
super(message, name);
917
// the array and object should be frozen to reflect the stackTrace at the time of the error
1018
this.stackTrace = stackTrace;
11-
this.name = name ?? 'ErrorWithStackTrace';
1219
}
1320
}
1421

@@ -31,13 +38,11 @@ export class ApiError extends ErrorWithStackTrace {
3138
}
3239
}
3340

34-
export class DeserializationError extends Error {
41+
export class DeserializationError extends AlgoliaError {
3542
response: Response;
36-
name: string;
3743

3844
constructor(message: string, response: Response) {
39-
super(message);
45+
super(message, 'DeserializationError');
4046
this.response = response;
41-
this.name = 'DeserializationError';
4247
}
4348
}

playground/javascript/node/search.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,17 @@ async function testSearch() {
2828
});
2929

3030
console.log(`[OK]`, res);
31-
} catch (e) {
31+
} catch (e: any) {
32+
// Instance of
3233
if (e instanceof ApiError) {
3334
return console.log(`[${e.status}] ${e.message}`, e.stackTrace);
3435
}
3536

37+
// Other way
38+
if (e.name === 'RetryError') {
39+
return console.log(`[${e.name}] ${e.message}`, e.stackTrace);
40+
}
41+
3642
console.log('[ERROR]', e);
3743
}
3844
}

0 commit comments

Comments
 (0)