Skip to content

Commit

Permalink
Work on dynamic error definition
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeheft committed Jul 8, 2024
1 parent e716c00 commit 2fd3dc7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/middleware/error_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def call(env)
rescue ApiException::RecordNotFound => e
render_error_response(e, 404, :not_found)
rescue StandardError => e
render_error_response(e, 500, :internal_error)
render_error_response(e, 500, e.status || :internal_error)
end

private def render_error_response(error, status, code)
Expand Down
2 changes: 1 addition & 1 deletion lib/api_exception/base_exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def initialize(message, code = 500, status = "error")
class HTTPRequestError < BaseException; end
class JSONParserError < BaseException; end
class NoBlockGivenError < BaseException; end
class NotFoundError < BaseException; end
class RecordNotFound < BaseException; end
class RetryError < BaseException; end
class RideCountMismatchError < BaseException; end
end
13 changes: 9 additions & 4 deletions lib/api_exception/error_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@ def map_error(exception)
end

private def define_error_class(class_name)
ApiException.module_eval <<~RUBY, __FILE__, __LINE__ + 1
class #{class_name.split('::').last} < ApiException::BaseException
clean_class_name = class_name.split("::").last.gsub(/[^\w]/, "") # Clean up class name

# Define the error class if it doesn't exist
unless Object.const_defined?(clean_class_name)
error_class = Class.new(ApiException::BaseException) do
def initialize(msg = nil, code = nil, status = nil)
super(msg, code, status)
end
end
RUBY

class_name.constantize
ApiException.const_set(clean_class_name, error_class)
end

ApiException.const_get(clean_class_name)
end
end
end
7 changes: 5 additions & 2 deletions lib/rides/commands/get_routes_data.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "./lib/api_exception/base_exception"

module Rides
module Commands
# Makes a request to the Google API to obtain the route information for all the
Expand Down Expand Up @@ -95,8 +97,9 @@ def call(rides:)
end

if response.status != 200
error = JSON.parse(response.error, symbolize_names: true)
raise HTTPRequestError, error[:message]
result = JSON.parse(response.body, symbolize_names: true)
error = result.first[:error]
raise ApiException::HTTPRequestError.new(error[:message], error[:status].downcase.to_sym, error[:code])
else
body = response.body
cache_response!(key, body)
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/drivers/selectable_rides_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
describe "errors" do
it "raises NotFoundError when unable to find driver" do
get "/drivers/1/selectable_rides"
# binding.pry
binding.pry
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)
Expand Down

0 comments on commit 2fd3dc7

Please sign in to comment.