(
State(state): State<ServeState>,
payload: Result<Json<InvokeRequest>, JsonRejection>,
)
| 194 | } |
| 195 | |
| 196 | async fn invoke_handler( |
| 197 | State(state): State<ServeState>, |
| 198 | payload: Result<Json<InvokeRequest>, JsonRejection>, |
| 199 | ) -> Response { |
| 200 | let Json(payload) = match payload { |
| 201 | Ok(payload) => payload, |
| 202 | Err(err) => { |
| 203 | let status = if err.status() == StatusCode::PAYLOAD_TOO_LARGE { |
| 204 | StatusCode::PAYLOAD_TOO_LARGE |
| 205 | } else { |
| 206 | StatusCode::BAD_REQUEST |
| 207 | }; |
| 208 | return json_error(status, format!("Invalid JSON request: {err}")); |
| 209 | } |
| 210 | }; |
| 211 | |
| 212 | let _permit = if dispatch::uses_subprocess_or_network_probe(&payload.cmd) { |
| 213 | match state.subprocess_limit.acquire().await { |
| 214 | Ok(permit) => Some(permit), |
| 215 | Err(_) => { |
| 216 | return json_error(StatusCode::SERVICE_UNAVAILABLE, "Server is shutting down") |
| 217 | } |
| 218 | } |
| 219 | } else { |
| 220 | None |
| 221 | }; |
| 222 | |
| 223 | match dispatch::dispatch(&state.app_state, &payload.cmd, payload.args).await { |
| 224 | Ok(value) => Json(value).into_response(), |
| 225 | Err(dispatch::DispatchError::UnknownCommand) => { |
| 226 | json_error(StatusCode::NOT_FOUND, "Unknown command") |
| 227 | } |
| 228 | Err(dispatch::DispatchError::BadArgs(err)) => { |
| 229 | json_error(StatusCode::BAD_REQUEST, format!("Invalid arguments: {err}")) |
| 230 | } |
| 231 | Err(dispatch::DispatchError::Command(err)) => json_error(StatusCode::BAD_REQUEST, err), |
| 232 | Err(dispatch::DispatchError::Serialize(err)) => json_error( |
| 233 | StatusCode::BAD_REQUEST, |
| 234 | format!("Failed to encode response: {err}"), |
| 235 | ), |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | async fn api_not_found() -> Response { |
| 240 | json_error(StatusCode::NOT_FOUND, "Unknown API route") |
nothing calls this directly
no test coverage detected