Skip to content

Commit

Permalink
improve error messages in ConanApi init failures (conan-io#14735)
Browse files Browse the repository at this point in the history
* improve error messages in ConanApi init failures

* fix
  • Loading branch information
memsharded authored and NoWiseMan committed Sep 19, 2023
1 parent e3bc22f commit 2caf0a1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
9 changes: 6 additions & 3 deletions conans/client/cache/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ def __init__(self, cache_folder):
self._store_folder = self.new_config.get("core.cache:storage_path") or \
os.path.join(self.cache_folder, "p")

mkdir(self._store_folder)
db_filename = os.path.join(self._store_folder, 'cache.sqlite3')
self._data_cache = DataCache(self._store_folder, db_filename)
try:
mkdir(self._store_folder)
db_filename = os.path.join(self._store_folder, 'cache.sqlite3')
self._data_cache = DataCache(self._store_folder, db_filename)
except Exception as e:
raise ConanException(f"Couldn't initialize storage in {self._store_folder}: {e}")

@property
def temp_folder(self):
Expand Down
5 changes: 2 additions & 3 deletions conans/test/integration/configuration/conf/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,9 @@ def test_new_config_file_required_version():
core:required_conan_version=>=2.0
""")
save(client.cache.new_config_path, conf)
with pytest.raises(ConanException) as excinfo:
client.run("install .")
client.run("install .", assert_error=True)
assert ("Current Conan version (1.26.0) does not satisfy the defined one (>=2.0)"
in str(excinfo.value))
in client.out)


def test_composition_conan_conf_overwritten_by_cli_arg(client):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ def test_wrong_version(self):
client = TestClient()
client.save({"global.conf": f"core:required_conan_version={required_version}"},
path=client.cache.cache_folder)
with self.assertRaises(ConanException) as error:
client.run("help")
client.run("help", assert_error=True)
self.assertIn("Current Conan version (1.26.0) does not satisfy the defined "
"one ({})".format(required_version), str(error.exception))
"one ({})".format(required_version), client.out)

@mock.patch("conans.client.conf.required_version.client_version", "1.22.0")
def test_exact_version(self):
Expand Down
8 changes: 8 additions & 0 deletions conans/test/integration/ui/exit_with_code_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from conans.paths import CONANFILE
from conans.test.utils.tools import TestClient
from conans.util.files import save


class ExitWithCodeTest(unittest.TestCase):
Expand All @@ -27,3 +28,10 @@ def build(self):
error_code = client.run("build .", assert_error=True)
self.assertEqual(error_code, 34)
self.assertIn("Exiting with code: 34", client.out)


def test_wrong_home_error():
client = TestClient()
save(client.cache.new_config_path, "core.cache:storage_path=//")
client.run("--version")
assert "Error in Conan initialization: Couldn't initialize storage in" in client.out
12 changes: 8 additions & 4 deletions conans/test/utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
from requests.exceptions import HTTPError
from webtest.app import TestApp

from conan.cli.exit_codes import SUCCESS
from conan.cli.exit_codes import SUCCESS, ERROR_GENERAL
from conan.internal.cache.cache import PackageLayout, RecipeLayout
from conans import REVISIONS
from conan.api.conan_api import ConanAPI
from conan.api.model import Remote
from conan.cli.cli import Cli
from conans.client.cache.cache import ClientCache
from conans.util.env import environment_update
from conans.errors import NotFoundException
from conans.errors import NotFoundException, ConanException
from conans.model.manifest import FileTreeManifest
from conans.model.package_ref import PkgReference
from conans.model.profile import Profile
Expand Down Expand Up @@ -489,8 +489,12 @@ def _run_cli(self, command_line, assert_error=False):

args = shlex.split(command_line)

self.api = ConanAPI(cache_folder=self.cache_folder)
command = Cli(self.api)
try:
self.api = ConanAPI(cache_folder=self.cache_folder)
command = Cli(self.api)
except ConanException as e:
sys.stderr.write("Error in Conan initialization: {}".format(e))
return ERROR_GENERAL

error = SUCCESS
trace = None
Expand Down

0 comments on commit 2caf0a1

Please sign in to comment.