Skip to content

Commit

Permalink
[backend] Improv error handling in import csv connector
Browse files Browse the repository at this point in the history
  • Loading branch information
RomuDeuxfois committed Oct 18, 2023
1 parent f593267 commit d7eaba9
Showing 1 changed file with 32 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,44 @@ const initImportCsvConnector = () => {

if (stream) {
const chunks: Uint8Array[] = [];
let hasError: boolean = false;
stream.on('data', async (chunk) => {
chunks.push(chunk.toString('utf8'));
}).on('error', (err) => {
throw err;
}).on('error', async (error) => {
hasError = true;
const errorData = {
error: error.message,
source: fileId,
};
await reportExpectation(context, applicantUser, workId, errorData);
})
.on('end', async () => {
const string = chunks.join('');
const bundle = await bundleProcess(context, applicantUser, Buffer.from(string), csvMapper, entity);
await updateExpectationsNumber(context, applicantUser, workId, 1);
if (!hasError) {
const string = chunks.join('');
const bundle = await bundleProcess(context, applicantUser, Buffer.from(string), csvMapper, entity);
await updateExpectationsNumber(context, applicantUser, workId, 1);

const validateBeforeImport = connectorConfig.config.validate_before_import;
if (validateBeforeImport) {
const contentStream = Readable.from([JSON.stringify(bundle, null, ' ')]);
const file = {
createReadStream: () => contentStream,
filename: `${workId}.json`,
mimetype: 'application/json',
};
await upload(context, applicantUser, 'import/pending', file, { entity });
const validateBeforeImport = connectorConfig.config.validate_before_import;
if (validateBeforeImport) {
const contentStream = Readable.from([JSON.stringify(bundle, null, ' ')]);
const file = {
createReadStream: () => contentStream,
filename: `${workId}.json`,
mimetype: 'application/json',
};
await upload(context, applicantUser, 'import/pending', file, { entity });

await reportExpectation(context, applicantUser, workId);
} else {
const content = Buffer.from(JSON.stringify(bundle), 'utf-8').toString('base64');
await pushToSync({
type: 'bundle',
update: true,
applicant_id: applicantId ?? OPENCTI_SYSTEM_UUID,
work_id: workId,
content
});
await reportExpectation(context, applicantUser, workId);
} else {
const content = Buffer.from(JSON.stringify(bundle), 'utf-8').toString('base64');
await pushToSync({
type: 'bundle',
update: true,
applicant_id: applicantId ?? OPENCTI_SYSTEM_UUID,
work_id: workId,
content
});
}
}
});
}
Expand Down

0 comments on commit d7eaba9

Please sign in to comment.