odoo_api/jsonrpc/response.rs
1//! JSON-RPC Responses
2
3use super::{JsonRpcId, JsonRpcVersion};
4use serde::{Deserialize, Serialize};
5use serde_json::{Map, Value};
6use std::fmt::Debug;
7
8/// An Odoo JSON-RPC API response
9///
10/// This struct represents the base JSON data, and is paramterized over the
11/// *request* [`OdooApiMethod`](super::OdooApiMethod). The deserialization struct is chosen by
12/// looking at the associated type [`OdooApiMethod::Response`](super::OdooApiMethod).
13///
14/// See: [odoo/http.py](https://github.com/odoo/odoo/blob/b6e195ccb3a6c37b0d980af159e546bdc67b1e42/odoo/http.py#L1805-L1841)
15#[derive(Debug, Serialize, Deserialize)]
16#[serde(untagged)]
17pub enum JsonRpcResponse<T>
18where
19 T: Debug,
20{
21 Success(JsonRpcResponseSuccess<T>),
22 Error(JsonRpcResponseError),
23}
24
25/// A successful Odoo API response
26#[derive(Debug, Serialize, Deserialize)]
27pub struct JsonRpcResponseSuccess<T>
28where
29 //TODO: should we have something else here?
30 T: Debug,
31{
32 /// The JSON-RPC version (`2.0`)
33 pub(crate) jsonrpc: JsonRpcVersion,
34
35 /// The request id
36 ///
37 /// This is not used for any stateful behaviour on the Odoo/Python side
38 pub(crate) id: JsonRpcId,
39
40 /// The response data, parameterized on the *request* [`OdooApiMethod::Response`](super::OdooApiMethod)
41 /// associated type.
42 pub(crate) result: T,
43}
44
45/// A failed Odoo API response
46#[derive(Debug, Serialize, Deserialize)]
47pub struct JsonRpcResponseError {
48 /// The JSON-RPC version (`2.0`)
49 pub(crate) jsonrpc: JsonRpcVersion,
50
51 /// The request id
52 ///
53 /// This is not used for any stateful behaviour on the Odoo/Python side
54 pub(crate) id: JsonRpcId,
55
56 /// A struct containing the error information
57 pub(crate) error: JsonRpcError,
58}
59
60#[derive(Debug, Serialize, Deserialize)]
61pub struct JsonRpcError {
62 /// The error code. Currently hardcoded to `200`
63 pub code: u32,
64
65 /// The error "message". This is a short string indicating the type of
66 /// error. Some examples are:
67 /// * `Odoo Server Error`
68 /// * `404: Not Found`
69 /// * `Odoo Session Expired`
70 pub message: String,
71
72 /// The actual error data
73 pub data: JsonRpcErrorData,
74}
75
76impl std::fmt::Display for JsonRpcError {
77 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
78 write!(f, "{}", self.message)
79 }
80}
81
82impl std::error::Error for JsonRpcError {}
83
84#[derive(Debug, Serialize, Deserialize)]
85pub struct JsonRpcErrorData {
86 /// The module? and type of the object where the exception was raised
87 ///
88 /// For example:
89 /// * `builtins.TypeError`
90 /// * `odoo.addons.account.models.account_move.AccountMove`
91 pub name: String,
92
93 /// The Python exception stack trace
94 pub debug: String,
95
96 /// The Python exception message (e.g. `str(exception)`)
97 pub message: String,
98
99 /// The Python exception arguments (e.g. `excetion.args`)
100 pub arguments: Vec<Value>,
101
102 /// The Python exception context (e.g. `excetion.context`)
103 pub context: Map<String, Value>,
104}