|
3 | 3 | all exceptions that Mathesar could possibly throw.
|
4 | 4 |
|
5 | 5 | The purpose is to let us send a code with an error response in case an
|
6 |
| -RPC function call fails. The codes are organized by the underlying |
7 |
| -code section that could throw the exception: |
| 6 | +RPC function call fails. |
8 | 7 |
|
9 |
| -- builtins: -31xxx |
10 |
| -- psycopg or psycopg2: -30xxx |
11 |
| -- django: -29xxx |
12 |
| -- mathesar (our code): -28xxx |
13 |
| -- db (our code): -27xxx |
14 |
| -- SQLAlchemy: -26xxx |
15 |
| -- other: -25xxx |
16 |
| -
|
17 |
| -Unknown errors return a "round number" code, so an unknown builtin error |
18 |
| -gets the code 31000. |
19 |
| -
|
20 |
| -THESE ENUMs ARE INITIALLY AUTO-GENERATED! |
| 8 | +THESE CODES WERE INITIALLY AUTO-GENERATED! |
21 | 9 | """
|
22 | 10 | from frozendict import frozendict
|
23 | 11 |
|
24 | 12 | UNKNOWN_KEY = "UNKNOWN"
|
25 | 13 |
|
26 | 14 |
|
27 |
| -def get_error_code(err): |
| 15 | +def get_error_code(err: Exception) -> int: |
| 16 | + """ |
| 17 | + Return an error code, given an exception. |
| 18 | +
|
| 19 | + The code produced will align with expectations of a JSON-RPC |
| 20 | + endpoint. Additionally, the codes are organized by the underlying |
| 21 | + section of code that could throw the exception: |
| 22 | +
|
| 23 | + - builtins: -31xxx |
| 24 | + - psycopg or psycopg2: -30xxx |
| 25 | + - django: -29xxx |
| 26 | + - mathesar (our code): -28xxx |
| 27 | + - db (our code): -27xxx |
| 28 | + - SQLAlchemy: -26xxx |
| 29 | + - other: -25xxx |
| 30 | +
|
| 31 | + Unknown errors return a "round number" code, so an unknown `builtins` |
| 32 | + error gets the code -31000. |
| 33 | +
|
| 34 | + Args: |
| 35 | + err: the Exception for which we need a code. |
| 36 | +
|
| 37 | + Returns: |
| 38 | + A negative integer code. |
| 39 | + """ |
28 | 40 | err_module = err.__class__.__module__
|
29 | 41 | err_name = err.__class__.__name__
|
30 | 42 | if err_module.startswith("builtin"):
|
|
0 commit comments