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

Show error if selected CSV file contains null character #982

Merged
merged 2 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions src/components/entity/upload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ const parseEntities = async (file, headerResults, signal) => {
warnings.value = results.warnings;
};
const selectFile = (file) => {
alert.blank();
headerErrors.value = null;
dataError.value = null;

Expand Down
1 change: 1 addition & 0 deletions src/locales/en.json5
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@
"csv": {
// {message} is a description of the problem.
"readError": "There was a problem reading your file: {message}",
"nullChar": "The file “{name}” is not a valid .csv file. It cannot be read.",
// {row} is a row number. {message} is a description of the problem.
"rowError": "There is a problem on row {row} of the file: {message}",
// This is an error that is shown for a spreadsheet. The field may be any
Expand Down
11 changes: 11 additions & 0 deletions src/util/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ export const parseCSVHeader = async (i18n, file, signal = undefined) => {
preview: 1
});
const columns = data.length !== 0 ? data[0] : [];
if (columns.some(column => column.includes('\0')))
throw new Error(i18n.t('util.csv.nullChar', { name: file.name }));
const unhandledErrors = errors.filter(error =>
error.code !== 'UndetectableDelimiter');
if (unhandledErrors.length === 0) {
Expand Down Expand Up @@ -200,6 +202,12 @@ export const parseCSV = async (i18n, file, columns, options = {}) => {
throw new Error(i18n.tc('util.csv.dataWithoutHeader', columns.length, counts));
}

if (values.some(value => value.includes('\0'))) {
const error = new Error(i18n.t('util.csv.nullChar', { name: file.name }));
error.row = NaN;
throw error;
}

data.push(transformRow(values, columns));
for (const warning of warnings) warning.pushRow(values, index, columns);
};
Expand Down Expand Up @@ -235,6 +243,9 @@ export const parseCSV = async (i18n, file, columns, options = {}) => {
worker: true
});
} catch (error) {
// Mention the row number in the error message unless the error has
// set its `row` property to NaN.
if (Number.isNaN(error.row)) throw error;
throw new Error(i18n.t('util.csv.rowError', {
message: error.message,
row: i18n.n((error.row ?? rowIndex) + 1, 'default')
Expand Down
29 changes: 29 additions & 0 deletions test/components/entity/upload.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,35 @@ describe('EntityUpload', () => {
});
});

describe('binary file', () => {
beforeEach(() => {
testData.extendedDatasets.createPast(1);
});

it('shows an alert for a null character in the header', async () => {
const modal = await showModal();
await selectFile(modal, createCSV('f\0o'));
modal.should.alert('danger', 'The file “my_data.csv” is not a valid .csv file. It cannot be read.');
modal.findComponent(EntityUploadHeaderErrors).exists().should.be.false();
});

it('hides the alert after a valid file is selected', async () => {
const modal = await showModal();
await selectFile(modal, createCSV('f\0o'));
await selectFile(modal);
modal.should.not.alert();
modal.findComponent(EntityUploadPopup).exists().should.be.true();
});

it('renders EntityUploadDataError for a null character after header', async () => {
const modal = await showModal();
await selectFile(modal, createCSV('label\nf\0o'));
const { message } = modal.getComponent(EntityUploadDataError).props();
message.should.equal('The file “my_data.csv” is not a valid .csv file. It cannot be read.');
modal.should.not.alert();
});
});

describe('warnings', () => {
beforeEach(() => {
testData.extendedDatasets.createPast(1, {
Expand Down
12 changes: 11 additions & 1 deletion test/unit/csv.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ describe('util/csv', () => {
});
});

describe('error', () => {
it('returns a rejected promise if there is a null character', () => {
const promise = parseCSVHeader(i18n, createCSV('f\0o,bar'));
return promise.should.be.rejectedWith('The file “my_data.csv” is not a valid .csv file. It cannot be read.');
});

describe('Papa Parse error', () => {
it('returns an error for a missing quote', async () => {
const { errors } = await parseCSVHeader(i18n, createCSV('"a\n1'));
errors.length.should.equal(1);
Expand Down Expand Up @@ -119,6 +124,11 @@ describe('util/csv', () => {
});
});

it('returns a rejected promise if there is a null character', () => {
const promise = parseCSV(i18n, createCSV('a\nf\0o'), ['a']);
return promise.should.be.rejectedWith('The file “my_data.csv” is not a valid .csv file. It cannot be read.');
});

describe('number of cells', () => {
it('allows a row to be ragged', async () => {
const csv = createCSV('a,b\n1,2\n3');
Expand Down
3 changes: 3 additions & 0 deletions transifex/strings_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,9 @@
"string": "There was a problem reading your file: {message}",
"developer_comment": "{message} is a description of the problem."
},
"nullChar": {
"string": "The file “{name}” is not a valid .csv file. It cannot be read."
},
"rowError": {
"string": "There is a problem on row {row} of the file: {message}",
"developer_comment": "{row} is a row number. {message} is a description of the problem."
Expand Down