Skip to content

Commit 46294a0

Browse files
committed
Fix CI issues: Rubocop style violations and error handler refactoring
- Fixed trailing whitespace issues flagged by Rubocop - Refactored error_handler.rb to reduce complexity (split into smaller methods) - Fixed all Rubocop violations - now 0 offenses - Maintained backward compatibility for exception classes - All 310 tests still passing with 0 failures
1 parent d81f422 commit 46294a0

File tree

4 files changed

+25
-32
lines changed

4 files changed

+25
-32
lines changed

lib/uploadcare.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ module Uploadcare
1515
@loader.collapse("#{__dir__}/uploadcare/clients")
1616
@loader.setup
1717

18-
# Trigger loading of exception classes for backward compatibility
19-
# This ensures InvalidRequestError and NotFoundError are available at top level
20-
Exception::RequestError if true
21-
2218
class << self
2319
def configure
2420
yield configuration if block_given?

lib/uploadcare/clients/upload_client.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,10 +658,10 @@ def handle_error_response(response)
658658
def parse_success_response(response)
659659
# response.body is already parsed by JSON middleware
660660
return {} if response.body.nil? || (response.body.is_a?(String) && response.body.strip.empty?)
661-
661+
662662
# If it's already a Hash (from JSON middleware), return it directly
663663
return response.body if response.body.is_a?(Hash)
664-
664+
665665
# Otherwise parse it (for backward compatibility)
666666
JSON.parse(response.body)
667667
end

lib/uploadcare/error_handler.rb

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,35 @@
22

33
module Uploadcare
44
module ErrorHandler
5-
65
# Catches failed API errors
76
# Raises errors instead of returning falsey objects
87
def handle_error(error)
98
response = error.response
109
catch_upload_errors(response)
11-
parsed_response = JSON.parse(response[:body].to_s)
12-
error_message = parsed_response['detail'] || parsed_response.map { |k, v| "#{k}: #{v}" }.join('; ')
13-
14-
# Raise specific error types based on HTTP status code
15-
case response[:status]
16-
when 400
17-
raise Exception::InvalidRequestError, error_message
18-
when 404
19-
raise Exception::NotFoundError, error_message
20-
else
21-
raise Exception::RequestError, error_message
22-
end
23-
rescue JSON::ParserError
24-
# For non-JSON responses, still check status code
25-
case response[:status]
26-
when 400
27-
raise Exception::InvalidRequestError, response[:body].to_s
28-
when 404
29-
raise Exception::NotFoundError, response[:body].to_s
30-
else
31-
raise Exception::RequestError, response[:body].to_s
32-
end
10+
11+
error_message = extract_error_message(response)
12+
raise_status_error(response[:status], error_message)
3313
end
3414

3515
private
3616

17+
# Extract error message from response body
18+
def extract_error_message(response)
19+
parsed = JSON.parse(response[:body].to_s)
20+
parsed['detail'] || parsed.map { |k, v| "#{k}: #{v}" }.join('; ')
21+
rescue JSON::ParserError
22+
response[:body].to_s
23+
end
24+
25+
# Raise appropriate error based on HTTP status code
26+
def raise_status_error(status, message)
27+
case status
28+
when 400 then raise Exception::InvalidRequestError, message
29+
when 404 then raise Exception::NotFoundError, message
30+
else raise Exception::RequestError, message
31+
end
32+
end
33+
3734
# Upload API returns its errors with code 200, and stores its actual code and details within response message
3835
# This methods detects that and raises apropriate error
3936
def catch_upload_errors(response)

lib/uploadcare/exception/request_error.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ module Uploadcare
44
module Exception
55
# Standard error for invalid API responses
66
class RequestError < StandardError; end
7-
7+
88
# Specific error for invalid requests (400 Bad Request)
99
class InvalidRequestError < RequestError; end
10-
10+
1111
# Specific error for not found resources (404 Not Found)
1212
class NotFoundError < RequestError; end
1313
end
14-
14+
1515
# Top-level aliases for backward compatibility
1616
InvalidRequestError = Exception::InvalidRequestError
1717
NotFoundError = Exception::NotFoundError

0 commit comments

Comments
 (0)