Skip to content

Commit

Permalink
Merge 3e26c61 into c8e357d
Browse files Browse the repository at this point in the history
  • Loading branch information
viniagostini committed Apr 29, 2019
2 parents c8e357d + 3e26c61 commit 2919f9b
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/api_error.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const httpStatus = require('http-status');

class APIError extends Error {
constructor(message, status = httpStatus.INTERNAL_SERVER_ERROR, stack) {
constructor(message, status = httpStatus.INTERNAL_SERVER_ERROR, errorCode, stack) {
super(message);
this.name = this.constructor.name;
this.message = message;
this.status = status;
this.errorCode = errorCode;
this.stack = stack;
this.stack || Error.captureStackTrace(this, this.constructor.name);
}
Expand Down
14 changes: 14 additions & 0 deletions src/error_messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
HEADER_SIZE_ERROR: (expectedSize, foundSize, sheetName) => ({
message: `The ${sheetName} sheet header should have the size (${expectedSize}) but found (${foundSize})`,
code: 1
}),
FETCH_SPREADSHEET_ERROR: (fetchingError, spreadsheetUrl) => ({
message: `The following error was found when fetching this spreadsheet: (${spreadsheetUrl}) [${fetchingError}]`,
code: 2
}),
XLSX_TO_JSON_ERROR: parsingError => ({
message: `The following error was found when converting the spreadsheet to JSON: [${parsingError}]`,
code: 3
}),
}
9 changes: 8 additions & 1 deletion src/parser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const { containsSubstring } = require('./string_utils');
const errorMessages = require('./error_messages');
const APIError = require('./api_error');
const httpStatus = require('http-status');

/**
* Keywords contained on each name or title.
Expand Down Expand Up @@ -147,7 +150,6 @@ const _getSubsidioData = spreadSheet => {
});
};


/**
* Get the indenizacoes data from the spreadsheet.
*
Expand All @@ -159,6 +161,11 @@ const _getIndenizacoesData = spreadSheet => {
const sheetkey = 'indenizacoes';
const fixedColsSize = 9;
const sheet = _getSheet(INDENIZACOES_KEYWORD, spreadSheet);
const headerSize = _getHeader(sheet).length;
if (headerSize < fixedColsSize) {
const { message, code } = errorMessages.HEADER_SIZE_ERROR(fixedColsSize, headerSize, sheetkey);
throw new APIError(message, httpStatus.BAD_REQUEST, code);
}

const outraCols = _getOutraAndDetalheColumns(sheet, sheetkey, fixedColsSize);

Expand Down
4 changes: 3 additions & 1 deletion src/parser_service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const http = require('http');
const xlsxService = require('./xlsx_service');
const APIError = require('./api_error');
const errorMessages = require('./error_messages');
const httpStatus = require('http-status');

/**
Expand All @@ -23,7 +24,8 @@ const _fetchSpreadshet = url =>
});
})
.on('error', err => {
reject(new APIError(err.message, httpStatus.BAD_REQUEST, err.stack));
const {message, code} = errorMessages.FETCH_SPREADSHEET_ERROR(err, url);
reject(new APIError(message, httpStatus.BAD_REQUEST, code, err.stack));
});
});

Expand Down
5 changes: 3 additions & 2 deletions src/xlsx_service.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const xlsx = require('xlsx');
const APIError = require('./api_error');
const httpStatus = require('http-status');

const errorMessages = require('./error_messages');
const convertSpreadsheetToJson = spreadsheetBuffer => {
try {
const workbook = xlsx.read(spreadsheetBuffer, { type: 'buffer' });
Expand All @@ -11,7 +11,8 @@ const convertSpreadsheetToJson = spreadsheetBuffer => {
});
return spreadsheetObj;
} catch (e) {
throw new APIError(e.message, httpStatus.BAD_REQUEST, e.stack);
const {message, code} = errorMessages.XLSX_TO_JSON_ERROR(e);
throw new APIError(message, httpStatus.BAD_REQUEST, code, e.stack);
}
};

Expand Down
Binary file added test/assets/indenizacoes_headers/header14.xlsx
Binary file not shown.
16 changes: 15 additions & 1 deletion test/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ const {
INDENIZACOES_HEADERS_1, INDENIZACOES_HEADERS_2, INDENIZACOES_HEADERS_3, INDENIZACOES_HEADERS_4,
INDENIZACOES_HEADERS_5, INDENIZACOES_HEADERS_6, INDENIZACOES_HEADERS_7, INDENIZACOES_HEADERS_8,
INDENIZACOES_HEADERS_9, INDENIZACOES_HEADERS_10, INDENIZACOES_HEADERS_11, INDENIZACOES_HEADERS_12,
INDENIZACOES_HEADERS_13, INDENIZACOES_HEADERS_14, INDENIZACOES_HEADERS_15, INDENIZACOES_HEADERS_16
INDENIZACOES_HEADERS_13, INDENIZACOES_HEADERS_14
} = require('./spreadsheets');
const { convertSpreadsheetToJson } = require('../src/xlsx_service');
const errorMessages = require('../src/error_messages');
const httpStatus = require('http-status');

describe('paser parse', () => {
it('shoul throw not implemented error', () => {
Expand Down Expand Up @@ -589,4 +591,16 @@ describe('parser _getIndenizacoesData', () => {
async () => {
await testIndenizacoesData(INDENIZACOES_HEADERS_13, regularIndenizacoesData);
});

it('should throw an APIError when the header is: [cpf,nome]', async () => {
try {
await testIndenizacoesData(INDENIZACOES_HEADERS_14, {});
fail('an error should have been thrown');
} catch (e) {
const {message, code} = errorMessages.HEADER_SIZE_ERROR(9, 2, 'indenizacoes');
expect(e.message).toEqual(message);
expect(e.errorCode).toEqual(code);
expect(e.status).toEqual(httpStatus.BAD_REQUEST);
}
});
});
2 changes: 0 additions & 2 deletions test/spreadsheets.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,5 @@ module.exports = {
INDENIZACOES_HEADERS_12: '/assets/indenizacoes_headers/header12.xlsx',
INDENIZACOES_HEADERS_13: '/assets/indenizacoes_headers/header13.xlsx',
INDENIZACOES_HEADERS_14: '/assets/indenizacoes_headers/header14.xlsx',
INDENIZACOES_HEADERS_15: '/assets/indenizacoes_headers/header15.xlsx',
INDENIZACOES_HEADERS_16: '/assets/indenizacoes_headers/header16.xlsx',
getSpreadsheet: path => readFileAsync(__dirname + path)
};
2 changes: 1 addition & 1 deletion test/xlsx_service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('xlsx_service convertpreadsheetToJson should', () => {
} catch (e) {
expect(e instanceof APIError).toBe(true);
expect(e.status).toBe(httpStatus.BAD_REQUEST);
expect(e.message).toBe('File is password-protected');
expect(e.message).toBe('The following error was found when converting the spreadsheet to JSON: [Error: File is password-protected]');
}
});
});

0 comments on commit 2919f9b

Please sign in to comment.