Exception: BrainzLab::RateLimitError

Inherits:
Error
  • Object
show all
Defined in:
lib/brainzlab/errors.rb

Overview

Raised when the rate limit for API requests has been exceeded.

Examples:

Rate limit exceeded

raise BrainzLab::RateLimitError.new(
  "Rate limit exceeded",
  hint: "Wait before retrying or consider upgrading your plan",
  code: "rate_limit_exceeded",
  context: { retry_after: 60, limit: 1000, remaining: 0 }
)

Constant Summary

Constants inherited from Error

Error::DOCS_BASE_URL

Instance Attribute Summary collapse

Attributes inherited from Error

#code, #context, #docs_url, #hint

Instance Method Summary collapse

Methods inherited from Error

#as_json, #detailed_message, #inspect, #to_h, #to_s

Constructor Details

#initialize(message = nil, hint: nil, docs_url: nil, code: nil, context: nil, retry_after: nil, limit: nil, remaining: nil) ⇒ RateLimitError

Returns a new instance of RateLimitError.



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/brainzlab/errors.rb', line 185

def initialize(message = nil, hint: nil, docs_url: nil, code: nil, context: nil, retry_after: nil, limit: nil, remaining: nil)
  @retry_after = retry_after
  @limit = limit
  @remaining = remaining

  hint ||= retry_after ? "Wait #{retry_after} seconds before retrying" : 'Reduce request frequency or upgrade your plan'
  docs_url ||= "#{DOCS_BASE_URL}/sdk/ruby/rate-limits"
  code ||= 'rate_limit_exceeded'

  context ||= {}
  context[:retry_after] = retry_after if retry_after
  context[:limit] = limit if limit
  context[:remaining] = remaining if remaining

  super(message, hint: hint, docs_url: docs_url, code: code, context: context.empty? ? nil : context)
end

Instance Attribute Details

#limitInteger? (readonly)

Returns The rate limit ceiling.

Returns:

  • (Integer, nil)

    The rate limit ceiling



180
181
182
# File 'lib/brainzlab/errors.rb', line 180

def limit
  @limit
end

#remainingInteger? (readonly)

Returns Remaining requests in the current window.

Returns:

  • (Integer, nil)

    Remaining requests in the current window



183
184
185
# File 'lib/brainzlab/errors.rb', line 183

def remaining
  @remaining
end

#retry_afterInteger? (readonly)

Returns Seconds to wait before retrying.

Returns:

  • (Integer, nil)

    Seconds to wait before retrying



177
178
179
# File 'lib/brainzlab/errors.rb', line 177

def retry_after
  @retry_after
end