Skip to content

Commit

Permalink
standardize client and server error codes capture (#10770)
Browse files Browse the repository at this point in the history
  • Loading branch information
sachin-sandhu authored Oct 10, 2024
1 parent 025db54 commit 8a93b6a
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,21 @@ class PoetryErrorHandler < UpdateChecker
# package version mentioned in .toml not found in package index
PACKAGE_NOT_FOUND = /Package (?<pkg>.*) ((?<req_ver>.*)) not found./

# error code 401 while accessing registry
ERROR_401 = /401 Client Error/
# client access error codes while accessing package index
CLIENT_ERROR_CODES = T.let({
error401: /401 Client Error/,
error403: /403 Client Error/,
error404: /404 Client Error/,
http403: /HTTP error 403/,
http404: /HTTP error 404/
}.freeze, T::Hash[T.nilable(String), Regexp])

# server response error codes while accessing package index
SERVER_ERROR_CODES = T.let({
server502: /502 Server Error/,
server503: /503 Server Error/,
server504: /504 Server Error/
}.freeze, T::Hash[T.nilable(String), Regexp])

sig do
params(
Expand Down Expand Up @@ -386,6 +399,8 @@ def sanitize_url(url)

public

# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/PerceivedComplexity
sig { params(error: Exception).void }
def handle_poetry_error(error)
Dependabot.logger.warn(error.message)
Expand All @@ -403,11 +418,22 @@ def handle_poetry_error(error)

raise DependencyFileNotResolvable, error.message if error.message.match(PYTHON_RANGE_NOT_SATISFIED)

return unless error.message.match?(ERROR_401)
SERVER_ERROR_CODES.each do |(_error_codes, error_regex)|
next unless error.message.match?(error_regex)

url = URI.extract(error.message).first.then { sanitize_url(_1) }
raise PrivateSourceAuthenticationFailure, url
index_url = URI.extract(error.message.to_s).last .then { sanitize_url(_1) }
raise InconsistentRegistryResponse, index_url
end

CLIENT_ERROR_CODES.each do |(_error_codes, error_regex)|
next unless error.message.match?(error_regex)

index_url = URI.extract(error.message.to_s).last .then { sanitize_url(_1) }
raise PrivateSourceAuthenticationFailure, index_url
end
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/PerceivedComplexity
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,69 @@
end
end

context "with private registry authentication 403 Client Error" do
let(:response) do
"Creating virtualenv reimbursement-coverage-api-fKdRenE--py3.12 in /home/dependabot/.cache/pypoetry/virtualenvs
Updating dependencies
Resolving dependencies...
403 Client Error: for url: https://fp-pypi.sm00p.com/simple/"
end

it "raises a helpful error" do
expect { poetry_error_handler }.to raise_error(Dependabot::PrivateSourceAuthenticationFailure) do |error|
expect(error.message)
.to include("https://fp-pypi.sm00p.com")
end
end
end

context "with private registry authentication 404 Client Error" do
let(:response) do
"NetworkConnectionError('404 Client Error: Not Found for url: https://raw.example.com/example/flow/" \
"constraints-$%7BAIRFLOW_VERSION%7D/constraints-3.10.txt'"
end

it "raises a helpful error" do
expect { poetry_error_handler }.to raise_error(Dependabot::PrivateSourceAuthenticationFailure) do |error|
expect(error.message)
.to include("https://raw.example.com")
end
end
end

context "with private registry authentication 504 Server Error" do
let(:response) do
"Creating virtualenv alk-service-import-product-TbrdR40A-py3.8 in /home/dependabot/.cache/pypoetry/virtualenvs
Updating dependencies
Resolving dependencies...
504 Server Error: for url: https://pypi.com:8443/packages/alk_ci-1.whl#sha256=f9"
end

it "raises a helpful error" do
expect { poetry_error_handler }.to raise_error(Dependabot::InconsistentRegistryResponse) do |error|
expect(error.message)
.to include("https://pypi.com")
end
end
end

context "with private index authentication HTTP error 404" do
let(:response) do
"HTTP error 404 while getting " \
"https://<redacted>.com/compute-cloud/8e1a9.zip" \
"Not Found for URL" \
" https://example.com/compute-cloud/[FILTERED_REPO]/archive/a5bf58e7a37be7503e1a79febf8b555b9d28e1a9.zip"
end

it "raises a helpful error" do
expect { poetry_error_handler }.to raise_error(Dependabot::PrivateSourceAuthenticationFailure) do |error|
expect(error.message)
.to include("https://example.com")
end
end
end

context "with dependency spec version not found in package index" do
let(:response) do
"Creating virtualenv pyiceberg-xBYdM_d2-py3.12 in /home/dependabot/.cache/pypoetry/virtualenvs
Expand Down

0 comments on commit 8a93b6a

Please sign in to comment.