error_handling_spec.rb 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Api::ErrorHandling do
  4. before do
  5. stub_const('FakeService', Class.new)
  6. end
  7. controller(Api::BaseController) do
  8. def failure
  9. FakeService.new
  10. end
  11. end
  12. describe 'error handling' do
  13. before do
  14. routes.draw { get 'failure' => 'api/base#failure' }
  15. end
  16. {
  17. ActiveRecord::RecordInvalid => 422,
  18. ActiveRecord::RecordNotFound => 404,
  19. ActiveRecord::RecordNotUnique => 422,
  20. Date::Error => 422,
  21. HTTP::Error => 503,
  22. Mastodon::InvalidParameterError => 400,
  23. Mastodon::NotPermittedError => 403,
  24. Mastodon::RaceConditionError => 503,
  25. Mastodon::RateLimitExceededError => 429,
  26. Mastodon::UnexpectedResponseError => 503,
  27. Mastodon::ValidationError => 422,
  28. OpenSSL::SSL::SSLError => 503,
  29. Seahorse::Client::NetworkingError => 503,
  30. Stoplight::Error::RedLight => 503,
  31. }.each do |error, code|
  32. it "Handles error class of #{error}" do
  33. allow(FakeService)
  34. .to receive(:new)
  35. .and_raise(error)
  36. get :failure
  37. expect(response)
  38. .to have_http_status(code)
  39. expect(FakeService)
  40. .to have_received(:new)
  41. end
  42. end
  43. end
  44. end