Exception: Basecamp::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/basecamp/error.rb

Overview

Base error class for all Basecamp SDK errors. Provides structured error handling with codes, hints, and CLI exit codes.

Examples:

Catching errors

begin
  client.projects.list
rescue Basecamp::Error => e
  puts "#{e.code}: #{e.message}"
  puts "Hint: #{e.hint}" if e.hint
  exit e.exit_code
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code:, message:, hint: nil, http_status: nil, retryable: false, retry_after: nil, request_id: nil, cause: nil) ⇒ Error

Returns a new instance of Error.

Parameters:

  • code (String)

    error category code

  • message (String)

    error message

  • hint (String, nil) (defaults to: nil)

    user-friendly hint

  • http_status (Integer, nil) (defaults to: nil)

    HTTP status code

  • retryable (Boolean) (defaults to: false)

    whether operation can be retried

  • retry_after (Integer, nil) (defaults to: nil)

    seconds to wait before retry

  • request_id (String, nil) (defaults to: nil)

    X-Request-Id from response

  • cause (Exception, nil) (defaults to: nil)

    underlying cause



45
46
47
48
49
50
51
52
53
54
# File 'lib/basecamp/error.rb', line 45

def initialize(code:, message:, hint: nil, http_status: nil, retryable: false, retry_after: nil, request_id: nil, cause: nil)
  super(message)
  @code = code
  @hint = hint
  @http_status = http_status
  @retryable = retryable
  @retry_after = retry_after
  @request_id = request_id
  @cause = cause
end

Instance Attribute Details

#causeException? (readonly)

Returns original error that caused this error.

Returns:

  • (Exception, nil)

    original error that caused this error



35
36
37
# File 'lib/basecamp/error.rb', line 35

def cause
  @cause
end

#codeString (readonly)

Returns error category code.

Returns:

  • (String)

    error category code



17
18
19
# File 'lib/basecamp/error.rb', line 17

def code
  @code
end

#hintString? (readonly)

Returns user-friendly hint for resolving the error.

Returns:

  • (String, nil)

    user-friendly hint for resolving the error



20
21
22
# File 'lib/basecamp/error.rb', line 20

def hint
  @hint
end

#http_statusInteger? (readonly)

Returns HTTP status code that caused the error.

Returns:

  • (Integer, nil)

    HTTP status code that caused the error



23
24
25
# File 'lib/basecamp/error.rb', line 23

def http_status
  @http_status
end

#request_idString? (readonly)

Returns X-Request-Id from the response.

Returns:

  • (String, nil)

    X-Request-Id from the response



32
33
34
# File 'lib/basecamp/error.rb', line 32

def request_id
  @request_id
end

#retry_afterInteger? (readonly)

Returns seconds to wait before retrying (for rate limits).

Returns:

  • (Integer, nil)

    seconds to wait before retrying (for rate limits)



29
30
31
# File 'lib/basecamp/error.rb', line 29

def retry_after
  @retry_after
end

#retryableBoolean (readonly)

Returns whether the operation can be retried.

Returns:

  • (Boolean)

    whether the operation can be retried



26
27
28
# File 'lib/basecamp/error.rb', line 26

def retryable
  @retryable
end

Class Method Details

.exit_code_for(code) ⇒ Integer

Maps error codes to exit codes.

Parameters:

  • code (String)

Returns:

  • (Integer)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/basecamp/error.rb', line 71

def self.exit_code_for(code)
  case code
  when ErrorCode::USAGE then ExitCode::USAGE
  when ErrorCode::NOT_FOUND then ExitCode::NOT_FOUND
  when ErrorCode::AUTH then ExitCode::AUTH
  when ErrorCode::FORBIDDEN then ExitCode::FORBIDDEN
  when ErrorCode::RATE_LIMIT then ExitCode::RATE_LIMIT
  when ErrorCode::NETWORK then ExitCode::NETWORK
  when ErrorCode::API then ExitCode::API
  when ErrorCode::AMBIGUOUS then ExitCode::AMBIGUOUS
  when ErrorCode::VALIDATION then ExitCode::VALIDATION
  else ExitCode::API
  end
end

Instance Method Details

#exit_codeInteger

Returns the exit code for CLI applications.

Returns:

  • (Integer)


58
59
60
# File 'lib/basecamp/error.rb', line 58

def exit_code
  self.class.exit_code_for(@code)
end

#retryable?Boolean

Returns whether this error can be retried.

Returns:

  • (Boolean)


64
65
66
# File 'lib/basecamp/error.rb', line 64

def retryable?
  @retryable
end