Add Api::ErrorHandling concern for api/base controller (#29574)
				
					
				
			This commit is contained in:
		@@ -3,10 +3,6 @@
 | 
			
		||||
require 'rails_helper'
 | 
			
		||||
 | 
			
		||||
describe Api::BaseController do
 | 
			
		||||
  before do
 | 
			
		||||
    stub_const('FakeService', Class.new)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  controller do
 | 
			
		||||
    def success
 | 
			
		||||
      head 200
 | 
			
		||||
@@ -72,36 +68,4 @@ describe Api::BaseController do
 | 
			
		||||
      expect(response).to have_http_status(403)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe 'error handling' do
 | 
			
		||||
    before do
 | 
			
		||||
      routes.draw { get 'failure' => 'api/base#failure' }
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
      ActiveRecord::RecordInvalid => 422,
 | 
			
		||||
      ActiveRecord::RecordNotFound => 404,
 | 
			
		||||
      ActiveRecord::RecordNotUnique => 422,
 | 
			
		||||
      Date::Error => 422,
 | 
			
		||||
      HTTP::Error => 503,
 | 
			
		||||
      Mastodon::InvalidParameterError => 400,
 | 
			
		||||
      Mastodon::NotPermittedError => 403,
 | 
			
		||||
      Mastodon::RaceConditionError => 503,
 | 
			
		||||
      Mastodon::RateLimitExceededError => 429,
 | 
			
		||||
      Mastodon::UnexpectedResponseError => 503,
 | 
			
		||||
      Mastodon::ValidationError => 422,
 | 
			
		||||
      OpenSSL::SSL::SSLError => 503,
 | 
			
		||||
      Seahorse::Client::NetworkingError => 503,
 | 
			
		||||
      Stoplight::Error::RedLight => 503,
 | 
			
		||||
    }.each do |error, code|
 | 
			
		||||
      it "Handles error class of #{error}" do
 | 
			
		||||
        allow(FakeService).to receive(:new).and_raise(error)
 | 
			
		||||
 | 
			
		||||
        get :failure
 | 
			
		||||
 | 
			
		||||
        expect(response).to have_http_status(code)
 | 
			
		||||
        expect(FakeService).to have_received(:new)
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										51
									
								
								spec/controllers/concerns/api/error_handling_spec.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								spec/controllers/concerns/api/error_handling_spec.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,51 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
require 'rails_helper'
 | 
			
		||||
 | 
			
		||||
describe Api::ErrorHandling do
 | 
			
		||||
  before do
 | 
			
		||||
    stub_const('FakeService', Class.new)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  controller(Api::BaseController) do
 | 
			
		||||
    def failure
 | 
			
		||||
      FakeService.new
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe 'error handling' do
 | 
			
		||||
    before do
 | 
			
		||||
      routes.draw { get 'failure' => 'api/base#failure' }
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
      ActiveRecord::RecordInvalid => 422,
 | 
			
		||||
      ActiveRecord::RecordNotFound => 404,
 | 
			
		||||
      ActiveRecord::RecordNotUnique => 422,
 | 
			
		||||
      Date::Error => 422,
 | 
			
		||||
      HTTP::Error => 503,
 | 
			
		||||
      Mastodon::InvalidParameterError => 400,
 | 
			
		||||
      Mastodon::NotPermittedError => 403,
 | 
			
		||||
      Mastodon::RaceConditionError => 503,
 | 
			
		||||
      Mastodon::RateLimitExceededError => 429,
 | 
			
		||||
      Mastodon::UnexpectedResponseError => 503,
 | 
			
		||||
      Mastodon::ValidationError => 422,
 | 
			
		||||
      OpenSSL::SSL::SSLError => 503,
 | 
			
		||||
      Seahorse::Client::NetworkingError => 503,
 | 
			
		||||
      Stoplight::Error::RedLight => 503,
 | 
			
		||||
    }.each do |error, code|
 | 
			
		||||
      it "Handles error class of #{error}" do
 | 
			
		||||
        allow(FakeService)
 | 
			
		||||
          .to receive(:new)
 | 
			
		||||
          .and_raise(error)
 | 
			
		||||
 | 
			
		||||
        get :failure
 | 
			
		||||
 | 
			
		||||
        expect(response)
 | 
			
		||||
          .to have_http_status(code)
 | 
			
		||||
        expect(FakeService)
 | 
			
		||||
          .to have_received(:new)
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
		Reference in New Issue
	
	Block a user