diff --git a/src/index.js b/src/index.js index 4ca3bea3f3d386ebabc668349b48890d5bb50e8b..2b1b630c47de62033c9f457e32e095dacbd30968 100644 --- a/src/index.js +++ b/src/index.js @@ -109,7 +109,6 @@ async function start(fields, cozyParameters) { transaction.setStatus(Tracing.SpanStatus.Ok) transaction.finish() } catch (error) { - log('error', error) Sentry.captureException(error) transaction.setStatus(Tracing.SpanStatus.Aborted) transaction.finish() @@ -214,14 +213,17 @@ async function authenticate(login, password, baseUrl, apiAuthKey) { if (resp.data.codeRetour === 100) { return resp.data } else { - const errorMessage = `Authentication failed. Response data: ${JSON.stringify( - resp.data - )}` - Sentry.captureMessage(errorMessage) - throw new Error() + const errorMessage = `Authentication failed. Response data: ${resp.data.libelleRetour}` + log('error', errorMessage) + throw new Error(`code retour ko : ${resp.data.codeRetour}`) } } catch (error) { - Sentry.captureException(error) + log('debug', error.message) + Sentry.captureException(error, { + tags: { + section: 'authenticate', + }, + }) throw new Error(errors.LOGIN_FAILED) } } @@ -252,80 +254,91 @@ async function getData(response, baseUrl, apiAuthKey) { case 100: return format(resp.data) case -2: - Sentry.captureMessage( - `Get data failed. codeRetour - 2. Response data: ${JSON.stringify( - resp.data - )}` + log( + 'error', + `Get data failed. codeRetour -2. ${resp.data.libelleRetour}` ) throw errors.LOGIN_FAILED case -1: - Sentry.captureMessage( - `Get data failed. codeRetour - 1. Response data: ${JSON.stringify( - resp.data - )}` + log( + 'error', + `Get data failed. codeRetour -1. ${resp.data.libelleRetour}` ) throw errors.VENDOR_DOWN default: - Sentry.captureMessage( - `Get data failed. Unknown error occurred. Response data: ${JSON.stringify( - resp.data - )}` + log( + 'error', + `Get data failed. ${resp.data.codeRetour}. ${resp.data.libelleRetour}` ) throw errors.UNKNOWN_ERROR } } catch (error) { + Sentry.captureException(error, { + tags: { + section: 'getData', + }, + }) if (axios.isAxiosError(error)) { throw new Error(errors.VENDOR_DOWN) } - Sentry.captureException(error) throw error } } function format(response) { - log('info', 'origin response size is : ' + response.resultatRetour.length) + log('info', 'origin response size is: ' + response.resultatRetour.length) // Store first value as reference for index processing let refValue = response.resultatRetour[0] + // Create copy of data without first value const data = response.resultatRetour .slice(1) .filter(value => value.ValeurIndex) - log('info', 'filtered size is : ' + data.length) - return data.map(value => { - const time = moment(value.DateReleve, moment.ISO_8601) - const processedLoad = value.ValeurIndex - refValue.ValeurIndex - if (processedLoad < 0) { - log( - 'error', - `processing load for day ${parseInt(time.format('D'))}/${parseInt( - time.format('M') - )}/${parseInt(time.format('YYYY'))}, value is : ${processedLoad}` - ) - throw errors.VENDOR_DOWN - } - // Change index ref value - refValue = value - return { - load: processedLoad, - year: parseInt(time.format('YYYY')), - month: parseInt(time.format('M')), - day: parseInt(time.format('D')), - hour: 0, - minute: 0, - type: value.TypeAgregat, - } - }) + + log('info', 'filtered size is: ' + data.length) + + try { + return data.map(value => { + const time = moment(value.DateReleve, moment.ISO_8601) + const processedLoad = value.ValeurIndex - refValue.ValeurIndex + + if (processedLoad < 0) { + const errorMessage = `Processing load error for day ${parseInt( + time.format('D') + )}/${parseInt(time.format('M'))}/${parseInt( + time.format('YYYY') + )}, value is: ${processedLoad}` + log('debug', errorMessage) + throw errors.VENDOR_DOWN + } + + // Change index ref value + refValue = value + + return { + load: processedLoad, + year: parseInt(time.format('YYYY')), + month: parseInt(time.format('M')), + day: parseInt(time.format('D')), + hour: 0, + minute: 0, + type: value.TypeAgregat, + } + }) + } catch (error) { + Sentry.captureException(error, { + tags: { + section: 'format', + }, + }) + throw error + } } /** * Save data in the right doctype db and prevent duplicated keys */ async function storeData(data, doctype, filterKeys) { - log('debug', 'Store into ' + doctype) - log('debug', 'Store into keys : ' + filterKeys) - // data.map(v => { - // log('info', 'Saving data ' + v.load + ' for ' + v.day + '/' + v.month + '/' + v.year) - // }) const filteredDocuments = await hydrateAndFilter(data, doctype, { keys: filterKeys, })