Skip to content

Commit

Permalink
feat: add option to get the original exception
Browse files Browse the repository at this point in the history
  • Loading branch information
onhate committed Aug 1, 2024
1 parent cfc37f9 commit 4d4f9e4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 35 deletions.
58 changes: 23 additions & 35 deletions src/clients/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const {
* @property {string} [asUser] - Optional header for making requests on behalf of a user.
* @property {object} [customHeaders] - Any additional custom headers for the request.
* @property {boolean} [throttle] - Flag to enable throttling of requests.
* @property {boolean} [throwOriginalException] - Throw the original exception when API requests fail.
* @property {CustomEventTarget} eventTarget - Event target to handle custom events.
* @property {Array} sideLoad - Array to handle side-loaded resources.
* @property {Array} jsonAPINames - Array to hold names used in the JSON API.
Expand Down Expand Up @@ -166,11 +167,7 @@ class Client {
!Array.isArray(args.at(-1)) &&
args.pop();

try {
return await this.transporter.request(method, uri, body);
} catch (error) {
throw new Error(`Raw request failed: ${error.message}`);
}
return await this.transporter.request(method, uri, body);

Check failure on line 170 in src/clients/client.js

View workflow job for this annotation

GitHub Actions / build-and-deploy

Redundant use of `await` on a return value.
}

/**
Expand All @@ -197,6 +194,10 @@ class Client {
);
return {response, result: responseBody};
} catch (error) {
if (this.options.throwOriginalException) {
throw error;
}

const {
message,
result: {error: {title = '', message: errorMessage = ''} = {}} = {},
Expand Down Expand Up @@ -244,43 +245,30 @@ class Client {

const fetchPagesRecursively = async (pageUri) => {
const isIncremental = pageUri.includes('incremental');

try {
const responseData = await __request.call(
this,
method,
pageUri,
...args,
);
const nextPage = processPage(responseData);
if (
nextPage &&
(!isIncremental ||
(responseData.response && responseData.response.count >= 1000))
) {
return fetchPagesRecursively(nextPage);
}
} catch (error) {
throw new Error(`Request all failed during fetching: ${error.message}`);
const responseData = await __request.call(

Check failure on line 248 in src/clients/client.js

View workflow job for this annotation

GitHub Actions / build-and-deploy

Replace `⏎········this,⏎········method,⏎········pageUri,⏎········...args,⏎······` with `this,·method,·pageUri,·...args`
this,
method,
pageUri,
...args,
);
const nextPage = processPage(responseData);
if (
nextPage &&
(!isIncremental ||
(responseData.response && responseData.response.count >= 1000))
) {
return fetchPagesRecursively(nextPage);
}
};

try {
await fetchPagesRecursively(uri);
return flatten(bodyList);
} catch (error) {
throw new Error(`RequestAll processing failed: ${error.message}`);
}
await fetchPagesRecursively(uri);
return flatten(bodyList);
}

// Request method for uploading files
async requestUpload(uri, file) {
try {
const {response, result} = await this.transporter.upload(uri, file);
return checkRequestResponse(response, result);
} catch (error) {
throw new Error(`Upload failed: ${error.message}`);
}
const {response, result} = await this.transporter.upload(uri, file);
return checkRequestResponse(response, result);
}
}

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

0 comments on commit 4d4f9e4

Please # to comment.