Skip to content

Commit 010eed7

Browse files
authored
feat(javascript): add error to APIError if possible (#1234)
1 parent 3d0bc99 commit 010eed7

File tree

3 files changed

+61
-17
lines changed

3 files changed

+61
-17
lines changed

clients/algoliasearch-client-javascript/bundlesize.config.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,47 @@
22
"files": [
33
{
44
"path": "packages/algoliasearch/dist/algoliasearch.umd.js",
5-
"maxSize": "7.80KB"
5+
"maxSize": "8.00KB"
66
},
77
{
88
"path": "packages/algoliasearch/dist/lite/lite.umd.js",
9-
"maxSize": "3.50KB"
9+
"maxSize": "3.70KB"
1010
},
1111
{
1212
"path": "packages/client-abtesting/dist/client-abtesting.umd.js",
13-
"maxSize": "3.65KB"
13+
"maxSize": "3.85KB"
1414
},
1515
{
1616
"path": "packages/client-analytics/dist/client-analytics.umd.js",
17-
"maxSize": "4.30KB"
17+
"maxSize": "4.50KB"
1818
},
1919
{
2020
"path": "packages/client-insights/dist/client-insights.umd.js",
21-
"maxSize": "3.50KB"
21+
"maxSize": "3.65KB"
2222
},
2323
{
2424
"path": "packages/client-personalization/dist/client-personalization.umd.js",
25-
"maxSize": "3.60KB"
25+
"maxSize": "3.80KB"
2626
},
2727
{
2828
"path": "packages/client-query-suggestions/dist/client-query-suggestions.umd.js",
29-
"maxSize": "3.70KB"
29+
"maxSize": "3.85KB"
3030
},
3131
{
3232
"path": "packages/client-search/dist/client-search.umd.js",
33-
"maxSize": "6.35KB"
33+
"maxSize": "6.55KB"
3434
},
3535
{
3636
"path": "packages/ingestion/dist/ingestion.umd.js",
37-
"maxSize": "3.50KB"
37+
"maxSize": "4.30KB"
3838
},
3939
{
4040
"path": "packages/predict/dist/predict.umd.js",
41-
"maxSize": "3.50KB"
41+
"maxSize": "4.20KB"
4242
},
4343
{
4444
"path": "packages/recommend/dist/recommend.umd.js",
45-
"maxSize": "3.50KB"
45+
"maxSize": "3.70KB"
4646
}
4747
]
4848
}

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ export class RetryError extends ErrorWithStackTrace {
3535
export class ApiError extends ErrorWithStackTrace {
3636
status: number;
3737

38-
constructor(message: string, status: number, stackTrace: StackFrame[]) {
39-
super(message, stackTrace, 'ApiError');
38+
constructor(
39+
message: string,
40+
status: number,
41+
stackTrace: StackFrame[],
42+
name = 'ApiError'
43+
) {
44+
super(message, stackTrace, name);
4045
this.status = status;
4146
}
4247
}
@@ -49,3 +54,34 @@ export class DeserializationError extends AlgoliaError {
4954
this.response = response;
5055
}
5156
}
57+
58+
export type DetailedErrorWithMessage = {
59+
message: string;
60+
label: string;
61+
};
62+
63+
export type DetailedErrorWithTypeID = {
64+
id: string;
65+
type: string;
66+
name?: string;
67+
};
68+
69+
export type DetailedError = {
70+
code: string;
71+
details?: DetailedErrorWithMessage[] | DetailedErrorWithTypeID[];
72+
};
73+
74+
// DetailedApiError is only used by the ingestion client to return more informative error, other clients will use ApiClient.
75+
export class DetailedApiError extends ApiError {
76+
error: DetailedError;
77+
78+
constructor(
79+
message: string,
80+
status: number,
81+
error: DetailedError,
82+
stackTrace: StackFrame[]
83+
) {
84+
super(message, status, stackTrace, 'DetailedApiError');
85+
this.error = error;
86+
}
87+
}

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
StackFrame,
99
} from '../types';
1010

11-
import { ApiError, DeserializationError } from './errors';
11+
import { ApiError, DeserializationError, DetailedApiError } from './errors';
1212

1313
export function shuffle<TData>(array: TData[]): TData[] {
1414
const shuffledArray = array;
@@ -109,11 +109,19 @@ export function deserializeFailure(
109109
{ content, status }: Response,
110110
stackFrame: StackFrame[]
111111
): Error {
112-
let message = content;
113112
try {
114-
message = JSON.parse(content).message;
113+
const parsed = JSON.parse(content);
114+
if ('error' in parsed) {
115+
return new DetailedApiError(
116+
parsed.message,
117+
status,
118+
parsed.error,
119+
stackFrame
120+
);
121+
}
122+
return new ApiError(parsed.message, status, stackFrame);
115123
} catch (e) {
116124
// ..
117125
}
118-
return new ApiError(message, status, stackFrame);
126+
return new ApiError(content, status, stackFrame);
119127
}

0 commit comments

Comments
 (0)