'use strict'

var createCustomError = require('custom-error-generator')

var QUERY_LINKS = {
  groupBy:         'http://druid.io/docs/0.6.121/GroupByQuery.html',
  search:          'http://druid.io/docs/0.6.121/SearchQuery.html',
  segmentMetadata: 'http://druid.io/docs/0.6.121/SegmentMetadataQuery.html',
  timeBoundary:    'http://druid.io/docs/0.6.121/TimeBoundaryQuery.html',
  timeseries:      'http://druid.io/docs/0.6.121/TimeseriesQuery.html',
  topN:            'http://druid.io/docs/0.6.121/TopNQuery.html'
}




/**
 * Used when query misses some mandatory fields
 *
 * @class MissingFieldError
 * @extends Error
 */
exports.MissingFieldError = createCustomError('MissingFieldError', null, function(field, queryType) {
  /**
   * Human-readable error message
   *
   * @type {string}
   */
  this.message = 'Query field ' + field + ' is required for ' + queryType + ' query.'


  /**
   * Link to Druid documentation describing query type
   *
   * @type {string}
   */
  this.moreInfo = QUERY_LINKS[queryType]


  /**
   * No comments, dudes.
   *
   * @type {string}
   */
  this.missingField = field


  /**
   * No comments, really!
   *
   * @type {string}
   */
  this.queryType = queryType
})




/**
 * Any field error
 *
 * @class FieldError
 * @extends Error
 */
exports.FieldError = createCustomError('FieldError')




/**
 * Thrown when field value has incorrect type
 *
 * @class FieldTypeError
 * @extends Error
 */
exports.FieldTypeError = createCustomError('FieldTypeError', null, function(field, type) {
  /**
   * Good error message
   *
   * @type {string}
   */
  this.message = 'Field ' + field + ' must be ' + type
})
@@ -1522,26 +1522,52 @@
 ld, type
-) %7B%0A
+, value) %7B%0A  if (field) %7B%0A  
   /**%0A
+  
    * Goo
@@ -1577,34 +1577,38 @@
 rror message%0A   
-*%0A
+  *%0A  
    * @type %7Bstri
@@ -1606,35 +1606,39 @@
 ype %7Bstring%7D%0A   
+  
 */%0A
+  
   this.message =
@@ -1672,14 +1672,83 @@
 be ' + type%0A
+  %7D%0A  else %7B%0A    this.message = 'Invalid ' + type + ': ' + value%0A  %7D%0A
 %7D)

generate another error message if field name is missing