diff --git a/lib/helpers/montaineRequest.js b/lib/helpers/montaineRequest.js index f8833324f..abf4ca1be 100644 --- a/lib/helpers/montaineRequest.js +++ b/lib/helpers/montaineRequest.js @@ -47,7 +47,7 @@ exports.request = async (api, params) => { return res; } catch (err) { if (err.response && err.response.data) return err.response; - throw new AppError(`Distant server (${api.url}) not reachable.`, { code: 'HELPERS_ERROR' }); + throw new AppError(`Distant server (${api.url}) not reachable.`, { code: 'HELPERS_ERROR', details: err }); } }; diff --git a/lib/helpers/montaineTyping.js b/lib/helpers/montaineTyping.js index 358bfe355..997622e3c 100644 --- a/lib/helpers/montaineTyping.js +++ b/lib/helpers/montaineTyping.js @@ -24,7 +24,7 @@ const format = (data, action, object) => { try { return moment(new Date(data)).format(); } catch (err) { - throw new AppError('Typing : format DATE error', { code: 'HELPERS_ERROR' }); + throw new AppError('Typing : format DATE error', { code: 'HELPERS_ERROR', details: err }); } case 'NUMBER': try { @@ -32,13 +32,13 @@ const format = (data, action, object) => { data = String(data).replace(/[^\d.-]/g, ''); // remove if it's not letters return Number(data); } catch (err) { - throw new AppError('Typing : format NUMBER error', { code: 'HELPERS_ERROR' }); + throw new AppError('Typing : format NUMBER error', { code: 'HELPERS_ERROR', details: err }); } case 'STRING': try { return String(data); } catch (err) { - throw new AppError('Typing : format STRING error', { code: 'HELPERS_ERROR' }); + throw new AppError('Typing : format STRING error', { code: 'HELPERS_ERROR', details: err }); } case 'HOUR': try { @@ -47,7 +47,7 @@ const format = (data, action, object) => { if (param) data = `${_.get(object, param)} ${data}`; return moment(new Date(data)).format(); } catch (err) { - throw new AppError('Typing : format HOURS error', { code: 'HELPERS_ERROR' }); + throw new AppError('Typing : format HOURS error', { code: 'HELPERS_ERROR', details: err }); } default: throw new AppError('Typing : format unknown', { code: 'HELPERS_ERROR' }); diff --git a/modules/apis/services/apis.service.js b/modules/apis/services/apis.service.js index 8540b3eef..e9079e63d 100644 --- a/modules/apis/services/apis.service.js +++ b/modules/apis/services/apis.service.js @@ -87,23 +87,29 @@ exports.load = async (api) => { const result = {}; // conf const params = montaineRequest.setParams(api.params); - + // request const request = await montaineRequest.request(api, params); result.request = request; - - // test data + // Typing if (result.request.data && result.request.type === 'success' && api.typing && api.typing !== '') { result.typing = montaineTyping.typing(result.request.data, JSON.parse(api.typing)); + if (!result.typing) { + result.type = 'error'; + result.message = 'Failed data Typing'; + } } - - // test data + // Mapping if (result.typing && api.mapping && api.mapping !== '') { result.mapping = montaineMapping.mapping(result.typing, JSON.parse(api.mapping)); + if (!result.mapping) { + result.type = 'error'; + result.message = 'Failed data Mapping'; + } } - - const history = await HistoryRepository.create(montaineRequest.setScrapHistory(result, api, start)); - api.status = result.type === 'success'; + const history = await HistoryRepository.create(montaineRequest.setScrapHistory(result.request, api, start)); + api.status = result.request.type === 'success'; + console.log(api.status); api.history.push(history._id); api = await ApisRepository.update(api); // return