Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle empty attachments correctly #35

Merged
merged 9 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 2
jobs:
test:
docker:
- image: circleci/node:8-stretch
- image: circleci/node:12-stretch
steps:
- checkout
- restore_cache:
Expand Down Expand Up @@ -41,4 +41,4 @@ workflows:
filters:
branches:
only:
- master
- master
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## 1.3.1 (May 13, 2020)
## 1.3.1 (May 22, 2020)

* Update sailor version to 2.6.7
* Correctly handle incoming attachments that are empty.
* Update dependencies.
* Add debug log statements.

## 1.3.0 (April 23, 2020)

Expand Down
11 changes: 10 additions & 1 deletion lib/actions/attachmentToJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,26 @@ module.exports.process = async function processAction(msg, cfg) {
// eslint-disable-next-line no-await-in-loop
const response = await new AttachmentProcessor().getAttachment(attachment.url, 'arraybuffer');

this.logger.debug(`For filename ${fileName} Response status: ${response.status} Response headers: ${JSON.stringify(response.headers)}`);

if (response.status >= 400) {
throw new Error(`Error in making request to ${attachment.url}
Status code: ${response.status},
Status code: ${response.status},
Body: ${Buffer.from(response.data, 'binary').toString('base64')}`);
}

const responseBodyString = Buffer.from(response.data, 'binary').toString('utf-8');

if (!responseBodyString) {
throw new Error(`Empty attachment received for file ${fileName}`);
}

this.logger.debug(`For filename ${fileName} Response Body string: ${responseBodyString}`);
fileSize = sizeof(responseBodyString);

if (fileSize < MAX_FILE_SIZE) {
const returnMsg = await xml2Json.process(this, responseBodyString);
this.logger.debug(`For filename ${fileName} Result of XML Conversion: ${JSON.stringify(returnMsg)}`);
foundXML = true;
await self.emit('data', messages.newMessageWithBody(returnMsg.body));
} else {
Expand Down
4 changes: 4 additions & 0 deletions lib/xml2Json.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const invalidXmlMessage = 'Given XML is not valid or the file can not be read. '
+ 'See XML naming rules https://www.w3schools.com/xml/xml_elements.asp';

module.exports.process = async function xml2Json(self, xmlString) {
if (!xmlString) {
throw new Error('XML string is missing');
}

const parser = new xml2js.Parser({
trim: false,
normalize: false,
Expand Down
Loading