Sha256: af90a09eed9e1e94a8fea05ac3bd606e630bdb982197d1047da04ea9d10b75b4

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

=begin
{% include "api_info.j2" %}
=end

module {{ module_name }}::{{ version|upper }}
  class APIError < StandardError
    attr_reader :code, :response_headers, :response_body

    # Usage examples:
    #   APIError.new
    #   APIError.new("message")
    #   APIError.new(:code => 500, :response_headers => {}, :response_body => "")
    #   APIError.new(:code => 404, :message => "Not Found")
    def initialize(arg = nil)
      if arg.is_a? Hash
        if arg.key?(:message) || arg.key?('message')
          super(arg[:message] || arg['message'])
        else
          super arg
        end

        arg.each do |k, v|
          instance_variable_set "@#{k}", v
        end
      else
        super arg
      end
    end

    # Override to_s to display a friendly error message
    def to_s
      message
    end

    def message
      if @message.nil?
        msg = "Error message: the server returns an error"
      else
        msg = @message
      end

      msg += "\nHTTP status code: #{code}" if code
      msg += "\nResponse headers: #{response_headers}" if response_headers
      msg += "\nResponse body: #{response_body}" if response_body

      msg
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
datadog_api_client-1.11.0 .generator/src/generator/templates/api_error.j2
datadog_api_client-1.10.0 .generator/src/generator/templates/api_error.j2
datadog_api_client-1.9.0 .generator/src/generator/templates/api_error.j2