Class: Canistor::ErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/canistor/error_handler.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ ErrorHandler

Returns a new instance of ErrorHandler.



9
10
11
12
13
# 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

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



5
6
7
# File 'lib/canistor/error_handler.rb', line 5

def context
  @context
end

#host_idObject (readonly)

Returns the value of attribute host_id.



7
8
9
# File 'lib/canistor/error_handler.rb', line 7

def host_id
  @host_id
end

#request_idObject (readonly)

Returns the value of attribute request_id.



6
7
8
# File 'lib/canistor/error_handler.rb', line 6

def request_id
  @request_id
end

Class Method Details

.access_denied(context, subject) ⇒ Object



131
132
133
# File 'lib/canistor/error_handler.rb', line 131

def self.access_denied(context, subject)
  new(context).access_denied(subject)
end

.serve_internal_error(context) ⇒ Object



135
136
137
# File 'lib/canistor/error_handler.rb', line 135

def self.serve_internal_error(context)
  new(context).serve_internal_error
end

.serve_invalid_access_key(context, authorization) ⇒ Object



115
116
117
# File 'lib/canistor/error_handler.rb', line 115

def self.serve_invalid_access_key(context, authorization)
  new(context).serve_invalid_access_key(authorization)
end

.serve_no_such_bucket(context, subject) ⇒ Object



123
124
125
# File 'lib/canistor/error_handler.rb', line 123

def self.serve_no_such_bucket(context, subject)
  new(context).serve_no_such_bucket(subject)
end

.serve_no_such_key(context, subject) ⇒ Object



127
128
129
# File 'lib/canistor/error_handler.rb', line 127

def self.serve_no_such_key(context, subject)
  new(context).serve_no_such_key(subject)
end

.serve_signature_does_not_match(context, authorization) ⇒ Object



119
120
121
# File 'lib/canistor/error_handler.rb', line 119

def self.serve_signature_does_not_match(context, authorization)
  new(context).serve_signature_does_not_match(authorization)
end

.trigger_fatal_error(context) ⇒ Object



143
144
145
# File 'lib/canistor/error_handler.rb', line 143

def self.trigger_fatal_error(context)
  new(context).trigger_fatal_error
end

.trigger_reset_connection(context) ⇒ Object



139
140
141
# File 'lib/canistor/error_handler.rb', line 139

def self.trigger_reset_connection(context)
  new(context).trigger_reset_connection
end

Instance Method Details

#requestObject



15
16
17
# File 'lib/canistor/error_handler.rb', line 15

def request
  context.http_request
end

#responseObject



19
20
21
# File 'lib/canistor/error_handler.rb', line 19

def response
  context.http_response
end

#serve_access_denied(subject) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/canistor/error_handler.rb', line 72

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_error(status_code, body) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/canistor/error_handler.rb', line 94

def serve_error(status_code, body)
  response.signal_headers(
    status_code,
    'data' => Time.now.httpdate,
    'x-amz-request-id' => request_id
  )
  unless request.http_method == 'HEAD'
    response.signal_data(body)
  end
end

#serve_internal_errorObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/canistor/error_handler.rb', line 83

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) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/canistor/error_handler.rb', line 23

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_no_such_bucket(subject) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/canistor/error_handler.rb', line 48

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) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/canistor/error_handler.rb', line 60

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_signature_does_not_match(authorization) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/canistor/error_handler.rb', line 35

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_errorObject



111
112
113
# File 'lib/canistor/error_handler.rb', line 111

def trigger_fatal_error
  response.signal_error(RuntimeError.new("Fatal error."))
end

#trigger_reset_connectionObject



105
106
107
108
109
# File 'lib/canistor/error_handler.rb', line 105

def trigger_reset_connection
  response.signal_error(Seahorse::Client::NetworkingError.new(
    Errno::ECONNRESET.new, 'Remote host reset the connection request.'
  ))
end