require_relative "devops_error"
class InvalidRecord < ::Devops::Exception::DevopsError

  # message could be a String or a hash like
  # {
  #   error_data: [{:key=>:provider, :message=>"Value can not be undefined"}]
  # }
  def initialize msg
    if msg.is_a?(String)
      super(msg)
    else
      @object = msg
    end
  end

  def message
    if @object
      messages = @object[:error_data].map do |error_item|
        "#{error_item[:key]}: #{error_item[:message]}"
      end
      "Following errors occured: \n#{messages.join('\n')}"
    else
      super
    end
  end

  def http_status
    400
  end

  def http_body
    if @object.nil?
      super
    else
      JSON.pretty_generate(@object)
    end
  end
end
