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



213
214
215
# File 'lib/canistor/error_handler.rb', line 213

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

.serve_bad_request(context) ⇒ Object



181
182
183
# File 'lib/canistor/error_handler.rb', line 181

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

.serve_continue(context) ⇒ Object



177
178
179
# File 'lib/canistor/error_handler.rb', line 177

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

.serve_internal_error(context) ⇒ Object



217
218
219
# 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) ⇒ Object



185
186
187
# 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) ⇒ Object



205
206
207
# 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) ⇒ Object



209
210
211
# 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) ⇒ Object



193
194
195
# 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) ⇒ Object



201
202
203
# 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) ⇒ Object



197
198
199
# 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) ⇒ Object



189
190
191
# 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) ⇒ Object



225
226
227
# File 'lib/canistor/error_handler.rb', line 225

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

.trigger_reset_connection(context) ⇒ Object



221
222
223
# File 'lib/canistor/error_handler.rb', line 221

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



134
135
136
137
138
139
140
141
142
143
# 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_requestObject



31
32
33
34
35
36
37
38
39
40
# 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_continueObject



23
24
25
26
27
28
29
# 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) ⇒ Object



156
157
158
159
160
161
162
163
164
165
# 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_errorObject



145
146
147
148
149
150
151
152
153
154
# 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) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# 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) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# 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) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
# 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) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# 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) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# 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) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# 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) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# 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_errorObject



173
174
175
# File 'lib/canistor/error_handler.rb', line 173

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

#trigger_reset_connectionObject



167
168
169
170
171
# 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