Skip to content

Commit

Permalink
Add spec for different error
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeheft committed Jul 8, 2024
1 parent 1063139 commit ac45a86
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/api_exception/base_exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/client/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion lib/rides/commands/get_routes_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions lib/rides/commands/rank_rides.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions spec/errors/error_spec.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions spec/requests/drivers/selectable_rides_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ac45a86

Please sign in to comment.