       cursor = @model_type.cursor(JSONUtils.parse(req.query))
       cursor = cursor.whiteList(@white_lists.index) if @white_lists.index
       cursor.toJSON (err, json) =>
-        return res.send(500).send(error: err.toString()) if err
+        return sendError(res, err) if err
         return res.json({result: json}) if cursor.hasCursorQuery('$count') or cursor.hasCursorQuery('$exists')
         unless json
           if cursor.hasCursorQuery('$one')


        if cursor.hasCursorQuery('$page')
          @render req, json.rows, (err, rendered_json) =>
            return res.status(500).send(error: err.toString()) if err
            json.rows = rendered_json
            res.json(json)
        else
          @render req, json, (err, rendered_json) =>
            return res.status(500).send(error: err.toString()) if err
            res.json(rendered_json)

    catch err
      res.status(500).send(error: err.toString())

  show: (req, res) =>
    try
      cursor = @model_type.cursor(req.params.id)
      cursor = cursor.whiteList(@white_lists.show) if @white_lists.show
      cursor.toJSON (err, json) =>
        return res.status(500).send(error: err.toString()) if err
        return res.status(404).send() unless json
        json = _.pick(json, @white_lists.show) if @white_lists.show

        @render req, json, (err, json) =>
          return res.status(500).send(error: err.toString()) if err
          res.json(json)

    catch err
      res.status(500).send(error: err.toString())

  create: (req, res) =>
    try
      json = JSONUtils.parse(if @white_lists.create then _.pick(req.body, @white_lists.create) else req.body)
      model = new @model_type(@model_type::parse(json))
      model.save {}, bbCallback (err) =>
        return res.status(500).send(error: err.toString()) if err

        json = if @white_lists.create then _.pick(model.toJSON(), @white_lists.create) else model.toJSON()
        @render req, json, (err, json) =>
          return res.status(500).send(error: err.toString()) if err
          res.json(json)

    catch err
      res.status(500).send(error: err.toString())

  update: (req, res) =>
    try
      json = JSONUtils.parse(if @white_lists.update then _.pick(req.body, @white_lists.update) else req.body)
      @model_type.find req.params.id, (err, model) =>
        return res.status(500).send(error: err.toString()) if err
        return res.status(404).send() unless model
        model.save model.parse(json), bbCallback (err) =>
          return res.status(500).send(error: err.toString()) if err

          json = if @white_lists.update then _.pick(model.toJSON(), @white_lists.update) else model.toJSON()
          @render req, json, (err, json) =>
            return res.status(500).send(error: err.toString()) if err
            res.json(json)

    catch err
      res.status(500).send(error: err.toString())

  destroy: (req, res) =>
    try
      @model_type.exists req.params.id, (err, exists) =>
        return res.status(500).send(error: err.toString()) if err
        return res.status(404).send() unless exists

        @model_type.destroy {id: req.params.id}, (err) ->
          return res.status(500).send(error: err.toString()) if err
          res.status(200).send()

    catch err
      res.status(500).send(error: err.toString())

  destroyByQuery: (req, res) =>
    try
      @model_type.destroy JSONUtils.parse(req.query), (err) =>
        return res.status(500).send(error: err.toString()) if err
        res.send(200)
    catch err
      res.status(500).send(error: err.toString())

  head: (req, res) =>
    try
      @model_type.exists req.params.id, (err, exists) =>
        return res.status(500).send(error: err.toString()) if err
        res.send(if exists then 200 else 404)
    catch err
      res.status(500).send(error: err.toString())

  headByQuery: (req, res) =>
    try
      @model_type.exists JSONUtils.parse(req.query), (err, exists) =>
        return res.status(500).send(error: err.toString()) if err
        res.send(if exists then 200 else 404)
    catch err
      res.status(500).send(error: err.toString())

  render: (req, json, callback) ->
    template_name = req.query.$render or req.query.$template or @default_template

bbCallback = ORMUtils.bbCallback
JSONUtils = require 'backbone-orm/lib/json_utils'
JoinTableControllerSingleton = require './lib/join_table_controller_singleton'

sendError = (res, err) ->
  res.setHeader('Content-Type', 'text/plain')
  return res.status(500).send(err.toString())

module.exports = class RESTController

