diff --git a/src/index.js b/src/index.js index afd337881933f4b8d9e048c549e911db1fa98512..768aa69c9cf357892965343aaf73489503cc42b4 100644 --- a/src/index.js +++ b/src/index.js @@ -109,8 +109,14 @@ async function start(fields, cozyParameters) { transaction.setStatus(Tracing.SpanStatus.Ok) transaction.finish() } catch (error) { - log('error', error) - Sentry.captureException(error) + const errorMessage = `EGL konnector encountered an error. Response data: ${JSON.stringify( + error.message + )}` + Sentry.captureMessage(errorMessage, { + tags: { + section: 'start', + }, + }) transaction.setStatus(Tracing.SpanStatus.Aborted) transaction.finish() await Sentry.flush() @@ -208,16 +214,23 @@ async function authenticate(login, password, baseUrl, apiAuthKey) { pass: password, }, } + try { const resp = await axios(authRequest) if (resp.data.codeRetour === 100) { return resp.data } else { - Sentry.captureException(JSON.stringify(resp.data)) - throw new Error() + const errorMessage = `Authentication failed. Response data: ${resp.data.libelleRetour}` + log('debug', errorMessage) + throw new Error(`code retour ko : ${resp.data.codeRetour}`) } } catch (error) { - Sentry.captureException(JSON.stringify(error)) + log('debug', error.message) + Sentry.captureException(`Authenticate failed: ${error.message}`, { + tags: { + section: 'authenticate', + }, + }) throw new Error(errors.LOGIN_FAILED) } } @@ -250,51 +263,85 @@ async function getData(response, baseUrl, apiAuthKey) { case 100: return format(resp.data) case -2: + log( + 'error', + `Get data failed. codeRetour -2. ${resp.data.libelleRetour}` + ) throw errors.LOGIN_FAILED case -1: + log( + 'error', + `Get data failed. codeRetour -1. ${resp.data.libelleRetour}` + ) throw errors.VENDOR_DOWN default: + log( + 'error', + `Get data failed. ${resp.data.codeRetour}. ${resp.data.libelleRetour}` + ) throw errors.UNKNOWN_ERROR } } catch (error) { - log('debug', 'Error from getAllAgregatsByAbonnement') - throw new Error(errors.VENDOR_DOWN) + Sentry.captureException(`GetData failed: ${error.message}`, { + tags: { + section: 'getData', + }, + }) + if (axios.isAxiosError(error)) { + throw new Error(errors.VENDOR_DOWN) + } + 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(`Format failed: ${error.message}`, { + tags: { + section: 'format', + }, + }) + throw error + } } /** @@ -303,9 +350,6 @@ function format(response) { 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, })