github.com/phrase/openapi@v0.0.0-20240514140800-49e8a106740e/openapi-generator/templates/ruby-client/api_error.mustache (about)

     1  module {{moduleName}}
     2    class ApiError < StandardError
     3      attr_reader :code, :response_headers, :response_body
     4  
     5      # Usage examples:
     6      #   ApiError.new
     7      #   ApiError.new("message")
     8      #   ApiError.new(:code => 500, :response_headers => {}, :response_body => "")
     9      #   ApiError.new(:code => 404, :message => "Not Found")
    10      def initialize(arg = nil)
    11        if arg.is_a? Hash
    12          if arg.key?(:message) || arg.key?('message')
    13            super(arg[:message] || arg['message'])
    14          else
    15            super arg
    16          end
    17  
    18          arg.each do |k, v|
    19            instance_variable_set "@#{k}", v
    20          end
    21        else
    22          super arg
    23        end
    24      end
    25  
    26      # Override to_s to display a friendly error message
    27      def to_s
    28        message
    29      end
    30  
    31      def message
    32        if @message.nil?
    33          msg = "Error message: the server returns an error"
    34        else
    35          msg = @message
    36        end
    37  
    38        msg += "\nHTTP status code: #{code}" if code
    39        msg += "\nResponse headers: #{response_headers}" if response_headers
    40        msg += "\nResponse body: #{response_body}" if response_body
    41  
    42        msg
    43      end
    44    end
    45  end