class Canistor::ErrorHandler

Attributes

context[R]
host_id[R]
request_id[R]

Public Class Methods

access_denied(context, subject) click to toggle source
# File lib/canistor/error_handler.rb, line 213
def self.access_denied(context, subject)
  new(context).access_denied(subject)
end
new(context) click to toggle source
# File lib/canistor/error_handler.rb, line 9
def initialize(context)
  @context = context
  @request_id = SecureRandom.hex(8).upcase
  @host_id = Base64.strict_encode64(SecureRandom.hex(16))
end
serve_bad_request(context) click to toggle source
# File lib/canistor/error_handler.rb, line 181
def self.serve_bad_request(context)
  new(context).serve_bad_request
end
serve_continue(context) click to toggle source
# File lib/canistor/error_handler.rb, line 177
def self.serve_continue(context)
  new(context).serve_continue
end
serve_internal_error(context) click to toggle source
# File lib/canistor/error_handler.rb, line 217
def self.serve_internal_error(context)
  new(context).serve_internal_error
end
serve_invalid_access_key(context, authorization) click to toggle source
# File lib/canistor/error_handler.rb, line 185
def self.serve_invalid_access_key(context, authorization)
  new(context).serve_invalid_access_key(authorization)
end
serve_invalid_part(context, upload_id, part_number, part_etag) click to toggle source
# File lib/canistor/error_handler.rb, line 205
def self.serve_invalid_part(context, upload_id, part_number, part_etag)
  new(context).serve_invalid_part(upload_id, part_number, part_etag)
end
serve_invalid_part_order(context, upload_id) click to toggle source
# File lib/canistor/error_handler.rb, line 209
def self.serve_invalid_part_order(context, upload_id)
  new(context).serve_invalid_part_order(upload_id)
end
serve_no_such_bucket(context, subject) click to toggle source
# File lib/canistor/error_handler.rb, line 193
def self.serve_no_such_bucket(context, subject)
  new(context).serve_no_such_bucket(subject)
end
serve_no_such_key(context, subject) click to toggle source
# File lib/canistor/error_handler.rb, line 201
def self.serve_no_such_key(context, subject)
  new(context).serve_no_such_key(subject)
end
serve_no_such_upload(context, subject) click to toggle source
# File lib/canistor/error_handler.rb, line 197
def self.serve_no_such_upload(context, subject)
  new(context).serve_no_such_upload(subject)
end
serve_signature_does_not_match(context, authorization) click to toggle source
# File lib/canistor/error_handler.rb, line 189
def self.serve_signature_does_not_match(context, authorization)
  new(context).serve_signature_does_not_match(authorization)
end
trigger_fatal_error(context) click to toggle source
# File lib/canistor/error_handler.rb, line 225
def self.trigger_fatal_error(context)
  new(context).trigger_fatal_error
end
trigger_reset_connection(context) click to toggle source
# File lib/canistor/error_handler.rb, line 221
def self.trigger_reset_connection(context)
  new(context).trigger_reset_connection
end

Public Instance Methods

request() click to toggle source
# File lib/canistor/error_handler.rb, line 15
def request
  context.http_request
end
response() click to toggle source
# File lib/canistor/error_handler.rb, line 19
def response
  context.http_response
end
serve_access_denied(subject) click to toggle source
# File lib/canistor/error_handler.rb, line 134
def serve_access_denied(subject)
  serve_error(403, Nokogiri::XML::Builder.new do |xml|
    xml.Error do
      xml.Code 'AccessDenied'
      xml.Message 'Access Denied'
      xml.RequestId request_id
      xml.HostId host_id
    end
  end.to_xml)
end
serve_bad_request() click to toggle source
# File lib/canistor/error_handler.rb, line 31
def serve_bad_request
  serve_error(400, Nokogiri::XML::Builder.new do |xml|
    xml.Error do
      xml.Code 'InvalidArgument'
      xml.Message 'Invalid Argument.'
      xml.RequestId request_id
      xml.HostId host_id
    end
  end.to_xml)
end
serve_continue() click to toggle source
# File lib/canistor/error_handler.rb, line 23
def serve_continue
  response.signal_headers(
    100,
    'date' => Time.now.httpdate,
    'x-amz-request-id' => request_id
  )
end
serve_error(status_code, body) click to toggle source
# File lib/canistor/error_handler.rb, line 156
def serve_error(status_code, body)
  response.signal_headers(
    status_code,
    'date' => Time.now.httpdate,
    'x-amz-request-id' => request_id
  )
  unless request.http_method == 'HEAD'
    response.signal_data(body)
  end
end
serve_internal_error() click to toggle source
# File lib/canistor/error_handler.rb, line 145
def serve_internal_error
  serve_error(500, Nokogiri::XML::Builder.new do |xml|
    xml.Error do
      xml.Code 'InternalError'
      xml.Message 'We encountered an internal error. Please try again.'
      xml.RequestId request_id
      xml.HostId host_id
    end
  end.to_xml)
end
serve_invalid_access_key(authorization) click to toggle source
# File lib/canistor/error_handler.rb, line 42
def serve_invalid_access_key(authorization)
  serve_error(403, Nokogiri::XML::Builder.new do |xml|
    xml.Error do
      xml.Code 'InvalidAccessKeyId'
      xml.Message 'The AWS Access Key Id you provided does not exist in our records.'
      xml.AWSAccessKeyId authorization.access_key_id
      xml.RequestId request_id
      xml.HostId host_id
    end
  end.to_xml)
end
serve_invalid_part(upload_id, part_number, part_etag) click to toggle source
# File lib/canistor/error_handler.rb, line 105
def serve_invalid_part(upload_id, part_number, part_etag)
  serve_error(400, Nokogiri::XML::Builder.new do |xml|
    xml.Error do
      xml.Code 'InvalidPart'
      xml.Message 'One or more of the specified parts could not be found. '\
                  'The part may not have been uploaded, or the specified ' \
                  ' entity tag may not match the part\'s entity tag.'
      xml.UploadId upload_id
      xml.PartNumber part_number
      xml.ETag part_etag
      xml.RequestId request_id
      xml.HostId host_id
    end
  end.to_xml)
end
serve_invalid_part_order(upload_id) click to toggle source
# File lib/canistor/error_handler.rb, line 121
def serve_invalid_part_order(upload_id)
  serve_error(400, Nokogiri::XML::Builder.new do |xml|
    xml.Error do
      xml.Code 'InvalidPartOrder'
      xml.Message 'The list of parts was not in ascending order. Parts ' \
                  'must be ordered by part number.'
      xml.UploadId upload_id
      xml.RequestId request_id
      xml.HostId host_id
    end
  end.to_xml)
end
serve_no_such_bucket(subject) click to toggle source
# File lib/canistor/error_handler.rb, line 67
def serve_no_such_bucket(subject)
  serve_error(404, Nokogiri::XML::Builder.new do |xml|
    xml.Error do
      xml.Code 'NoSuchBucket'
      xml.Message 'The specified bucket does not exist'
      xml.BucketName subject.bucket
      xml.RequestId request_id
      xml.HostId host_id
    end
  end.to_xml)
end
serve_no_such_key(subject) click to toggle source
# File lib/canistor/error_handler.rb, line 93
def serve_no_such_key(subject)
  serve_error(404, Nokogiri::XML::Builder.new do |xml|
    xml.Error do
      xml.Code 'NoSuchKey'
      xml.Message 'The specified key does not exist.'
      xml.Key subject.key
      xml.RequestId request_id
      xml.HostId host_id
    end
  end.to_xml)
end
serve_no_such_upload(subject) click to toggle source
# File lib/canistor/error_handler.rb, line 79
def serve_no_such_upload(subject)
  serve_error(404, Nokogiri::XML::Builder.new do |xml|
    xml.Error do
      xml.Code 'NoSuchUpload'
      xml.Message 'The specified upload does not exist. The upload ID ' \
                  'may be invalid, or the upload may have been aborted ' \
                  'or completed.'
      xml.Key subject.key
      xml.RequestId request_id
      xml.HostId host_id
    end
  end.to_xml)
end
serve_signature_does_not_match(authorization) click to toggle source
# File lib/canistor/error_handler.rb, line 54
def serve_signature_does_not_match(authorization)
  serve_error(403, Nokogiri::XML::Builder.new do |xml|
    xml.Error do
      xml.Code 'SignatureDoesNotMatch'
      xml.Message 'The request signature we calculated does not match the signature you provided. Check your key and signing method.'
      xml.AWSAccessKeyId authorization.access_key_id
      xml.SignatureProvided authorization.signature
      xml.RequestId request_id
      xml.HostId host_id
    end
  end.to_xml)
end
trigger_fatal_error() click to toggle source
# File lib/canistor/error_handler.rb, line 173
def trigger_fatal_error
  response.signal_error(RuntimeError.new("Fatal error."))
end
trigger_reset_connection() click to toggle source
# File lib/canistor/error_handler.rb, line 167
def trigger_reset_connection
  response.signal_error(Seahorse::Client::NetworkingError.new(
    Errno::ECONNRESET.new, 'Remote host reset the connection request.'
  ))
end