Skip to content

Commit 82da721

Browse files
committed
server/json-rpc: Simplify handlers with to_response helper
Consolidate the repetitive success/error handling pattern into a single `to_response` helper function. Inline the error module into handlers.rs and remove redundant per-handler logging (the dispatch function already logs method, params, and connection info).
1 parent ffde190 commit 82da721

File tree

3 files changed

+114
-142
lines changed

3 files changed

+114
-142
lines changed

server/json-rpc/src/error.rs

Lines changed: 0 additions & 47 deletions
This file was deleted.

server/json-rpc/src/handlers.rs

Lines changed: 114 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! for each JSON-RPC method.
55
66
use std::collections::BTreeMap;
7+
use std::fmt::Debug;
78
use std::net::SocketAddr;
89
use std::sync::Arc;
910

@@ -12,15 +13,24 @@ use axum::http::{HeaderMap, StatusCode};
1213
use axum::response::IntoResponse;
1314
use axum::Json;
1415
use graph::prelude::{
15-
DeploymentHash, NodeId, SubgraphName, SubgraphRegistrar, Value as GraphValue, ENV_VARS,
16+
DeploymentHash, NodeId, SubgraphName, SubgraphRegistrar, SubgraphRegistrarError, ENV_VARS,
1617
};
1718
use serde::Deserialize;
1819
use serde_json::{self, Value as JsonValue};
19-
use slog::{info, Logger};
20+
use slog::{error, info, Logger};
2021

21-
use crate::error::{error_codes, registrar_error_to_jsonrpc};
2222
use crate::jsonrpc::{JsonRpcError, JsonRpcId, JsonRpcRequest, JsonRpcResponse};
2323

24+
/// Application-specific error codes for subgraph operations.
25+
mod error_codes {
26+
pub const DEPLOY_ERROR: i64 = 0;
27+
pub const REMOVE_ERROR: i64 = 1;
28+
pub const CREATE_ERROR: i64 = 2;
29+
pub const REASSIGN_ERROR: i64 = 3;
30+
pub const PAUSE_ERROR: i64 = 4;
31+
pub const RESUME_ERROR: i64 = 5;
32+
}
33+
2434
/// Shared application state for the JSON-RPC server.
2535
pub struct AppState<R> {
2636
pub registrar: Arc<R>,
@@ -102,6 +112,33 @@ fn parse_params<T: for<'de> Deserialize<'de>>(
102112
})
103113
}
104114

115+
/// Convert a registrar result to a JSON-RPC response.
116+
fn to_response<P: Debug>(
117+
logger: &Logger,
118+
method: &str,
119+
error_code: i64,
120+
params: &P,
121+
result: Result<JsonValue, SubgraphRegistrarError>,
122+
id: JsonRpcId,
123+
) -> JsonRpcResponse {
124+
match result {
125+
Ok(value) => JsonRpcResponse::success(id, value),
126+
Err(e) => {
127+
error!(logger, "{} failed", method;
128+
"error" => format!("{:?}", e),
129+
"params" => format!("{:?}", params));
130+
131+
let message = if let SubgraphRegistrarError::Unknown(_) = e {
132+
"internal error".to_owned()
133+
} else {
134+
e.to_string()
135+
};
136+
137+
JsonRpcResponse::error(id, JsonRpcError::new(error_code, message))
138+
}
139+
}
140+
}
141+
105142
/// Handler for `subgraph_create`.
106143
async fn handle_create<R: SubgraphRegistrar>(
107144
state: &AppState<R>,
@@ -113,24 +150,20 @@ async fn handle_create<R: SubgraphRegistrar>(
113150
Err(resp) => return resp,
114151
};
115152

116-
info!(&state.logger, "Received subgraph_create request"; "params" => format!("{:?}", params));
153+
let result = state
154+
.registrar
155+
.create_subgraph(params.name.clone())
156+
.await
157+
.map(|r| serde_json::to_value(r).expect("invalid result"));
117158

118-
match state.registrar.create_subgraph(params.name.clone()).await {
119-
Ok(result) => {
120-
let value = serde_json::to_value(result).expect("invalid subgraph creation result");
121-
JsonRpcResponse::success(id, value)
122-
}
123-
Err(e) => {
124-
let error = registrar_error_to_jsonrpc(
125-
&state.logger,
126-
"subgraph_create",
127-
e,
128-
error_codes::CREATE_ERROR,
129-
&params,
130-
);
131-
JsonRpcResponse::error(id, error)
132-
}
133-
}
159+
to_response(
160+
&state.logger,
161+
"subgraph_create",
162+
error_codes::CREATE_ERROR,
163+
&params,
164+
result,
165+
id,
166+
)
134167
}
135168

136169
/// Handler for `subgraph_deploy`.
@@ -144,12 +177,10 @@ async fn handle_deploy<R: SubgraphRegistrar>(
144177
Err(resp) => return resp,
145178
};
146179

147-
info!(&state.logger, "Received subgraph_deploy request"; "params" => format!("{:?}", params));
148-
149180
let node_id = params.node_id.clone().unwrap_or(state.node_id.clone());
150181
let routes = subgraph_routes(&params.name, state.http_port);
151182

152-
match state
183+
let result = state
153184
.registrar
154185
.create_subgraph_version(
155186
params.name.clone(),
@@ -164,19 +195,16 @@ async fn handle_deploy<R: SubgraphRegistrar>(
164195
false,
165196
)
166197
.await
167-
{
168-
Ok(_) => JsonRpcResponse::success(id, routes),
169-
Err(e) => {
170-
let error = registrar_error_to_jsonrpc(
171-
&state.logger,
172-
"subgraph_deploy",
173-
e,
174-
error_codes::DEPLOY_ERROR,
175-
&params,
176-
);
177-
JsonRpcResponse::error(id, error)
178-
}
179-
}
198+
.map(|_| routes);
199+
200+
to_response(
201+
&state.logger,
202+
"subgraph_deploy",
203+
error_codes::DEPLOY_ERROR,
204+
&params,
205+
result,
206+
id,
207+
)
180208
}
181209

182210
/// Handler for `subgraph_remove`.
@@ -190,21 +218,20 @@ async fn handle_remove<R: SubgraphRegistrar>(
190218
Err(resp) => return resp,
191219
};
192220

193-
info!(&state.logger, "Received subgraph_remove request"; "params" => format!("{:?}", params));
221+
let result = state
222+
.registrar
223+
.remove_subgraph(params.name.clone())
224+
.await
225+
.map(|_| JsonValue::Null);
194226

195-
match state.registrar.remove_subgraph(params.name.clone()).await {
196-
Ok(_) => JsonRpcResponse::success(id, serde_json::to_value(GraphValue::Null).unwrap()),
197-
Err(e) => {
198-
let error = registrar_error_to_jsonrpc(
199-
&state.logger,
200-
"subgraph_remove",
201-
e,
202-
error_codes::REMOVE_ERROR,
203-
&params,
204-
);
205-
JsonRpcResponse::error(id, error)
206-
}
207-
}
227+
to_response(
228+
&state.logger,
229+
"subgraph_remove",
230+
error_codes::REMOVE_ERROR,
231+
&params,
232+
result,
233+
id,
234+
)
208235
}
209236

210237
/// Handler for `subgraph_reassign`.
@@ -218,25 +245,20 @@ async fn handle_reassign<R: SubgraphRegistrar>(
218245
Err(resp) => return resp,
219246
};
220247

221-
info!(&state.logger, "Received subgraph_reassignment request"; "params" => format!("{:?}", params));
222-
223-
match state
248+
let result = state
224249
.registrar
225250
.reassign_subgraph(&params.ipfs_hash, &params.node_id)
226251
.await
227-
{
228-
Ok(_) => JsonRpcResponse::success(id, serde_json::to_value(GraphValue::Null).unwrap()),
229-
Err(e) => {
230-
let error = registrar_error_to_jsonrpc(
231-
&state.logger,
232-
"subgraph_reassign",
233-
e,
234-
error_codes::REASSIGN_ERROR,
235-
&params,
236-
);
237-
JsonRpcResponse::error(id, error)
238-
}
239-
}
252+
.map(|_| JsonValue::Null);
253+
254+
to_response(
255+
&state.logger,
256+
"subgraph_reassign",
257+
error_codes::REASSIGN_ERROR,
258+
&params,
259+
result,
260+
id,
261+
)
240262
}
241263

242264
/// Handler for `subgraph_pause`.
@@ -250,21 +272,20 @@ async fn handle_pause<R: SubgraphRegistrar>(
250272
Err(resp) => return resp,
251273
};
252274

253-
info!(&state.logger, "Received subgraph_pause request"; "params" => format!("{:?}", params));
275+
let result = state
276+
.registrar
277+
.pause_subgraph(&params.deployment)
278+
.await
279+
.map(|_| JsonValue::Null);
254280

255-
match state.registrar.pause_subgraph(&params.deployment).await {
256-
Ok(_) => JsonRpcResponse::success(id, serde_json::to_value(GraphValue::Null).unwrap()),
257-
Err(e) => {
258-
let error = registrar_error_to_jsonrpc(
259-
&state.logger,
260-
"subgraph_pause",
261-
e,
262-
error_codes::PAUSE_ERROR,
263-
&params,
264-
);
265-
JsonRpcResponse::error(id, error)
266-
}
267-
}
281+
to_response(
282+
&state.logger,
283+
"subgraph_pause",
284+
error_codes::PAUSE_ERROR,
285+
&params,
286+
result,
287+
id,
288+
)
268289
}
269290

270291
/// Handler for `subgraph_resume`.
@@ -278,21 +299,20 @@ async fn handle_resume<R: SubgraphRegistrar>(
278299
Err(resp) => return resp,
279300
};
280301

281-
info!(&state.logger, "Received subgraph_resume request"; "params" => format!("{:?}", params));
302+
let result = state
303+
.registrar
304+
.resume_subgraph(&params.deployment)
305+
.await
306+
.map(|_| JsonValue::Null);
282307

283-
match state.registrar.resume_subgraph(&params.deployment).await {
284-
Ok(_) => JsonRpcResponse::success(id, serde_json::to_value(GraphValue::Null).unwrap()),
285-
Err(e) => {
286-
let error = registrar_error_to_jsonrpc(
287-
&state.logger,
288-
"subgraph_resume",
289-
e,
290-
error_codes::RESUME_ERROR,
291-
&params,
292-
);
293-
JsonRpcResponse::error(id, error)
294-
}
295-
}
308+
to_response(
309+
&state.logger,
310+
"subgraph_resume",
311+
error_codes::RESUME_ERROR,
312+
&params,
313+
result,
314+
id,
315+
)
296316
}
297317

298318
/// Build the subgraph routes response for deploy.

server/json-rpc/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//! This crate provides a JSON-RPC 2.0 server for managing subgraphs,
44
//! supporting operations like create, deploy, remove, reassign, pause, and resume.
55
6-
mod error;
76
mod handlers;
87
mod jsonrpc;
98
mod server;

0 commit comments

Comments
 (0)