diff --git a/swh/scanner/exceptions.py b/swh/scanner/exceptions.py --- a/swh/scanner/exceptions.py +++ b/swh/scanner/exceptions.py @@ -6,4 +6,16 @@ class APIError(Exception): def __str__(self): - return 'API Error: "%s"' % self.args + return '"%s"' % self.args + + +status_code_msg = { + 404: 'Resource not found', + 413: 'Payload Too Large', + 429: 'Too many request', +} + + +def error_response(status_code: int, api_url: str): + error_msg = f'{status_code} {status_code_msg[status_code]}: \'{api_url}\'' + raise APIError(error_msg) diff --git a/swh/scanner/scanner.py b/swh/scanner/scanner.py --- a/swh/scanner/scanner.py +++ b/swh/scanner/scanner.py @@ -10,7 +10,7 @@ from typing import List, Dict, Tuple, Iterator from pathlib import PosixPath -from .exceptions import APIError +from .exceptions import error_response from .model import Tree from swh.model.cli import pid_of_file, pid_of_dir @@ -49,9 +49,7 @@ async def make_request(pids): async with session.post(endpoint, json=pids) as resp: if resp.status != 200: - error_message = '%s with given values %s' % ( - resp.text, str(pids)) - raise APIError(error_message) + error_response(resp.status, endpoint) return await resp.json()