From 82a9e3a23a23f1287116226787edaa44001256e7 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 27 Jul 2021 11:40:24 +0200 Subject: [PATCH] Improved index out of range exception --- src/cpl_query/_query/first.py | 4 ++-- src/cpl_query/exceptions.py | 8 +++++++- src/cpl_query/tests/query_test.py | 3 +++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/cpl_query/_query/first.py b/src/cpl_query/_query/first.py index 6f3859ae..0eb98924 100644 --- a/src/cpl_query/_query/first.py +++ b/src/cpl_query/_query/first.py @@ -1,6 +1,6 @@ from typing import Optional -from cpl_query.exceptions import ArgumentNoneException, ExceptionArgument +from cpl_query.exceptions import ArgumentNoneException, ExceptionArgument, IndexOutOfRangeException from cpl_query.extension.iterable_abc import IterableABC @@ -9,7 +9,7 @@ def first_query(_list: IterableABC) -> any: raise ArgumentNoneException(ExceptionArgument.list) if len(_list) == 0: - raise Exception('Index out of range') + raise IndexOutOfRangeException() return _list[0] diff --git a/src/cpl_query/exceptions.py b/src/cpl_query/exceptions.py index 1b6e550d..7fc33909 100644 --- a/src/cpl_query/exceptions.py +++ b/src/cpl_query/exceptions.py @@ -14,7 +14,13 @@ class ExceptionArgument(Enum): class ArgumentNoneException(Exception): def __init__(self, arg: ExceptionArgument): - Exception.__init__(self, f'Argument {arg} is None') + Exception.__init__(self, f'argument {arg} is None') + + +class IndexOutOfRangeException(Exception): + + def __init__(self): + Exception.__init__(self, f'List index out of range') class InvalidTypeException(Exception): diff --git a/src/cpl_query/tests/query_test.py b/src/cpl_query/tests/query_test.py index 2481f60e..0f6b3e17 100644 --- a/src/cpl_query/tests/query_test.py +++ b/src/cpl_query/tests/query_test.py @@ -100,6 +100,9 @@ class QueryTest(unittest.TestCase): self.assertEqual(self._tests[index], self._tests.element_at_or_default(index)) self.assertIsNone(self._tests.element_at_or_default(len(self._tests))) + test = [1, 2, 3] + t = test[100] + def test_first(self): results = [] for user in self._tests: