diff --git a/lib/anthropic/client/base.rb b/lib/anthropic/client/base.rb index 4909bca..c38d039 100644 --- a/lib/anthropic/client/base.rb +++ b/lib/anthropic/client/base.rb @@ -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) ## diff --git a/lib/anthropic/client/standard.rb b/lib/anthropic/client/standard.rb index 6c16efe..e5f6986 100644 --- a/lib/anthropic/client/standard.rb +++ b/lib/anthropic/client/standard.rb @@ -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 diff --git a/lib/anthropic/client/streaming.rb b/lib/anthropic/client/streaming.rb index a849a86..195b30b 100644 --- a/lib/anthropic/client/streaming.rb +++ b/lib/anthropic/client/streaming.rb @@ -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 diff --git a/lib/anthropic/errors/client.rb b/lib/anthropic/errors/client.rb deleted file mode 100644 index f854c38..0000000 --- a/lib/anthropic/errors/client.rb +++ /dev/null @@ -1,41 +0,0 @@ -# frozen_string_literal: true - -module Anthropic - module Errors - ## - # Error when the request is malformed. - class InvalidRequestError < StandardError; end - - ## - # Error when the API key is invalid. - class AuthenticationError < StandardError; end - - ## - # Error when the account does not have permission for the operation. - class PermissionError < StandardError; end - - ## - # Error when the resource is not found. - class NotFoundError < StandardError; end - - ## - # Error when the resource already exists. - class ConflictError < StandardError; end - - ## - # Error when the resource cannot be processed. - class UnprocessableEntityError < StandardError; end - - ## - # Error when the request exceeds the rate limit. - class RateLimitError < StandardError; end - - ## - # Error when the server experienced an internal error. - class ApiError < StandardError; end - - ## - # Error when the API servers are overloaded. - class OverloadedError < StandardError; end - end -end diff --git a/spec/support/shared/client_shared_examples.rb b/spec/support/shared/client_shared_examples.rb index c00eb93..89c76eb 100644 --- a/spec/support/shared/client_shared_examples.rb +++ b/spec/support/shared/client_shared_examples.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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