Skip to content
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
19 changes: 19 additions & 0 deletions cds_dojson/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2026 CERN.
#
# CDS-Doson is free software; you can redistribute it and/or modify it under
# the terms of the MIT License; see LICENSE file for more details.

"""CDS-Dojson exceptions module."""


class ModelMissingException(Exception):
"""CDSDoJSONException class."""

description = "[Record did not match any available model]"


class MultipleModelsException(Exception):
"""CDSDoJSONException class."""

20 changes: 11 additions & 9 deletions cds_dojson/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
from invenio_query_parser.walkers.match_unit import MatchUnit
from invenio_query_parser.walkers.pypeg_to_ast import PypegConverter

from cds_dojson.exceptions import ModelMissingException, MultipleModelsException

# Cache of (name, model, parsed_query_ast) tuples keyed by entry_point_group.
# Entry points and their query ASTs are static for the lifetime of a process,
# so scanning importlib_metadata and re-parsing pypeg2 queries on every call
Expand Down Expand Up @@ -87,20 +89,20 @@ def matcher(record, entry_point_group):
name, model
))
_matches.append([name, model])
try:
if len(_matches) > 1:
logger.error(
("Found more than one matches `{0}`, we'll use {1}"
" for record {2}.").format(
_matches, default, record
)
)
return default
return _matches[0][1]
except IndexError:
logger.warning(
"Model *not* found, fallback to default {0} for record {1}".format(
default, record
raise MultipleModelsException(
"Found more than one models {0} for record {1}".format(
_matches, record))
if len(_matches) == 0:
logger.warning(
"Model *not* found, fallback to default {0} for record {1}".format(
default, record
)
)
)
return default
raise ModelMissingException("Model *not* found")
Loading