From a8978096a393294b5c508432280fa7b12b4ebada Mon Sep 17 00:00:00 2001 From: A-Baji Date: Fri, 29 Sep 2023 16:39:48 -0500 Subject: [PATCH 1/2] throw appropriate errors for 404 --- pharus/error.py | 12 ++++++++++++ pharus/interface.py | 32 ++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/pharus/error.py b/pharus/error.py index a5b1c25b..6fcc6cd5 100644 --- a/pharus/error.py +++ b/pharus/error.py @@ -11,3 +11,15 @@ class InvalidRestriction(Exception): """Exception raised when restrictions result in no records when expected at least one.""" pass + + +class SchemaNotFound(Exception): + """Exception raised when a given schema is not found to exist""" + + pass + + +class TableNotFound(Exception): + """Exception raised when a given table is not found to exist""" + + pass diff --git a/pharus/interface.py b/pharus/interface.py index a2c3f289..a91aef4f 100644 --- a/pharus/interface.py +++ b/pharus/interface.py @@ -1,12 +1,18 @@ """Library for interfaces into DataJoint pipelines.""" import datajoint as dj +from datajoint import DataJointError from datajoint.utils import to_camel_case from datajoint.user_tables import UserTable from datajoint import VirtualModule import datetime import numpy as np import re -from .error import InvalidRestriction, UnsupportedTableType +from .error import ( + InvalidRestriction, + UnsupportedTableType, + SchemaNotFound, + TableNotFound, +) DAY = 24 * 60 * 60 DEFAULT_FETCH_LIMIT = 1000 # Stop gap measure to deal with super large tables @@ -60,9 +66,11 @@ def _list_tables( """ # Get list of tables names - tables_name = dj.Schema( - schema_name, create_schema=False, connection=connection - ).list_tables() + try: + schema = dj.Schema(schema_name, create_schema=False, connection=connection) + except DataJointError: + raise SchemaNotFound("Schema does not exist") + tables_name = schema.list_tables() # Dict to store list of table name for each type tables_dict_list = dict(manual=[], lookup=[], computed=[], imported=[], part=[]) # Loop through each table name to figure out what type it is and add them to @@ -452,12 +460,16 @@ def _get_table_object( # Split the table name by '.' for dealing with part tables table_name_parts = table_name.split(".") - if len(table_name_parts) == 2: - return getattr( - getattr(schema_virtual_module, table_name_parts[0]), table_name_parts[1] - ) - else: - return getattr(schema_virtual_module, table_name_parts[0]) + try: + if len(table_name_parts) == 2: + return getattr( + getattr(schema_virtual_module, table_name_parts[0]), + table_name_parts[1], + ) + else: + return getattr(schema_virtual_module, table_name_parts[0]) + except AttributeError: + raise TableNotFound("Table does not exist") @staticmethod def _filter_to_restriction(attribute_filter: dict, attribute_type: str) -> str: From b0bfc6ff2146f45985330aeff8bd54a532832b3c Mon Sep 17 00:00:00 2001 From: A-Baji Date: Fri, 29 Sep 2023 16:49:41 -0500 Subject: [PATCH 2/2] docs: :memo: changelog --- CHANGELOG.md | 7 +++++++ pharus/server.py | 2 +- pharus/version.py | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d4a706c3..0795a0f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ Observes [Semantic Versioning](https://semver.org/spec/v2.0.0.html) standard and [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) convention. +## [0.8.9] - 2023-10-02 + +### Added + +- Throw new errors for invalid schemas and tables [#168](https://github.com/datajoint/pharus/pull/168) + ## [0.8.8] - 2023-09-20 ### Changed @@ -321,6 +327,7 @@ Observes [Semantic Versioning](https://semver.org/spec/v2.0.0.html) standard and - Support for DataJoint attribute types: `varchar`, `int`, `float`, `datetime`, `date`, `time`, `decimal`, `uuid`. - Check dependency utility to determine child table references. +[0.8.9]: https://github.com/datajoint/pharus/compare/0.8.8...0.8.9 [0.8.8]: https://github.com/datajoint/pharus/compare/0.8.7...0.8.8 [0.8.7]: https://github.com/datajoint/pharus/compare/0.8.6...0.8.7 [0.8.6]: https://github.com/datajoint/pharus/compare/0.8.5...0.8.6 diff --git a/pharus/server.py b/pharus/server.py index 53a40f7d..975f22fd 100644 --- a/pharus/server.py +++ b/pharus/server.py @@ -127,7 +127,7 @@ def api_version() -> str: Content-Type: application/json { - "version": "0.8.8" + "version": "0.8.9" } ``` diff --git a/pharus/version.py b/pharus/version.py index 23bfa5b5..429387fe 100644 --- a/pharus/version.py +++ b/pharus/version.py @@ -1,2 +1,2 @@ """Package metadata.""" -__version__ = "0.8.8" +__version__ = "0.8.9"