Exception: Buildkit::Error

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

Overview

Custom error class for rescuing from all Buildkite errors

Direct Known Subclasses

ClientError, ServerError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ Error

Returns a new instance of Error.



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

def initialize(response = nil)
  @response = response
  super(build_error_message)
end

Class Method Details

.from_response(response) ⇒ Buildkit::Error

Returns the appropriate Buildkit::Error subclass based on status and response message

Parameters:

  • response (Hash)

    HTTP response

Returns:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/buildkit/error.rb', line 11

def self.from_response(response)
  status = response[:status].to_i
  if klass =  case status
              when 400      then Buildkit::BadRequest
              when 401      then Buildkit::Unauthorized
              when 403      then Buildkit::Forbidden
              when 404      then Buildkit::NotFound
              when 405      then Buildkit::MethodNotAllowed
              when 406      then Buildkit::NotAcceptable
              when 409      then Buildkit::Conflict
              when 415      then Buildkit::UnsupportedMediaType
              when 422      then Buildkit::UnprocessableEntity
              when 400..499 then Buildkit::ClientError
              when 500      then Buildkit::InternalServerError
              when 501      then Buildkit::NotImplemented
              when 502      then Buildkit::BadGateway
              when 503      then Buildkit::ServiceUnavailable
              when 500..599 then Buildkit::ServerError
              end
    klass.new(response)
  end
end

Instance Method Details

#documentation_urlString

Documentation URL returned by the API for some errors

Returns:

  • (String)


42
43
44
# File 'lib/buildkit/error.rb', line 42

def documentation_url
  data[:documentation_url] if data.is_a? Hash
end

#errorsArray<Hash>

Array of validation errors

Returns:

  • (Array<Hash>)

    Error info



48
49
50
51
52
53
54
# File 'lib/buildkit/error.rb', line 48

def errors
  if data&.is_a?(Hash)
    data[:errors] || []
  else
    []
  end
end