Skip to content

Commit 8021ba2

Browse files
refactor(cli): update env execution errors to use report_error
1 parent 101eba7 commit 8021ba2

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

hatch/cli/cli_env.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def handle_env_create(args: Namespace) -> int:
104104
reporter.report_result()
105105
return EXIT_SUCCESS
106106
else:
107-
print(f"[ERROR] Failed to create environment: {name}")
107+
reporter.report_error(f"Failed to create environment '{name}'")
108108
return EXIT_ERROR
109109

110110

@@ -150,7 +150,7 @@ def handle_env_remove(args: Namespace) -> int:
150150
reporter.report_result()
151151
return EXIT_SUCCESS
152152
else:
153-
print(f"[ERROR] Failed to remove environment: {name}")
153+
reporter.report_error(f"Failed to remove environment '{name}'")
154154
return EXIT_ERROR
155155

156156

@@ -182,7 +182,11 @@ def handle_env_list(args: Namespace) -> int:
182182
regex = re.compile(pattern)
183183
environments = [env for env in environments if regex.search(env.get("name", ""))]
184184
except re.error as e:
185-
print(f"[ERROR] Invalid regex pattern: {e}")
185+
format_validation_error(ValidationError(
186+
f"Invalid regex pattern: {e}",
187+
field="--pattern",
188+
suggestion="Use a valid Python regex pattern"
189+
))
186190
return EXIT_ERROR
187191

188192
if json_output:
@@ -269,7 +273,7 @@ def handle_env_use(args: Namespace) -> int:
269273
reporter.report_result()
270274
return EXIT_SUCCESS
271275
else:
272-
print(f"[ERROR] Failed to set environment: {name}")
276+
reporter.report_error(f"Failed to set environment '{name}'")
273277
return EXIT_ERROR
274278

275279

@@ -334,7 +338,7 @@ def handle_env_python_init(args: Namespace) -> int:
334338
reporter.report_result()
335339
return EXIT_SUCCESS
336340
else:
337-
print(f"[ERROR] Failed to initialize Python environment for: {env_name}")
341+
reporter.report_error(f"Failed to initialize Python environment for '{env_name}'")
338342
return EXIT_ERROR
339343

340344

@@ -431,7 +435,7 @@ def handle_env_python_remove(args: Namespace) -> int:
431435
reporter.report_result()
432436
return EXIT_SUCCESS
433437
else:
434-
print(f"[ERROR] Failed to remove Python environment from: {env_name}")
438+
reporter.report_error(f"Failed to remove Python environment from '{env_name}'")
435439
return EXIT_ERROR
436440

437441

@@ -455,7 +459,8 @@ def handle_env_python_shell(args: Namespace) -> int:
455459
return EXIT_SUCCESS
456460
else:
457461
env_name = hatch_env or env_manager.get_current_environment()
458-
print(f"Failed to launch Python shell for: {env_name}")
462+
reporter = ResultReporter("hatch env python shell")
463+
reporter.report_error(f"Failed to launch Python shell for '{env_name}'")
459464
return EXIT_ERROR
460465

461466

@@ -490,7 +495,7 @@ def handle_env_python_add_hatch_mcp(args: Namespace) -> int:
490495
reporter.report_result()
491496
return EXIT_SUCCESS
492497
else:
493-
print(f"[ERROR] Failed to install hatch_mcp_server wrapper in environment: {env_name}")
498+
reporter.report_error(f"Failed to install hatch_mcp_server wrapper in environment '{env_name}'")
494499
return EXIT_ERROR
495500

496501

@@ -621,15 +626,23 @@ def handle_env_list_hosts(args: Namespace) -> int:
621626
try:
622627
env_re = re.compile(env_pattern)
623628
except re.error as e:
624-
print(f"[ERROR] Invalid env regex pattern: {e}")
629+
format_validation_error(ValidationError(
630+
f"Invalid env regex pattern: {e}",
631+
field="--env",
632+
suggestion="Use a valid Python regex pattern"
633+
))
625634
return EXIT_ERROR
626635

627636
server_re = None
628637
if server_pattern:
629638
try:
630639
server_re = re.compile(server_pattern)
631640
except re.error as e:
632-
print(f"[ERROR] Invalid server regex pattern: {e}")
641+
format_validation_error(ValidationError(
642+
f"Invalid server regex pattern: {e}",
643+
field="--server",
644+
suggestion="Use a valid Python regex pattern"
645+
))
633646
return EXIT_ERROR
634647

635648
# Get all environments
@@ -741,7 +754,11 @@ def handle_env_list_servers(args: Namespace) -> int:
741754
try:
742755
env_re = re.compile(env_pattern)
743756
except re.error as e:
744-
print(f"[ERROR] Invalid env regex pattern: {e}")
757+
format_validation_error(ValidationError(
758+
f"Invalid env regex pattern: {e}",
759+
field="--env",
760+
suggestion="Use a valid Python regex pattern"
761+
))
745762
return EXIT_ERROR
746763

747764
# Special handling for '-' (undeployed filter)
@@ -751,7 +768,11 @@ def handle_env_list_servers(args: Namespace) -> int:
751768
try:
752769
host_re = re.compile(host_pattern)
753770
except re.error as e:
754-
print(f"[ERROR] Invalid host regex pattern: {e}")
771+
format_validation_error(ValidationError(
772+
f"Invalid host regex pattern: {e}",
773+
field="--host",
774+
suggestion="Use a valid Python regex pattern"
775+
))
755776
return EXIT_ERROR
756777

757778
# Get all environments

0 commit comments

Comments
 (0)