Skip to content
Snippets Groups Projects
Commit a3ec6009 authored by Bastien DUMONT's avatar Bastien DUMONT :angel:
Browse files

Merge branch 'fix/unexpected-tag' into 'dev'

fix: throw vendor down when request is rejected

See merge request !54
parents a492b89c 206774cf
No related branches found
No related tags found
2 merge requests!56v1.4.0,!54fix: throw vendor down when request is rejected
Pipeline #93841 passed
......@@ -5,6 +5,7 @@ const { parseTags, parseValue, parseServiceId } = require('../helpers/parsing')
const { commanderCollectePublicationMesures } = require('../requests/sge')
const xml2js = require('xml2js')
const Sentry = require('@sentry/node')
const { catchRequestReject } = require('../helpers/catch')
/**
* @param {string} url
......@@ -59,6 +60,8 @@ async function activateContract(
throw new Error(errors.CAPTCHA_RESOLUTION_FAILED)
})
catchRequestReject(response.body)
const parsedReply = await xml2js.parseStringPromise(response.body, {
tagNameProcessors: [parseTags],
valueProcessors: [parseValue],
......
......@@ -5,6 +5,7 @@ const { parseTags, parseValue } = require('../helpers/parsing')
const { commanderArretServiceSouscritMesures } = require('../requests/sge')
const xml2js = require('xml2js')
const Sentry = require('@sentry/node')
const { catchRequestReject } = require('../helpers/catch')
/**
* @param {string} url
......@@ -53,6 +54,8 @@ async function terminateContract(
throw new Error(errors.VENDOR_DOWN)
})
catchRequestReject(response.body)
const parsedReply = await xml2js.parseStringPromise(response.body, {
tagNameProcessors: [parseTags],
valueProcessors: [parseValue],
......
......@@ -11,6 +11,7 @@ const { rechercherServicesSouscritsMesures } = require('../requests/sge')
const xml2js = require('xml2js')
const { contractState } = require('./types/enum')
const Sentry = require('@sentry/node')
const { catchRequestReject } = require('../helpers/catch')
/**
* @param {string} url
......@@ -45,6 +46,12 @@ async function verifyContract(url, apiAuthKey, appLogin, contractId, pointId) {
throw new Error(errors.CAPTCHA_RESOLUTION_FAILED)
})
try {
catchRequestReject(response.body)
} catch (error) {
throw new Error(errors.CAPTCHA_RESOLUTION_FAILED)
}
const parsedReply = await xml2js.parseStringPromise(response.body, {
tagNameProcessors: [parseTags],
valueProcessors: [parseValue],
......@@ -79,7 +86,7 @@ async function verifyContract(url, apiAuthKey, appLogin, contractId, pointId) {
section: 'verifyContract',
},
})
if (parsedReply.Envelope.Body.Fault) {
if (parsedReply?.Envelope?.Body?.Fault) {
log(
'error',
`Enedis issue ${parsedReply.Envelope.Body.Fault.detail.erreur.resultat.$.code}: ${parsedReply.Envelope.Body.Fault.faultstring}`
......
......@@ -9,6 +9,7 @@ const {
const xml2js = require('xml2js')
const { consulterDonneesTechniquesContractuelles } = require('../requests/sge')
const Sentry = require('@sentry/node')
const { catchRequestReject } = require('../helpers/catch')
/**
* Get user contract start date
......@@ -43,6 +44,8 @@ async function findUserAddress(url, apiAuthKey, userLogin, pointId) {
throw new Error(errors.VENDOR_DOWN)
})
catchRequestReject(response.body)
const result = await xml2js.parseStringPromise(response.body, {
tagNameProcessors: [parseTags],
valueProcessors: [parseValue],
......
......@@ -5,6 +5,7 @@ const { parseUserPdl, parseTags, parseValue } = require('../helpers/parsing')
const { rechercherPoint } = require('../requests/sge')
const xml2js = require('xml2js')
const Sentry = require('@sentry/node')
const { catchRequestReject } = require('../helpers/catch')
/**
* @param {string} url
......@@ -60,12 +61,17 @@ async function findUserPdl(
throw new Error(errors.VENDOR_DOWN)
})
const parsedReply = await xml2js.parseStringPromise(response.body, {
tagNameProcessors: [parseTags],
valueProcessors: [parseValue],
explicitArray: false,
})
catchRequestReject(response.body)
const parsedReply = await xml2js
.parseStringPromise(response.body, {
tagNameProcessors: [parseTags],
valueProcessors: [parseValue],
explicitArray: false,
})
.catch(error => {
log('error', 'Error while parsing XML: ' + error)
})
try {
return parseUserPdl(parsedReply)
} catch (error) {
......
const { log } = require('cozy-konnector-libs')
/**
* Throw an error if the response contains a "Request Rejected"
* Enedis might send a 429 status but the F5 always transform it to a 200
* @param {string} response
* @example <html><head><title>Request Rejected</title></head>
* <body>The requested URL was rejected. Please consult with your administrator</body></html>
*/
function catchRequestReject(response) {
if (response.includes('Request Rejected')) {
const supportID = response.replace(/\D/g, '')
log('debug', response.slice(0, 100))
log('error', `Support ID : ${supportID}`)
log('error', 'Request Rejected')
throw new Error('Request Rejected')
}
}
module.exports = { catchRequestReject }
......@@ -43,6 +43,7 @@ const Sentry = require('@sentry/node')
// eslint-disable-next-line
const Tracing = require('@sentry/tracing') // Needed for tracking performance in Sentry
const { version } = require('../package.json')
const { catchRequestReject } = require('./helpers/catch')
moment.locale('fr') // set the language
moment.tz.setDefault('Europe/Paris') // set the timezone
......@@ -398,6 +399,8 @@ async function getOffPeakHours(url, apiAuthKey, userLogin, pointId) {
return err
})
catchRequestReject(response.body)
const result = await xml2js.parseStringPromise(response.body, {
tagNameProcessors: [parseTags],
valueProcessors: [parseValue],
......@@ -426,14 +429,14 @@ async function getOffPeakHours(url, apiAuthKey, userLogin, pointId) {
}
/**
* Get hour data
* Get daily data
* @param {string} url
* @param {string} apiAuthKey
* @param {string} userLogin
* @param {string} pointId
*/
async function getData(url, apiAuthKey, userLogin, pointId) {
log('info', 'Fetching data')
log('info', 'Fetching daily data')
const sgeHeaders = {
'Content-Type': 'text/xml;charset=UTF-8',
apikey: apiAuthKey,
......@@ -459,6 +462,8 @@ async function getData(url, apiAuthKey, userLogin, pointId) {
return err
})
catchRequestReject(response.body)
xml2js.parseString(
response.body,
{
......@@ -466,7 +471,7 @@ async function getData(url, apiAuthKey, userLogin, pointId) {
valueProcessors: [parseValue],
explicitArray: false,
},
processData()
processData('com.grandlyon.enedis.day')
)
}
......@@ -502,6 +507,8 @@ async function getMaxPowerData(url, apiAuthKey, userLogin, pointId) {
return err
})
catchRequestReject(response.body)
xml2js.parseString(
response.body,
{
......@@ -521,7 +528,7 @@ async function getMaxPowerData(url, apiAuthKey, userLogin, pointId) {
* @param {string} pointId
*/
async function getDataHalfHour(url, apiAuthKey, userLogin, pointId) {
log('info', 'Fetching data')
log('info', 'Fetching half-hour data')
const sgeHeaders = {
'Content-Type': 'text/xml;charset=UTF-8',
apikey: apiAuthKey,
......@@ -562,6 +569,8 @@ async function getDataHalfHour(url, apiAuthKey, userLogin, pointId) {
return err
})
catchRequestReject(response.body)
xml2js.parseString(
response.body,
{
......@@ -579,7 +588,7 @@ async function getDataHalfHour(url, apiAuthKey, userLogin, pointId) {
* @param {string} doctype
* @returns
*/
function processData(doctype = 'com.grandlyon.enedis.day') {
function processData(doctype) {
return async (err, result) => {
if (err) {
log('error', err)
......@@ -587,7 +596,7 @@ function processData(doctype = 'com.grandlyon.enedis.day') {
throw err
}
// Return only needed part of info
log('info', doctype)
log('info', `Processing ${doctype} data`)
try {
const data = parseSgeXmlData(result)
const processedDailyData = await storeData(
......@@ -595,8 +604,6 @@ function processData(doctype = 'com.grandlyon.enedis.day') {
doctype,
['year', 'month', 'day', 'hour', 'minute']
)
log('info', 'Aggregate enedis daily data for month and year')
if (doctype === 'com.grandlyon.enedis.day') {
log('info', 'Aggregating...')
await aggregateMonthAndYearData(processedDailyData)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment