<commit_before>var request = require( 'request' )
var debug = require( 'debug' )( 'flightstats' )

module.exports = function( options, callback ) {
  
  options.method = ( options.method || 'GET' ).toUpperCase()
  
  debug( 'request:method', options.method || 'GET' )
  debug( 'request:url', options.url )
  debug( 'request:query', options.qs )
  debug( 'request', options.headers )
  
  var req = request({
    method: options.method || 'GET',
    headers: Object.assign( {}, options.headers || {}, this.headers ),
    url: options.url,
    qs: options.qs,
  }, function( error, response, body ) {
    
    if( error && !response ) {
      error = error || new Error( 'Unknown Error' )
      debug( 'response:error', error )
      return callback( error )
    }
    
    if( error || ( response && response.statusCode !== 200 ) ) {
      
      // NOTE: Only god knows why they return JSON
      // to browsers, but not API clients – parsing strings FTW!
      var data = null
      try {
        data = response.body.split( /\r?\n/g )
          .reduce( function( err, line ) {
            var parts = line.split( ': ' )
            err[ parts.shift() ] = parts.join()
            return err
          }, {})
      } catch( e ) {}
      
      if( data ) {
        error = error || new Error( data.message )
        error.id = data.id
        error.code = data.code
        error.message = data.message
      }
      
      error = error || new Error( response.statusMessage )
      error.statusCode = response.statusCode
      error.statusMessage = response.statusMessage
      error.url = response.url
      error.headers = response.headers
      
      debug( 'response:error', error )
      
      return callback( error )
      
    }
    
    debug( 'response', response.statusCode, response.statusMessage )
    debug( 'response', response.headers )
    
    var data = null
    
    try {
      data = JSON.parse( body )
    } catch( error ) {
      debug( 'response:error:body', error )
      error.statusCode = 500
      return callback( error )
    }
    
    callback( null, data )
    
  })
  <commit_msg>Update lib/request: Enhance error message with URL<commit_after>var URL = require( 'url' )
var request = require( 'request' )
var debug = require( 'debug' )( 'flightstats' )

module.exports = function( options, callback ) {
  
  options.method = ( options.method || 'GET' ).toUpperCase()
  
  debug( 'request:method', options.method || 'GET' )
  debug( 'request:url', options.url )
  debug( 'request:query', options.qs )
  debug( 'request', options.headers )
  
  var req = request({
    method: options.method || 'GET',
    headers: Object.assign( {}, options.headers || {}, this.headers ),
    url: options.url,
    qs: options.qs,
  }, function( error, response, body ) {
    
    if( error && !response ) {
      error = error || new Error( 'Unknown Error' )
      debug( 'response:error', error )
      return callback( error )
    }
    
    if( error || ( response && response.statusCode !== 200 ) ) {
      
      // NOTE: Only god knows why they return JSON
      // to browsers, but not API clients – parsing strings FTW!
      var data = null
      try {
        data = response.body.split( /\r?\n/g )
          .reduce( function( err, line ) {
            var parts = line.split( ': ' )
            err[ parts.shift() ] = parts.join()
            return err
          }, {})
      } catch( e ) {}
      
      if( data ) {
        error = error || new Error( data.message )
        error.id = data.id
        error.code = data.code
        error.message = data.message
      }
      
      error = error || new Error( response.statusMessage )
      error.statusCode = response.statusCode
      error.statusMessage = response.statusMessage
      error.url = URL.format( req.url )
      error.headers = response.headers
      
      error.message += '\n' + error.url
      
      debug( 'response:error', error )
      
      return callback( error )
      
    }
    
    debug( 'response', response.statusCode, response.statusMessage )
    debug( 'response', response.headers )
    
    var data = null
    
    try {
      data = JSON.parse( body )
    } catch( error ) {
      debug( 'response:error:body', error )
      error.statusCode = 500
      return callback( error )
    }
    
    callback( null, data )
    
  })
  
{'fraction_non_alphanumeric': 0.13171179440110142, 'fraction_numerical': 0.0027535566773749425, 'mean_word_length': 2.0439944134078214, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3547128', 'n_tokens_mistral': 1292, 'n_tokens_neox': 1268, 'n_words': 346}