Skip to content

Commit cc42ba3

Browse files
Fix error handling for attachments and provide example of delays and streaming in parallel (#54)
# Description <!-- A brief description of what the PR does/changes. Use active voice and present tense, e.g., This PR fixes ... --> This PR provides better example of error handling while streaming attachments. Adds handling of delays and provides an example of streaming attachments in parallel. ## Connected Issues <!-- DevRev issue(s) full link(s) (e.g. https://app.devrev.ai/devrev/works/ISS-123). --> https://app.devrev.ai/devrev/works/ISS-217174 ## Checklist - [x] Tests added/updated and ran with `npm run test` OR no tests needed. - [x] Code formatted and checked with `npm run lint`. - [x] Added "How to test" section to the description OR this section is not needed.
1 parent 6a9f73e commit cc42ba3

File tree

3 files changed

+23
-27
lines changed

3 files changed

+23
-27
lines changed

code/package-lock.json

Lines changed: 4 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"yargs": "^17.6.2"
5757
},
5858
"dependencies": {
59-
"@devrev/ts-adaas": "1.9.0",
59+
"@devrev/ts-adaas": "1.11.0",
6060
"@devrev/typescript-sdk": "1.1.63",
6161
"axios": "^1.9.0",
6262
"dotenv": "^16.0.3",

code/src/functions/extraction/workers/attachments-extraction.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import {
55
ExternalSystemAttachmentStreamingResponse,
66
ExtractorEventType,
77
processTask,
8-
serializeAxiosError,
98
} from '@devrev/ts-adaas';
109

1110
// TODO: Replace with function for fetching attachment streams from the
12-
// external system. This function should return a stream of the attachment data.
11+
// external system. This function should return either a stream of the
12+
// attachment data, a delay or an error.
1313
async function getFileStream({
1414
item,
1515
}: ExternalSystemAttachmentStreamingParams): Promise<ExternalSystemAttachmentStreamingResponse> {
@@ -20,22 +20,28 @@ async function getFileStream({
2020
responseType: 'stream',
2121
headers: {
2222
'Accept-Encoding': 'identity',
23+
timeout: 30000,
2324
},
2425
});
2526

2627
return { httpStream: fileStreamResponse };
2728
} catch (error) {
2829
if (axios.isAxiosError(error)) {
29-
console.warn(`Error while fetching attachment ${id} from URL.`, serializeAxiosError(error));
30-
console.warn('Failed attachment metadata', item);
31-
} else {
32-
console.warn(`Error while fetching attachment ${id} from URL.`, error);
33-
console.warn('Failed attachment metadata', item);
30+
if (error?.response?.status === 429) {
31+
const retryAfter = error.response?.headers['retry-after'];
32+
return { delay: retryAfter };
33+
} else {
34+
return {
35+
error: {
36+
message: `Error while fetching attachment ${id} from URL. Error code: ${error.response?.status}. Error message: ${error.response?.data.message}.`,
37+
},
38+
};
39+
}
3440
}
3541

3642
return {
3743
error: {
38-
message: `Failed to fetch attachment ${id} from URL.`,
44+
message: `Unknown error while fetching attachment ${id} from URL. Error: ${error}.`,
3945
},
4046
};
4147
}
@@ -46,6 +52,10 @@ processTask({
4652
try {
4753
const response = await adapter.streamAttachments({
4854
stream: getFileStream,
55+
56+
// TODO: If needed you can specify how many attachments to stream at
57+
// once. Minimum is 1 and maximum is 50.
58+
// batchSize: 10,
4959
});
5060

5161
if (response?.delay) {

0 commit comments

Comments
 (0)