Skip to content

Commit aae099f

Browse files
committed
Refactored _parse.py even more
1 parent 9690678 commit aae099f

5 files changed

Lines changed: 349 additions & 293 deletions

File tree

exasol/error/_error.py

Lines changed: 9 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
Union,
1313
)
1414

15+
from exasol.error._internal_errors import LIBRARY_ERRORS, INVALID_ERROR_CODE, UNKNOWN_EXCEPTION_OCCURED
16+
1517
with warnings.catch_warnings():
1618
warnings.simplefilter("ignore")
1719
from exasol_error_reporting_python import exa_error
@@ -100,66 +102,6 @@ def _long(error: Error):
100102
return output[:-1]
101103

102104

103-
@dataclass(frozen=True)
104-
class _ExaStaticError:
105-
identifier: str
106-
message: str
107-
messagePlaceholders: List[Dict[str, str]]
108-
description: Optional[str]
109-
mitigations: List[str]
110-
sourceFile: str
111-
112-
def to_dict(self) -> Dict[str, Any]:
113-
ret_val = dataclasses.asdict(self)
114-
if self.description is None:
115-
del ret_val["description"]
116-
return ret_val
117-
118-
119-
# ATTENTION: In the event of an exception while creating an error, we encounter a chicken-and-egg problem regarding error definitions.
120-
# Therefore, errors created by this library must be defined "statically" within this file.
121-
# Details should then be used to create a low-level error. The information should also be used to create/update the error-codes.json
122-
# as part of the release preparation. This is only necessary for this library, as it is the root, other libraries should use
123-
# the `ec` command-line tool to update and create their project specifc error-codes.json file.
124-
LIBRARY_ERRORS = {
125-
"E-ERP-1": _ExaStaticError(
126-
identifier="E-ERP-1",
127-
message="Invalid error code {{code}}.",
128-
messagePlaceholders=[
129-
{
130-
"placeholder": "code",
131-
"description": "Error code which was causing the error.",
132-
}
133-
],
134-
description=None,
135-
mitigations=["Ensure you follow the standard error code format."],
136-
sourceFile=Path(__file__).name,
137-
),
138-
"E-ERP-2": _ExaStaticError(
139-
identifier="E-ERP-2",
140-
message="Unknown error/exception occurred.",
141-
messagePlaceholders=[
142-
{
143-
"placeholder": "traceback",
144-
"description": "Exception traceback which lead to the generation of this error.",
145-
}
146-
],
147-
description="An unexpected error occurred during the creation of the error",
148-
mitigations=[
149-
cleandoc(
150-
"""
151-
A good starting point would be to investigate the cause of the attached exception.
152-
153-
Trackback:
154-
{{traceback}}
155-
"""
156-
)
157-
],
158-
sourceFile=Path(__file__).name,
159-
),
160-
}
161-
162-
163105
def ExaError(
164106
code: str,
165107
message: str,
@@ -183,25 +125,21 @@ def ExaError(
183125
try:
184126
return Error(code, message, mitigations, parameters)
185127
except InvalidErrorCode:
186-
error_code = "E-ERP-1"
187-
error_details = LIBRARY_ERRORS[error_code]
188128
return Error(
189-
code=error_details.identifier,
190-
message=error_details.message,
191-
mitigations=error_details.mitigations,
129+
code=INVALID_ERROR_CODE.identifier,
130+
message=INVALID_ERROR_CODE.message,
131+
mitigations=INVALID_ERROR_CODE.mitigations,
192132
parameters={"code": code},
193133
)
194134
except Exception as ex:
195135
import traceback
196136

197137
tb = traceback.format_exc()
198138
parameters = {"traceback": tb}
199-
error_code = "E-ERP-2"
200-
error_details = LIBRARY_ERRORS[error_code]
201139
return Error(
202-
code=error_details.identifier,
203-
message=error_details.message,
204-
mitigations=error_details.mitigations,
140+
code=UNKNOWN_EXCEPTION_OCCURED.identifier,
141+
message=UNKNOWN_EXCEPTION_OCCURED.message,
142+
mitigations=UNKNOWN_EXCEPTION_OCCURED.mitigations,
205143
parameters=parameters,
206144
)
207145

@@ -214,7 +152,7 @@ def _create_error_code_definitions(version=None) -> Dict[str, Any]:
214152
"$schema": "https://schemas.exasol.com/error_code_report-1.0.0.json",
215153
"projectName": "exasol-error-reporting",
216154
"projectVersion": version,
217-
"errorCodes": [code.to_dict() for code in LIBRARY_ERRORS.values()],
155+
"errorCodes": [code.to_dict() for code in LIBRARY_ERRORS],
218156
}
219157

220158

exasol/error/_internal_errors.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import dataclasses
2+
from dataclasses import dataclass
3+
from inspect import cleandoc
4+
from pathlib import Path
5+
from typing import List, Dict, Optional, Any
6+
7+
8+
@dataclass(frozen=True)
9+
class _ExaStaticError:
10+
identifier: str
11+
message: str
12+
messagePlaceholders: List[Dict[str, str]]
13+
description: Optional[str]
14+
mitigations: List[str]
15+
sourceFile: str
16+
17+
def to_dict(self) -> Dict[str, Any]:
18+
ret_val = dataclasses.asdict(self)
19+
if self.description is None:
20+
del ret_val["description"]
21+
return ret_val
22+
23+
24+
# ATTENTION: In the event of an exception while creating an error, we encounter a chicken-and-egg problem regarding error definitions.
25+
# Therefore, errors created by this library must be defined "statically" within this file.
26+
# Details should then be used to create a low-level error. The information should also be used to create/update the error-codes.json
27+
# as part of the release preparation. This is only necessary for this library, as it is the root, other libraries should use
28+
# the `ec` command-line tool to update and create their project specifc error-codes.json file.
29+
INVALID_ERROR_CODE = _ExaStaticError(
30+
identifier="E-ERP-1",
31+
message="Invalid error code {{code}}.",
32+
messagePlaceholders=[
33+
{
34+
"placeholder": "code",
35+
"description": "Error code which was causing the error.",
36+
}
37+
],
38+
description=None,
39+
mitigations=["Ensure you follow the standard error code format."],
40+
sourceFile=Path(__file__).name,
41+
)
42+
43+
UNKNOWN_EXCEPTION_OCCURED = _ExaStaticError(
44+
identifier="E-ERP-2",
45+
message="Unknown error/exception occurred.",
46+
messagePlaceholders=[
47+
{
48+
"placeholder": "traceback",
49+
"description": "Exception traceback which lead to the generation of this error.",
50+
}
51+
],
52+
description="An unexpected error occurred during the creation of the error",
53+
mitigations=[
54+
cleandoc(
55+
"""
56+
A good starting point would be to investigate the cause of the attached exception.
57+
58+
Trackback:
59+
{{traceback}}
60+
"""
61+
)
62+
],
63+
sourceFile=Path(__file__).name,
64+
)
65+
66+
INVALID_ERROR_CODE_DEFINITION = _ExaStaticError(
67+
identifier="E-ERP-3",
68+
message="Invalid error code definition: {{error_element}} only can contain constant values, but is of type: {{defined_type}}. In file {{file}} line {{line}}",
69+
messagePlaceholders=[
70+
{
71+
"error_element": "The element within the error definition which caused the error.",
72+
"defined_type": "The actual Python type of the error definition.",
73+
"file": "The file in which the error occurred.",
74+
"line": "The line where the error occurred.",
75+
}
76+
],
77+
description="An unexpected error occurred during the creation of the error catalog, when parsing the project for EXA error codes.",
78+
mitigations=[
79+
cleandoc(
80+
"""
81+
Check the definition of ExaError. Possible errors:
82+
1. Usage of none-constant expression in error code, message
83+
2. Mitigations are not a list, but another container
84+
3. Invalid definition of parameters.
85+
86+
"""
87+
)
88+
],
89+
sourceFile=Path(__file__).name,
90+
)
91+
92+
INTERNAL_ERROR_WHEN_CREATING_ERROR_CATALOG = _ExaStaticError(
93+
identifier="E-ERP-4",
94+
message="Internal error during creation of the error catalog. Details to append on Bug ticket: {{traceback}}",
95+
messagePlaceholders=[
96+
{
97+
"traceback": "Traceback which was collected when creating this error.",
98+
}
99+
],
100+
description="This error signals a programming error in the exasol.error library.",
101+
mitigations=[
102+
cleandoc(
103+
"""
104+
Open a bug ticket at https://github.com/exasol/error-reporting-python/issues/new?template=bug.md
105+
"""
106+
)
107+
],
108+
sourceFile=Path(__file__).name,
109+
),
110+
111+
LIBRARY_ERRORS = (INVALID_ERROR_CODE, UNKNOWN_EXCEPTION_OCCURED, INVALID_ERROR_CODE_DEFINITION, INTERNAL_ERROR_WHEN_CREATING_ERROR_CATALOG)

0 commit comments

Comments
 (0)