Skip to content

fix: Improve validation errors in Crawlee CLI #1140

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

Merged
merged 2 commits into from
Apr 7, 2025
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
40 changes: 27 additions & 13 deletions src/crawlee/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import importlib.resources
import json
import sys
from pathlib import Path
from typing import Annotated, Optional, cast

Expand Down Expand Up @@ -199,19 +200,32 @@ def create(
TextColumn('[progress.description]{task.description}'),
transient=True,
) as progress:
progress.add_task(description='Bootstrapping...', total=None)
cookiecutter(
template=str(template_directory),
no_input=True,
extra_context={
'project_name': project_name,
'package_manager': package_manager,
'crawler_type': crawler_type,
'http_client': http_client,
'enable_apify_integration': enable_apify_integration,
'start_url': start_url,
},
)
bootstrap_task = progress.add_task(description='Bootstrapping...', total=None)

try:
cookiecutter(
template=str(template_directory),
no_input=True,
extra_context={
'project_name': project_name,
'package_manager': package_manager,
'crawler_type': crawler_type,
'http_client': http_client,
'enable_apify_integration': enable_apify_integration,
'start_url': start_url,
},
)
except Exception as exc:
progress.update(bootstrap_task, visible=False)
progress.refresh()

# Print just the last line of the error message (the actual error without traceback)
if 'Hook script failed' in str(exc):
typer.echo('Project creation failed. Check the error message above.', err=True)
else:
typer.echo(f'Project creation failed: {exc!s}', err=True)

sys.exit(1)

typer.echo(f'Your project "{project_name}" was created.')

Expand Down
18 changes: 15 additions & 3 deletions src/crawlee/project_template/hooks/pre_gen_project.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# % if cookiecutter.package_manager in ['poetry', 'uv']
import subprocess
import shutil
import re
import sys

manager = "{{cookiecutter.package_manager}}"
manager_text = manager.title()
Expand All @@ -12,10 +14,20 @@
r_version = '0.x'
# % endif

# Check if package manager is available in PATH
if not shutil.which(manager):
sys.stderr.write(f'\nError: You selected {manager_text} as your package manager, but it is not installed. Please install it and try again.\n')
sys.exit(1)

# Check if the package manager is executable
try:
version = subprocess.check_output([manager, '--version']).decode().strip()
except OSError as exc:
raise RuntimeError(f'You chose to use the {manager_text} package manager, but it does not seem to be installed') from exc
except OSError:
sys.stderr.write(f'\nError: Your selected package manager {manager_text} was found but failed to execute.\n')
sys.exit(1)

# Check if the version matches the required regex
if not re.match(version_regex, version):
raise RuntimeError(f'{manager_text} {r_version} is required, but "{version}" is installed')
sys.stderr.write(f'\nError: Your selected package manager {manager_text} requires version {r_version}, but {version} is installed.\n')
sys.exit(1)
# % endif
Loading