Module: Exceptions::ResponseMappings

Included in:
ArchivesSpaceService
Defined in:
backend/app/lib/exceptions.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'backend/app/lib/exceptions.rb', line 119

def self.included(base)
  base.instance_eval do

    error ImportException do
      json_response({:error => request.env['sinatra.error'].to_hash}, 400)
    end

    error RepositoryNotEmpty do
      json_response({:error => "Repository not empty"}, 409)
    end

    error Sinatra::NotFound do
      json_response({:error => request.env['sinatra.error']}, 404)
    end

    error NotFoundException do
      json_response({:error => request.env['sinatra.error']}, 404)
    end

    error BadParamsException do
      json_response({:error => request.env['sinatra.error'].params}, 400)
    end

    error ReadOnlyException do
      json_response({:error => request.env['sinatra.error']}, 409)
    end

    error UserNotFoundException do
      json_response({:error => {"member_usernames" => [request.env['sinatra.error']]}}, 400)
    end

    error BatchDeleteFailed do
      Log.exception(request.env['sinatra.error'])
      json_response({:error => {"failures" => request.env['sinatra.error'].errors}}, 403)
    end

    error TransferConstraintError do
      Log.exception(request.env['sinatra.error'])
      json_response({:error => request.env['sinatra.error'].conflicts}, 409)
    end

    error JSONModel::ValidationException do
      json_response({
                      :error => request.env['sinatra.error'].errors,
                      :warning => request.env['sinatra.error'].warnings,
                      :invalid_object => request.env['sinatra.error'].invalid_object.inspect
                    }, 400)
    end

    error ConflictException do
      json_response({:error => request.env['sinatra.error'].conflicts}, 409)
    end

    error AccessDeniedException do
      json_response({:error => "Access denied"}, 403)
    end

    error InvalidUsernameException do
      json_response({:error => "Invalid username"}, 400)
    end

    error Sequel::ValidationFailed do
      json_response({:error => request.env['sinatra.error'].errors}, 400)
    end

    error ReferenceError do
      Log.exception(request.env['sinatra.error'])
      json_response({:error => request.env['sinatra.error']}, 400)
    end

    error MergeRequestFailed do
      Log.exception(request.env['sinatra.error'])
      json_response({:error => request.env['sinatra.error']}, 400)
    end

    error EnumerationMigrationFailed do
      Log.exception(request.env['sinatra.error'])
      json_response({:error => request.env['sinatra.error']}, 400)
    end

    error Sequel::DatabaseError do
      Log.exception(request.env['sinatra.error'])
      json_response({:error => {:db_error => ["Database integrity constraint conflict: #{request.env['sinatra.error']}"]}}, 400)
    end

    error Sequel::Plugins::OptimisticLocking::Error do
      json_response({:error => "The record you tried to update has been modified since you fetched it."}, 409)
    end

    error JSON::ParserError do
      Log.exception(request.env['sinatra.error'])
      json_response({:error => "Had some trouble parsing your request: #{request.env['sinatra.error']}"}, 400)
    end


    # Overriding Sinatra's default behaviour here
    define_method(:handle_exception!) do |ex|
      @env['sinatra.error'] = ex
      status ex.respond_to?(:code) ? Integer(ex.code) : 500

      if not_found?
        headers['X-Cascade'] = 'pass'
        body '<h1>Not Found</h1>'
        return
      end

      res = error_block!(ex.class, ex) || error_block!(status, ex)

      if res
        # One of our custom error handlers has saved the day
        res
      else
        Log.error('Unhandled exception!')
        Log.exception(request.env['sinatra.error'])

        message = ex.message + ": " + ex.backtrace.join("\n\t")

        json_response({:error => message}, 500)
      end
    end
  end
end