aboutsummaryrefslogtreecommitdiff
path: root/thcolor/errors.py
blob: 9d827b99eba972640a3d842ad1c432eb2d7718a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python3
#**************************************************************************
# Copyright (C) 2019-2021 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
# This file is part of the thcolor project, which is MIT-licensed.
#**************************************************************************
""" Exception definitions, with internal and external exceptions
	defined. """

__all__ = ["ColorExpressionDecodingError",
	"NotEnoughArgumentsError", "TooManyArgumentsError",
	"InvalidArgumentTypeError", "InvalidArgumentValueError"]

# ---
# External exceptions.
# ---

class ColorExpressionDecodingError(Exception):
	""" A color decoding error has occurred on the text. """

	def __init__(self, text, column=None, func=None):
		try:
			self._column = column
			assert self._column >= 0
		except AssertionError:
			self._column = None

		self._func = str(func) if func else None
		self._text = str(text) if text else ""

	def __str__(self):
		msg = ""

		if self._column is not None:
			msg += f"at column {self._column}"
			if self._func is not None:
				msg += ", "
		if self._func is not None:
			msg += f"for function {repr(self._func)}"
		if msg:
			msg += ": "

		return msg + self._text

	@property
	def text(self):
		""" Exception message, usually linked to the context. """

		return self._text

	@property
	def column(self):
		""" Column of the expression at which the exception has occurred,
			``None`` if the error has occurred on an unknown column or on
			the whole exception. """

		return self._column

	@property
	def func(self):
		""" Function name we were calling when the error has occurred,
			either on arguments decoding or erroneous argument type or
			value, ``None`` if the context is unknown or the error hasn't
			occurred while calling a function or decoding its
			arguments. """

		return self._func

# ---
# Internal exceptions.
# ---

class NotEnoughArgumentsError(Exception):
	""" Not enough arguments. """

	def __init__(self, count, name=None):
		self._name = name
		self._count = count

	def __str__(self):
		msg = "not enough arguments"
		if self._name is not None:
			msg += f" for function {repr(self._name)}"
		msg += f", expected {self._count} arguments at least"

		return msg

	@property
	def count(self):
		return self._count


class TooManyArgumentsError(Exception):
	""" Too many arguments. """

	def __init__(self, count, name=None):
		self._name = name
		self._count = count

	def __str__(self):
		msg = "too many arguments"
		if self._name is not None:
			msg += f" for function {repr(self._name)}"
		msg += f", expected {self._count} arguments at most"

		return msg

	@property
	def count(self):
		return self._count


class InvalidArgumentTypeError(Exception):
	""" Invalid argument type. """

	def __init__(self, index, expected, got, name=None):
		self._name = name
		self._index = index
		self._expected = expected
		self._got = got

	def __str__(self):
		msg = f"wrong type for argument {self._index + 1}"
		if self._name:
			msg += f" of function {repr(self._name)}"
		msg += f": expected {self._expected}, got {self._got}"

		return msg

	@property
	def index(self):
		return self._index

	@property
	def expected(self):
		return self._expected

	@property
	def got(self):
		return self._got


class InvalidArgumentValueError(Exception):
	""" Invalid argument value. """

	def __init__(self, index, text, name=None):
		self._name = name
		self._index = index
		self._text = text

	def __str__(self):
		msg = f"erroneous value for argument {self._index + 1}"
		if self._name:
			msg += f" of function {repr(self._name)}"
		msg += f": {self._text}"

		return msg

	@property
	def index(self):
		return self._index

	@property
	def text(self):
		return self._text


# End of file.