Skip to content
Open
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
11 changes: 9 additions & 2 deletions lib/init/grass.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def help_message(default_gui) -> None:
flags=_("Flags"),
help_flag=_("print this help message"),
version_flag=_("show version information and exit"),
create=_("create given database, project or mapset if it doesn't exist"),
create=_("create given project or mapset if it doesn't exist"),
exit_after=_("exit after creation of project or mapset. Only with -c flag"),
force_removal=_(
"force removal of .gislock if exists (use with care!)."
Expand Down Expand Up @@ -666,15 +666,22 @@ def create_location(gisdbase, location, geostring) -> None:
:param geostring: path to a georeferenced file or EPSG code
"""
import grass.script as gs # pylint: disable=E0611
from grass.exceptions import ScriptError

try:
gs.create_project(gisdbase, location, crs=geostring)
except gs.ScriptError as err:
except ScriptError as err:
fatal(
_("Error creating project: {}").format(
err.value.strip('"').strip("'").replace("\\n", os.linesep)
)
)
except OSError as err:
fatal(
_("Cannot create project <{}>: {}: {}").format(
location, err.strerror, err.filename
)
)


def can_create_location(gisdbase: StrPath, location) -> bool:
Expand Down
21 changes: 21 additions & 0 deletions lib/init/tests/grass_create_project_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""Test that grass -c reports a clear error instead of a traceback

SPDX-FileCopyrightText: 2026 Akash Maity
SPDX-FileCopyrightText: GRASS Development Team
SPDX-License-Identifier: GPL-2.0-or-later
"""

import subprocess


def test_missing_database_directory(tmp_path):
"""Missing parent directories give a clear error"""
project = tmp_path / "missing" / "deeper" / "myproject"
result = subprocess.run(
["grass", "-c", "EPSG:4326", str(project), "-e"],
capture_output=True,
text=True,
)
assert result.returncode != 0
assert "Traceback" not in result.stderr
assert "Cannot create project" in result.stderr
Loading