require_relative 'devops_error'
module Devops
  module Exception
    class ValidationError < DevopsError

      attr_accessor :body

      def self.create_from_db_exception e
        msgs = e.document.errors.messages
        create_from_messages(msgs)
      end

      def self.create_from_messages msgs
        msgs.keys.each{|k| msgs[k] = msgs[k][0]}
        Devops::Exception::ValidationError.new(nil, msgs)
      end

      def initialize msg, body={}
        super(msg || "Validation error")
        self.body = body
      end

      def http_status
        422
      end

      def http_body
        JSON.pretty_generate(self.body.merge(message: self.message))
      end
    end

  end
end
