All Downloads are FREE. Search and download functionalities are using the official Maven repository.

crystal.api_error.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
# {{#lambdaPrefixWithHash}}{{> api_info}}{{/lambdaPrefixWithHash}}

module {{moduleName}}
  class ApiError < Exception
    getter code : Int32?
    getter response_headers : Hash(String, Array(String) | String)?

    # Usage examples:
    #   ApiError.new
    #   ApiError.new(message: "message")
    #   ApiError.new(code: 500, response_headers: {}, message: "")
    #   ApiError.new(code: 404, message: "Not Found")
    def initialize(@code , @message, @response_headers)
    end

    def initialize(@code , @message)
    end

    # Override to_s to display a friendly error message
    def to_s
      msg = ""
      msg = msg + "\nHTTP status code: #{code}" if @code
      msg = msg + "\nResponse headers: #{response_headers}" if @response_headers
      if @message.try &.empty?
        msg = msg + "\nError message: the server returns an error but the HTTP response body is empty."
      else
        msg = msg + "\nResponse body: #{@message}"
      end

      msg
    end
  end
end




© 2015 - 2024 Weber Informatics LLC | Privacy Policy