Skip to content

Commit 3fb2b05

Browse files
committed
add info about response and error objects
1 parent 8634d50 commit 3fb2b05

File tree

2 files changed

+66
-15
lines changed

2 files changed

+66
-15
lines changed

docs/docs/api/rpc.md

+39
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,42 @@ To use an RPC function:
4545
- add_from_known_connection
4646
- add_from_scratch
4747
- DBModelReturn
48+
49+
## Responses
50+
51+
### Success
52+
53+
Upon a successful call to an RPC function, the API will return a success object. Such an object has the following form:
54+
55+
```json
56+
{
57+
"jsonrpc": "2.0",
58+
"id": 234,
59+
"result": <any>
60+
}
61+
```
62+
63+
The `result` is whatever was returned by the underlying function.
64+
65+
### Errors
66+
67+
When an error is produced by a call to the RPC endpoint, we produce an error of the following form:
68+
69+
```json
70+
{
71+
"jsonrpc": "2.0",
72+
"id": 234,
73+
"error": {
74+
"code": <int>,
75+
"message": <str>
76+
}
77+
}
78+
```
79+
80+
The `code` is a negative integer. Some codes are produced according to the [JSON-RPC spec](https://www.jsonrpc.org/specification#error_object).
81+
82+
More specific codes are produced according to the following documentation:
83+
84+
---
85+
86+
::: mathesar.rpc.exceptions.error_codes.get_error_code

mathesar/rpc/exceptions/error_codes.py

+27-15
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,40 @@
33
all exceptions that Mathesar could possibly throw.
44
55
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.
87
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!
219
"""
2210
from frozendict import frozendict
2311

2412
UNKNOWN_KEY = "UNKNOWN"
2513

2614

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+
"""
2840
err_module = err.__class__.__module__
2941
err_name = err.__class__.__name__
3042
if err_module.startswith("builtin"):

0 commit comments

Comments
 (0)