Skip to content

Commit

Permalink
Exception handling, user name as flag
Browse files Browse the repository at this point in the history
  • Loading branch information
DraTeots committed Apr 20, 2024
1 parent 5becbee commit 5c70e33
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 22 deletions.
5 changes: 5 additions & 0 deletions python/ccdb/cmd/cli_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ def process(self, args, start_index=1):
self.context.connection_string = workargs[i]
i += 1

# connection string
if (token == "-u" or token == "--user") and (i < len(workargs)):
self.context.user_name = workargs[i]
i += 1

# it is an interactive mode
elif token == "-I" or token == "-i" or token == "--interactive":
self.context.is_interactive = True
Expand Down
1 change: 0 additions & 1 deletion python/ccdb/cmd/commands/cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from ccdb import TypeTable, Assignment
from ccdb import AlchemyProvider
from ccdb.cmd import CliCommandBase, UtilityArgumentParser
from ccdb.errors import MissingArgumentError
from ccdb.path_utils import ParseRequestResult, parse_request
from ccdb import BraceMessage as Lfm # lfm is aka log format message. See BraceMessage desc about
from sqlalchemy.orm.exc import NoResultFound
Expand Down
32 changes: 14 additions & 18 deletions python/ccdb/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class AuthVerificationError(Exception):
"""
Exception raised when wrong pair login,password is obtained by authentication
"""
pass
def __init__(self, message="Wrong login and password (if passwords are used) are obtained by authentication"):
Exception.__init__(self, message)


class ObjectIsNotFoundInDbError(Exception):
Expand All @@ -16,46 +17,41 @@ def __init__(self, db_obj_type, message="", **kwargs):
Exception.__init__(self, message)



class DatabaseStructureError(Exception):
"""
Exception raised if No database structure found.
Possibly because of connecting to wrong SQLite file or to MySQL database without schema.
"""

pass
def __init__(self):
message = ("No database structure found. "
"Possibly because of connecting to wrong SQLite file or to MySQL database without schema")
Exception.__init__(self, message)


class UserNotFoundError(Exception):
""" Exception raised if user not found in the database"""

def __init__(self, message="", username=""):
self.username = username
if not message:
message = f"Username '{username}' is not found"
Exception.__init__(self, message)


class UserExistsError(Exception):
def __init__(self, message="", username=""):
self.message = message,
self.username = username
if not message:
message = f"Username '{username}' already exists"
Exception.__init__(self, message, username)


class AnonymousUserForbiddenError(Exception):
""" Exception raised when an Anonymous user is trying to perform actions that they are forbidden from doing."""
def __init__(self, message="This action is forbidden for an Anonymous user."):
self.message = message


class AllowDefaultsError(Exception):
"""Exception when allow_defaults is not set to true but other fields are"""
pass


class MissingArgumentError(Exception):
"""Exception raised if allow_defaults is true and an arguement is missing"""
pass


class MissingVariation(Exception):
"""Exception raised Variation is not specified"""
pass
def __init__(self, message="This action is forbidden for an Anonymous user."):
Exception.__init__(self, message)

6 changes: 3 additions & 3 deletions python/ccdb/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from sqlalchemy.sql.expression import desc
from .model import Directory, TypeTable, TypeTableColumn, ConstantSet, Assignment, RunRange, Variation, User, LogRecord
from .errors import ObjectIsNotFoundInDbError, AnonymousUserForbiddenError, DatabaseStructureError, UserNotFoundError, \
UserExistsError, MissingArgumentError, AllowDefaultsError
UserExistsError

from .table_file import TextFileDOM
from .authentication import Authentication
Expand Down Expand Up @@ -1156,9 +1156,9 @@ def get_assignment_by_request(self, request,
request = parse_request(request)
assert isinstance(request, ParseRequestResult)
if not allow_defaults and (default_variation or default_run or default_time):
raise AllowDefaultsError
raise ValueError("allow_defaults is not set to true but other fields are")
if allow_defaults and not (default_time or default_run or default_variation):
raise MissingArgumentError
raise ValueError("allow_defaults is true and default_time or default_run or default_variation is missing")
if default_variation == '' and request.variation == '':
request.variation = "default"

Expand Down
30 changes: 30 additions & 0 deletions python/tests/unit_test_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import unittest

from ccdb.errors import *


class PathUtilsTest(unittest.TestCase):

def test_representations(self):
msg = str(AuthVerificationError())
self.assertTrue(msg)
msg = str(ObjectIsNotFoundInDbError(int, "my-message"))
self.assertTrue(msg)
msg = str(DatabaseStructureError())
self.assertTrue(msg)
msg = str(UserNotFoundError("User-Name"))
self.assertTrue(msg)
msg = str(UserExistsError("", "User-Name"))
self.assertTrue(msg)
msg = str(AnonymousUserForbiddenError())
self.assertTrue(msg)

# AuthVerificationError()
# ObjectIsNotFoundInDbError()
# DatabaseStructureError()
# UserNotFoundError()
# UserExistsError(self, message="", username="")
# AnonymousUserForbiddenError()
# AllowDefaultsError()
# MissingArgumentError()
# MissingVariation()

0 comments on commit 5c70e33

Please sign in to comment.