Skip to content

Commit 4d4f9e4

Browse files
committed
feat: add option to get the original exception
1 parent cfc37f9 commit 4d4f9e4

File tree

2 files changed

+24
-35
lines changed

2 files changed

+24
-35
lines changed

src/clients/client.js

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const {
2121
* @property {string} [asUser] - Optional header for making requests on behalf of a user.
2222
* @property {object} [customHeaders] - Any additional custom headers for the request.
2323
* @property {boolean} [throttle] - Flag to enable throttling of requests.
24+
* @property {boolean} [throwOriginalException] - Throw the original exception when API requests fail.
2425
* @property {CustomEventTarget} eventTarget - Event target to handle custom events.
2526
* @property {Array} sideLoad - Array to handle side-loaded resources.
2627
* @property {Array} jsonAPINames - Array to hold names used in the JSON API.
@@ -166,11 +167,7 @@ class Client {
166167
!Array.isArray(args.at(-1)) &&
167168
args.pop();
168169

169-
try {
170-
return await this.transporter.request(method, uri, body);
171-
} catch (error) {
172-
throw new Error(`Raw request failed: ${error.message}`);
173-
}
170+
return await this.transporter.request(method, uri, body);
174171
}
175172

176173
/**
@@ -197,6 +194,10 @@ class Client {
197194
);
198195
return {response, result: responseBody};
199196
} catch (error) {
197+
if (this.options.throwOriginalException) {
198+
throw error;
199+
}
200+
200201
const {
201202
message,
202203
result: {error: {title = '', message: errorMessage = ''} = {}} = {},
@@ -244,43 +245,30 @@ class Client {
244245

245246
const fetchPagesRecursively = async (pageUri) => {
246247
const isIncremental = pageUri.includes('incremental');
247-
248-
try {
249-
const responseData = await __request.call(
250-
this,
251-
method,
252-
pageUri,
253-
...args,
254-
);
255-
const nextPage = processPage(responseData);
256-
if (
257-
nextPage &&
258-
(!isIncremental ||
259-
(responseData.response && responseData.response.count >= 1000))
260-
) {
261-
return fetchPagesRecursively(nextPage);
262-
}
263-
} catch (error) {
264-
throw new Error(`Request all failed during fetching: ${error.message}`);
248+
const responseData = await __request.call(
249+
this,
250+
method,
251+
pageUri,
252+
...args,
253+
);
254+
const nextPage = processPage(responseData);
255+
if (
256+
nextPage &&
257+
(!isIncremental ||
258+
(responseData.response && responseData.response.count >= 1000))
259+
) {
260+
return fetchPagesRecursively(nextPage);
265261
}
266262
};
267263

268-
try {
269-
await fetchPagesRecursively(uri);
270-
return flatten(bodyList);
271-
} catch (error) {
272-
throw new Error(`RequestAll processing failed: ${error.message}`);
273-
}
264+
await fetchPagesRecursively(uri);
265+
return flatten(bodyList);
274266
}
275267

276268
// Request method for uploading files
277269
async requestUpload(uri, file) {
278-
try {
279-
const {response, result} = await this.transporter.upload(uri, file);
280-
return checkRequestResponse(response, result);
281-
} catch (error) {
282-
throw new Error(`Upload failed: ${error.message}`);
283-
}
270+
const {response, result} = await this.transporter.upload(uri, file);
271+
return checkRequestResponse(response, result);
284272
}
285273
}
286274

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const {ZendeskClientVoice} = require('./clients/voice');
2020
* @property {string} [asUser] - Optional header for requests on behalf of a user.
2121
* @property {object} [customHeaders] - Additional custom headers for the request.
2222
* @property {boolean} [throttle] - Enables request throttling.
23+
* @property {boolean} [throwOriginalException] - Throw the original exception when API requests fail.
2324
* @property {boolean} [debug=false] - Enables or disables debug logging.
2425
* @property {object} [logger=ConsoleLogger] - Logger for logging. Defaults to a basic console logger.
2526
* @property {object} [transportConfig] - Configuration for custom transport.

0 commit comments

Comments
 (0)