Implementing error handling for #50. So that the errors do not come at the very end, but in the beginning right after the user inputted something (quicker feedback to the user).

This commit is contained in:
Anton Hvornum 2020-10-18 09:58:33 +02:00
parent 60aaae4337
commit 1a10fe3f51
1 changed files with 17 additions and 1 deletions

View File

@ -1,5 +1,6 @@
import urllib.request, urllib.parse
import ssl, json
from .lib.exceptions import *
BASE_URL = 'https://www.archlinux.org/packages/search/json/?name={package}'
@ -24,4 +25,19 @@ def find_packages(*names):
result = {}
for package in names:
result[package] = find_package(package)
return result
return result
def validate_package_list(packages :list):
"""
Validates a list of given packages.
Raises `RequirementError` if one or more packages are not found.
"""
invalid_packages = []
for package in packages:
if not find_package(package)['results']:
invalid_packages.append(package)
if invalid_packages:
raise RequirementError(f"Invalid package names: {invalid_packages}")
return True