Module: Exceptions::ResponseMappings

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

Class Method Summary (collapse)

Class Method Details

+ (Object) included(base)



110
111
112
113
114
115
116
117
118
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
# File 'backend/app/lib/exceptions.rb', line 110

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 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 UserNotFoundException do
      json_response({:error => {"member_usernames" => [request.env['sinatra.error']]}}, 400)
    end

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

    error TransferConstraintError do
      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
      json_response({:error => request.env['sinatra.error']}, 400)
    end

    error MergeRequestFailed do
      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
      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)

      res or raise ex
    end

  end
end