parse-xml.js 843 Bytes
const MODULE_NAME = 'PARSE-XML';

const isInTest = typeof global.it === 'function';
const xmlParser = require('fast-xml-parser');

// eslint-disable-next-line no-console
const warn = isInTest ? console.log : require('tektrans-logger').warn;

const parseOptions = {
    parseNodeValue: false,
    trimValue: true,
};

module.exports = (xid, str) => {
    let result;
    try {
        result = xmlParser.parse(str, parseOptions);
        if (!result) {
            const e = new Error(`${MODULE_NAME} 64EE88A6: XML parser return invalid value`);
            warn(e.message, { xid });

            throw e;
        }
    } catch (e) {
        warn(`${MODULE_NAME} F9B6010C: Exception on parsing xml`, {
            xid,
            eCode: e.code,
            eMessage: e.message,
        });

        result = null;
    }

    return result;
};