diff --git a/lib/api_exception/base_exception.rb b/lib/api_exception/base_exception.rb index ad46b04..7707887 100644 --- a/lib/api_exception/base_exception.rb +++ b/lib/api_exception/base_exception.rb @@ -5,7 +5,7 @@ class BaseException < StandardError include ActiveModel::Serialization attr_reader :status, :code, :message - def initialize(message, code = 500, status = "error") + def initialize(message, code = 500, status = :internal_error) super(message) @status = status @code = code diff --git a/lib/client/helpers.rb b/lib/client/helpers.rb index 2e1496d..37b8ce5 100644 --- a/lib/client/helpers.rb +++ b/lib/client/helpers.rb @@ -3,13 +3,13 @@ module Client module Helpers def with_retries(max_retries: 10, &blk) - raise NoBlockGivenError, "Must provide a block" if blk.blank? + raise ApiException::NoBlockGivenError, "Must provide a block" if blk.blank? retries = 0 begin yield rescue StandardError => _e - raise RetryError unless retries <= max_retries + raise ApiException::RetryError unless retries <= max_retries retries += 1 max_sleep_seconds = Float(2**retries) diff --git a/lib/rides/commands/get_routes_data.rb b/lib/rides/commands/get_routes_data.rb index d1b1f54..696575a 100644 --- a/lib/rides/commands/get_routes_data.rb +++ b/lib/rides/commands/get_routes_data.rb @@ -37,7 +37,10 @@ def call(rides:) # we want the computations where the indicies match. data = data.flatten.select { _1[:originIndex] == _1[:destinationIndex] } if data.length != rides.length - raise RideCountMismatchError, "The number of routes does not match the number of rides.", 500, :internal_error + raise ApiException::RideCountMismatchError, + "The number of routes does not match the number of rides.", + 500, + :internal_error end data = transform_keys!(data) diff --git a/lib/rides/commands/rank_rides.rb b/lib/rides/commands/rank_rides.rb index e125ef8..5f7a3ef 100644 --- a/lib/rides/commands/rank_rides.rb +++ b/lib/rides/commands/rank_rides.rb @@ -42,9 +42,11 @@ def call(driver:) rides = route_data(driver) if commutes.count != rides.count - raise RideCountMismatchError, + raise ApiException::RideCountMismatchError, "The number of rides doesn't match the number of commute rides." \ - "Please check the ride(s) configuration and try again." + "Please check the ride(s) configuration and try again.", + 500, + :internal_error end combine_rides!(rides, commutes) diff --git a/spec/errors/error_spec.rb b/spec/errors/error_spec.rb new file mode 100644 index 0000000..883facf --- /dev/null +++ b/spec/errors/error_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe "Errors", :skip_geocode, type: :request do + it "raises RetryError" do + expect(Rides::Commands::GetRoutesData).to receive(:call).and_raise( + ApiException::JSONParserError.new("Attemped to parse invalid JSON:"), 500 + ) + driver = create(:driver) + get "/drivers/#{driver.id}/selectable_rides" + + expect(response).to have_http_status(500) + result = JSON.parse(response.body, symbolize_names: true) + + expect(result[:error].keys).to include(:status, :code, :message) + expect(result.dig(:error, :message)).to eq("Attemped to parse invalid JSON:") + expect(result.dig(:error, :code)).to eq("internal_error") + end +end diff --git a/spec/requests/drivers/selectable_rides_spec.rb b/spec/requests/drivers/selectable_rides_spec.rb index 5b5f1ba..cf94cdf 100644 --- a/spec/requests/drivers/selectable_rides_spec.rb +++ b/spec/requests/drivers/selectable_rides_spec.rb @@ -7,11 +7,13 @@ it "raises NotFoundError when unable to find driver" do get "/drivers/1/selectable_rides" expect(response).to have_http_status(:not_found) + result = JSON.parse(response.body, symbolize_names: true) expect(result[:error].keys).to include(:status, :code, :message) expect(result.dig(:error, :message)).to eq("Couldn't find Driver with 'id'=1") end end + describe "GET /drivers/:driver_id/rides" do it "returns ranked rides" do VCR.use_cassette("ranked_rides") do