'use strict'

const {Parser: XMLParser} = require('xml2js')
const camelCase = require('camelcase')

// -------------------------------------------------------------
// Constants.
// -------------------------------------------------------------

const OPTIONS = {
  explicitArray: false,
  async: false,
  tagNameProcessors: [camelCase]
}

// -------------------------------------------------------------
// Module.
// -------------------------------------------------------------

function parseXML (xml) {
  if (!xml) {
    throw new Error('XML input was null or empty')
  }

  let data

  new XMLParser(OPTIONS).parseString(xml, function (err, result) {
    // FIXME: is there a way to not have an error BUT no result aswell?
    // Check the behavior of xml2js.
    if (err || !result) {
      throw err
    }

    // Values are nested in the "product", "game" or "company" root node.
    // We need to detect and ignore this root element.
    if (result.game) {
      data = result.game
      data.type = 'product'
    } else if (result.product) {
      data = result.product
      data.type = 'product'
    } else if (result.company) {
      data = result.company
      data.type = 'company'
    } else {
      throw new Error('Unrecognized XML file, expected <game> or <company> root tag')
    }
  })

  return data
}

// -------------------------------------------------------------
// Exports.
// -------------------------------------------------------------

module.exports = {
  parseXML
}

@@ -657,154 +657,79 @@
     
-// FIXME: is there a way to not have an error BUT no result aswell?%0A    // Check the behavior of xml2js.%0A    if (err %7C%7C !result) %7B%0A      throw err
+if (err %7C%7C !result) %7B%0A      throw new Error('Failed to parse XML file')
 %0A   

Remove fixme in parser