const parsers = require('../parsers')
const transform = require('../transform')
const request = require('../request')
const { ConnectionFailedError, EmptyParseOutputError } = require('../errors')

async function parse(parser, text) {
  const parsed = await parsers[parser](text)
  if (!parsed) throw new EmptyParseOutputError()
  return transform(parsed)
}

module.exports = async function parseFromQuery({ url, parser }) {
  let content
  try {
    content = (await request(url)).text
  } catch (error) {
    if (error.code === 'ENOTFOUND') {
      throw new ConnectionFailedError(url)
    }
    throw error
  }

  if (parser) {
    return parse(parser, content)
  } else {
    for (let i = 0; i < parsers.keys.length; i++) {
      try {
        return parse(parsers.keys[i], content)
      } catch (error) {
        if (i !== parsers.keys.length - 1) {
          throw error
        }
        continue
      }
    }
  }
}

@@ -739,32 +739,38 @@
 %0A        return 
+await 
 parse(parsers.ke
@@ -827,11 +827,9 @@
  (i 
-!==
+%3C
  par
@@ -862,27 +862,24 @@
         
-throw error
+continue
 %0A       
@@ -885,32 +885,35 @@
   %7D%0A        
-continue
+throw error
 %0A      %7D%0A   

Make error handling use full parser list