Skip to content

Commit

Permalink
Move client error classes (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
dickdavis committed Jun 26, 2024
1 parent dff7700 commit 16710a7
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 77 deletions.
38 changes: 38 additions & 0 deletions lib/anthropic/client/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,44 @@

module Anthropic
module Client
##
# Error when the server experienced an internal error.
class ApiError < StandardError; end

##
# Error when the API key is invalid.
class AuthenticationError < StandardError; end

##
# Error when the resource already exists.
class ConflictError < StandardError; end

##
# Error when the request is malformed.
class InvalidRequestError < StandardError; end

##
# Error when the resource is not found.
class NotFoundError < StandardError; end

##
# Error when the API servers are overloaded.
class OverloadedError < StandardError; end

##
# Error when the account does not have permission for the operation.
class PermissionError < StandardError; end

##
# Error when the request exceeds the rate limit.
class RateLimitError < StandardError; end

##
# Error when the resource cannot be processed.
class UnprocessableEntityError < StandardError; end

##
# Defines a data object for responses
Response = Data.define(:status, :body)

##
Expand Down
18 changes: 9 additions & 9 deletions lib/anthropic/client/standard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ def self.post(url, data, headers = {})
when 200
response_data
when 400
raise Anthropic::Errors::InvalidRequestError, response_data
raise Anthropic::Client::InvalidRequestError, response_data
when 401
raise Anthropic::Errors::AuthenticationError, response_data
raise Anthropic::Client::AuthenticationError, response_data
when 403
raise Anthropic::Errors::PermissionError, response_data
raise Anthropic::Client::PermissionError, response_data
when 404
raise Anthropic::Errors::NotFoundError, response_data
raise Anthropic::Client::NotFoundError, response_data
when 409
raise Anthropic::Errors::ConflictError, response_data
raise Anthropic::Client::ConflictError, response_data
when 422
raise Anthropic::Errors::UnprocessableEntityError, response_data
raise Anthropic::Client::UnprocessableEntityError, response_data
when 429
raise Anthropic::Errors::RateLimitError, response_data
raise Anthropic::Client::RateLimitError, response_data
when 500
raise Anthropic::Errors::ApiError, response_data
raise Anthropic::Client::ApiError, response_data
when 529
raise Anthropic::Errors::OverloadedError, response_data
raise Anthropic::Client::OverloadedError, response_data
end
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity
Expand Down
18 changes: 9 additions & 9 deletions lib/anthropic/client/streaming.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ def self.post(url, data, headers = {})
rescue HTTPX::HTTPError => error
case error.response.status
when 400
raise Anthropic::Errors::InvalidRequestError
raise Anthropic::Client::InvalidRequestError
when 401
raise Anthropic::Errors::AuthenticationError
raise Anthropic::Client::AuthenticationError
when 403
raise Anthropic::Errors::PermissionError
raise Anthropic::Client::PermissionError
when 404
raise Anthropic::Errors::NotFoundError
raise Anthropic::Client::NotFoundError
when 409
raise Anthropic::Errors::ConflictError
raise Anthropic::Client::ConflictError
when 422
raise Anthropic::Errors::UnprocessableEntityError
raise Anthropic::Client::UnprocessableEntityError
when 429
raise Anthropic::Errors::RateLimitError
raise Anthropic::Client::RateLimitError
when 500
raise Anthropic::Errors::ApiError
raise Anthropic::Client::ApiError
when 529
raise Anthropic::Errors::OverloadedError
raise Anthropic::Client::OverloadedError
end
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
Expand Down
41 changes: 0 additions & 41 deletions lib/anthropic/errors/client.rb

This file was deleted.

36 changes: 18 additions & 18 deletions spec/support/shared/client_shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
}
end

it 'raises an Anthropic::Errors::InvalidRequestError' do
it 'raises an Anthropic::Client::InvalidRequestError' do
stub_http_request(:post, url).and_return(status: 400, body:)
expect { send_request }.to raise_error(Anthropic::Errors::InvalidRequestError)
expect { send_request }.to raise_error(Anthropic::Client::InvalidRequestError)
end
end

Expand All @@ -29,9 +29,9 @@
}
end

it 'raises an Anthropic::Errors::AuthenticationError' do
it 'raises an Anthropic::Client::AuthenticationError' do
stub_http_request(:post, url).and_return(status: 401, body:)
expect { send_request }.to raise_error(Anthropic::Errors::AuthenticationError)
expect { send_request }.to raise_error(Anthropic::Client::AuthenticationError)
end
end

Expand All @@ -46,9 +46,9 @@
}
end

it 'raises an Anthropic::Errors::PermissionError' do
it 'raises an Anthropic::Client::PermissionError' do
stub_http_request(:post, url).and_return(status: 403, body:)
expect { send_request }.to raise_error(Anthropic::Errors::PermissionError)
expect { send_request }.to raise_error(Anthropic::Client::PermissionError)
end
end

Expand All @@ -63,9 +63,9 @@
}
end

it 'raises an Anthropic::Errors::NotFoundError' do
it 'raises an Anthropic::Client::NotFoundError' do
stub_http_request(:post, url).and_return(status: 404, body:)
expect { send_request }.to raise_error(Anthropic::Errors::NotFoundError)
expect { send_request }.to raise_error(Anthropic::Client::NotFoundError)
end
end

Expand All @@ -80,9 +80,9 @@
}
end

it 'raises an Anthropic::Errors::ConflictError' do
it 'raises an Anthropic::Client::ConflictError' do
stub_http_request(:post, url).and_return(status: 409, body:)
expect { send_request }.to raise_error(Anthropic::Errors::ConflictError)
expect { send_request }.to raise_error(Anthropic::Client::ConflictError)
end
end

Expand All @@ -97,9 +97,9 @@
}
end

it 'raises an Anthropic::Errors::UnprocessableEntityError' do
it 'raises an Anthropic::Client::UnprocessableEntityError' do
stub_http_request(:post, url).and_return(status: 422, body:)
expect { send_request }.to raise_error(Anthropic::Errors::UnprocessableEntityError)
expect { send_request }.to raise_error(Anthropic::Client::UnprocessableEntityError)
end
end

Expand All @@ -114,9 +114,9 @@
}
end

it 'raises an Anthropic::Errors::RateLimitError' do
it 'raises an Anthropic::Client::RateLimitError' do
stub_http_request(:post, url).and_return(status: 429, body:)
expect { send_request }.to raise_error(Anthropic::Errors::RateLimitError)
expect { send_request }.to raise_error(Anthropic::Client::RateLimitError)
end
end

Expand All @@ -131,9 +131,9 @@
}
end

it 'raises an Anthropic::Errors::ApiError' do
it 'raises an Anthropic::Client::ApiError' do
stub_http_request(:post, url).and_return(status: 500, body:)
expect { send_request }.to raise_error(Anthropic::Errors::ApiError)
expect { send_request }.to raise_error(Anthropic::Client::ApiError)
end
end

Expand All @@ -148,9 +148,9 @@
}
end

it 'raises an Anthropic::Errors::OverloadedError' do
it 'raises an Anthropic::Client::OverloadedError' do
stub_http_request(:post, url).and_return(status: 529, body:)
expect { send_request }.to raise_error(Anthropic::Errors::OverloadedError)
expect { send_request }.to raise_error(Anthropic::Client::OverloadedError)
end
end
end

0 comments on commit 16710a7

Please sign in to comment.