Skip to content

Commit

Permalink
Use JSON to pass error hash instead of dangerous instance_eval
Browse files Browse the repository at this point in the history
  • Loading branch information
pixeltrix committed Dec 1, 2023
1 parent 1cfee9b commit 4f9b98e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
8 changes: 4 additions & 4 deletions app/controllers/concerns/api/error_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ module ErrorHandler
private

def format_error(error)
error_hash = instance_eval(error.message)
error_hash = JSON.parse(error.message)

response = error_hash[:response]
response = error_hash["response"]
message = response["message"] || response
status = error_hash[:status].to_s || API_ERROR.to_s
http_method = error_hash[:http_method].to_s
status = error_hash["status"].to_s || API_ERROR.to_s
http_method = error_hash["http_method"].to_s

respond_to do |format|
format.json do
Expand Down
2 changes: 1 addition & 1 deletion app/services/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def call # rubocop:disable Metrics/CyclomaticComplexity
attr_reader :connection, :http_method, :params, :upload_file

def errors(response, status)
{response:, status:, http_method:}
{response:, status:, http_method:}.to_json
end

def get_response_and_status # rubocop:disable Metrics/AbcSize, Naming/AccessorMethodName
Expand Down
12 changes: 6 additions & 6 deletions spec/services/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
expect do
request.call
end.to raise_error(described_class::BadRequestError)
.with_message("{:response=>\"Bad request\", :status=>400, :http_method=>:get}")
.with_message(%({"response":"Bad request","status":400,"http_method":"get"}))
end
end

Expand All @@ -57,7 +57,7 @@
expect do
request.call
end.to raise_error(described_class::UnauthorizedError)
.with_message("{:response=>\"Unauthorized\", :status=>401, :http_method=>:get}")
.with_message(%({"response":"Unauthorized","status":401,"http_method":"get"}))
end
end

Expand All @@ -73,7 +73,7 @@
expect do
request.call
end.to raise_error(described_class::ForbiddenError)
.with_message("{:response=>\"Forbidden\", :status=>403, :http_method=>:get}")
.with_message(%({"response":"Forbidden","status":403,"http_method":"get"}))
end
end

Expand All @@ -89,7 +89,7 @@
expect do
request.call
end.to raise_error(described_class::RecordNotFoundError)
.with_message("{:response=>\"Record not found\", :status=>404, :http_method=>:get}")
.with_message(%({"response":"Record not found","status":404,"http_method":"get"}))
end
end

Expand All @@ -105,7 +105,7 @@
expect do
request.call
end.to raise_error(described_class::ApiError)
.with_message("{:response=>\"API error\", :status=>500, :http_method=>:get}")
.with_message(%({"response":"API error","status":500,"http_method":"get"}))
end
end

Expand All @@ -121,7 +121,7 @@
expect do
request.call
end.to raise_error(described_class::TimeoutError)
.with_message("{:response=>\"Timeout Error\", :status=>504, :http_method=>:get}")
.with_message(%({"response":"Timeout Error","status":504,"http_method":"get"}))
end
end
end
Expand Down

0 comments on commit 4f9b98e

Please sign in to comment.