Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve UX of error messages when wrong files #16091

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion conan/api/subapi/remotes.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,10 @@ def _load(remotes_file):
_save(remotes_file, [remote])
return [remote]

data = json.loads(load(remotes_file))
try:
data = json.loads(load(remotes_file))
except Exception as e:
raise ConanException(f"Error loading JSON remotes file '{remotes_file}': {e}")
result = []
for r in data.get("remotes", []):
remote = Remote(r["name"], r["url"], r["verify_ssl"], r.get("disabled", False),
Expand Down
10 changes: 5 additions & 5 deletions conans/client/rest/conan_requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ def __init__(self, cache_folder):
creds_path = os.path.join(cache_folder, "source_credentials.json")
if not os.path.exists(creds_path):
return
template = Template(load(creds_path))
content = template.render({"platform": platform, "os": os})
content = json.loads(content)

def _get_auth(credentials):
result = {}
Expand All @@ -52,10 +49,13 @@ def _get_auth(credentials):
raise ConanException(f"Unknown credentials method for '{credentials['url']}'")

try:
template = Template(load(creds_path))
content = template.render({"platform": platform, "os": os})
content = json.loads(content)
self._urls = {credentials["url"]: _get_auth(credentials)
for credentials in content["credentials"]}
except KeyError as e:
raise ConanException(f"Authentication error, wrong source_credentials.json layout: {e}")
except Exception as e:
raise ConanException(f"Error loading 'source_credentials.json' {creds_path}: {repr(e)}")

def add_auth(self, url, kwargs):
for u, creds in self._urls.items():
Expand Down
15 changes: 9 additions & 6 deletions conans/client/rest/remote_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ def __init__(self, cache_folder, global_conf):
creds_path = os.path.join(cache_folder, "credentials.json")
if not os.path.exists(creds_path):
return
template = Template(load(creds_path))
content = template.render({"platform": platform, "os": os})
content = json.loads(content)
try:
template = Template(load(creds_path))
content = template.render({"platform": platform, "os": os})
content = json.loads(content)

self._urls = {credentials["remote"]: {"user": credentials["user"],
"password": credentials["password"]}
for credentials in content["credentials"]}
self._urls = {credentials["remote"]: {"user": credentials["user"],
"password": credentials["password"]}
for credentials in content["credentials"]}
except Exception as e:
raise ConanException(f"Error loading 'credentials.json' {creds_path}: {repr(e)}")

def auth(self, remote, user=None, password=None):
if user is not None and password is not None:
Expand Down
7 changes: 7 additions & 0 deletions conans/test/integration/command/remote_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,13 @@ def test_add_wrong_conancenter():
assert "the correct remote API is https://center.conan.io" in c.out


def test_wrong_remotes_json_file():
c = TestClient(light=True)
c.save_home({"remotes.json": ""})
c.run("remote list", assert_error=True)
assert "ERROR: Error loading JSON remotes file" in c.out


def test_allowed_packages_remotes():
tc = TestClient(light=True, default_server_user=True)
tc.save({"conanfile.py": GenConanfile(),
Expand Down
11 changes: 11 additions & 0 deletions conans/test/integration/remote/test_remote_file_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,14 @@ def test_remote_file_credentials_error(client):
save(os.path.join(c.cache_folder, "credentials.json"), json.dumps(content))
c.run("upload * -r=default -c", assert_error=True)
assert "ERROR: Wrong user or password" in c.out


def test_remote_file_credentials_bad_file(client):
c = client
save(os.path.join(c.cache_folder, "credentials.json"), "")
c.run("upload * -r=default -c", assert_error=True)
assert "ERROR: Error loading 'credentials.json'" in c.out
content = {"credentials": [{"remote": "default"}]}
save(os.path.join(c.cache_folder, "credentials.json"), json.dumps(content))
c.run("upload * -r=default -c", assert_error=True)
assert "ERROR: Error loading 'credentials.json'" in c.out
15 changes: 11 additions & 4 deletions conans/test/integration/test_source_download_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,21 @@ def source(self):
c.run("source .")
assert "Content: hello world!" in c.out

# Errors
for invalid in [{"token": "mytoken"},
{"url": server_url, "token": "mytoken2"}, # Unauthorized
# Errors loading file
for invalid in ["",
"potato",
{"token": "mytoken"},
{},
{"url": server_url},
{"auth": {}},
{"user": "other", "password": "pass"}]:
content = {"credentials": [invalid]}
save(os.path.join(c.cache_folder, "source_credentials.json"), json.dumps(content))
c.run("source .", assert_error=True)
assert "Authentication" in c.out or "Unknown credentials" in c.out
assert "Error loading 'source_credentials.json'" in c.out

content = {"credentials": [{"url": server_url, "token": "mytoken2"}]}
save(os.path.join(c.cache_folder, "source_credentials.json"), json.dumps(content))
c.run("source .", assert_error=True)
assert "ERROR: conanfile.py: Error in source() method, line 6" in c.out
assert "Authentication" in c.out