*** Wartungsfenster jeden ersten Mittwoch vormittag im Monat ***

Skip to content
Snippets Groups Projects
Commit 118bbb7d authored by Moser, Maximilian's avatar Moser, Maximilian
Browse files

Make error handling more specific

parent 81a73693
Branches
Tags
1 merge request!2Modernization
...@@ -17,6 +17,7 @@ import click ...@@ -17,6 +17,7 @@ import click
from flask.cli import with_appcontext from flask.cli import with_appcontext
from invenio_access.permissions import system_identity from invenio_access.permissions import system_identity
from invenio_db import db from invenio_db import db
from invenio_records_resources.services.errors import PermissionDeniedError
from ..utils import get_identity_for_user, get_record_service, get_user_by_identifier from ..utils import get_identity_for_user, get_record_service, get_user_by_identifier
from .options import ( from .options import (
...@@ -67,7 +68,7 @@ def list_drafts(user): ...@@ -67,7 +68,7 @@ def list_drafts(user):
num_files = "no" num_files = "no"
click.secho(f"{recid}\t{num_files} files\t{title}", fg="green") click.secho(f"{recid}\t{num_files} files\t{title}", fg="green")
except: except PermissionDeniedError:
pass pass
...@@ -111,7 +112,7 @@ def create_draft(metadata_path, publish, user, owners, vanity_pid): ...@@ -111,7 +112,7 @@ def create_draft(metadata_path, publish, user, owners, vanity_pid):
metadata_file_path = join(metadata_path, "metadata.json") metadata_file_path = join(metadata_path, "metadata.json")
deposit_files_path = join(metadata_path, "files") deposit_files_path = join(metadata_path, "files")
if not isfile(metadata_file_path): if not isfile(metadata_file_path):
raise Exception("metadata file does not exist: %s" % metadata_file_path) raise FileNotFoundError(metadata_file_path)
metadata = read_metadata(metadata_file_path) metadata = read_metadata(metadata_file_path)
draft = create_record_from_metadata(metadata, identity) draft = create_record_from_metadata(metadata, identity)
...@@ -143,7 +144,7 @@ def create_draft(metadata_path, publish, user, owners, vanity_pid): ...@@ -143,7 +144,7 @@ def create_draft(metadata_path, publish, user, owners, vanity_pid):
file_service.commit_file(id_=recid, file_key=fn, identity=identity) file_service.commit_file(id_=recid, file_key=fn, identity=identity)
else: else:
raise Exception("neither a file nor a directory: %s" % metadata_path) raise TypeError(f"neither a file nor a directory: {metadata_path}")
if owners: if owners:
owners = [get_user_by_identifier(owner) for owner in owners] owners = [get_user_by_identifier(owner) for owner in owners]
......
...@@ -142,8 +142,11 @@ def hard_delete_files(pid, pid_type): ...@@ -142,8 +142,11 @@ def hard_delete_files(pid, pid_type):
fi.delete() fi.delete()
storage.delete() storage.delete()
click.secho("{}\t{}".format(key, fi.uri), fg="red") click.secho("{}\t{}".format(key, fi.uri), fg="red")
except:
click.secho("cannot delete file: %s" % fi.uri, fg="yellow") except Exception as error:
click.secho(
f"cannot delete file '{fi.uri}': {error}", fg="yellow", err=True
)
db.session.commit() db.session.commit()
......
...@@ -14,6 +14,7 @@ import sys ...@@ -14,6 +14,7 @@ import sys
import click import click
from flask.cli import with_appcontext from flask.cli import with_appcontext
from invenio_db import db from invenio_db import db
from invenio_records_resources.services.errors import PermissionDeniedError
from ..utils import get_identity_for_user, get_record_service, get_user_by_identifier from ..utils import get_identity_for_user, get_record_service, get_user_by_identifier
from .options import ( from .options import (
...@@ -60,7 +61,7 @@ def list_records(user): ...@@ -60,7 +61,7 @@ def list_records(user):
num_files = "no" num_files = "no"
click.secho(f"{recid}\t{num_files} files\t{title}", fg="green") click.secho(f"{recid}\t{num_files} files\t{title}", fg="green")
except: except PermissionDeniedError:
pass pass
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
import json import json
from invenio_db import db from invenio_db import db
from invenio_pidstore.errors import PIDAlreadyExists
from invenio_pidstore.models import PersistentIdentifier from invenio_pidstore.models import PersistentIdentifier
from ..utils import get_record_service from ..utils import get_record_service
...@@ -23,7 +24,7 @@ def read_metadata(metadata_file_path): ...@@ -23,7 +24,7 @@ def read_metadata(metadata_file_path):
metadata = json.load(metadata_file) metadata = json.load(metadata_file)
if metadata is None: if metadata is None:
raise Exception("not a valid json file: %s" % metadata_file_path) raise TypeError(f"not a valid json file: {metadata_file_path}")
return metadata return metadata
...@@ -41,9 +42,7 @@ def create_record_from_metadata( ...@@ -41,9 +42,7 @@ def create_record_from_metadata(
).count() ).count()
if count > 0: if count > 0:
raise Exception( raise PIDAlreadyExists(pid_type=vanity_pid_type, pid_value=vanity_pid)
"PID '{}:{}' is already taken".format(vanity_pid_type, vanity_pid)
)
draft = service.create(identity=identity, data=metadata)._record draft = service.create(identity=identity, data=metadata)._record
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment