From d398d761d6e3f9ceb41d476fa96767367b833bed Mon Sep 17 00:00:00 2001 From: Bly Kim Date: Wed, 18 Dec 2024 16:44:15 +0900 Subject: [PATCH] Handle configuration conversion failures explicitly --- CHANGELOG.md | 2 ++ src/graphql/node/input.rs | 65 ++++++++++++++++++++++++++------------- 2 files changed, 46 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54e32aa..6e9e81a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,8 @@ Versioning](https://semver.org/spec/v2.0.0.html). - Resolved an issue in the `applyNode` GraphQL API, where configuration values set to an empty string were not saved to the `config` in the database. +- Fixed an issue where configuration conversion failures were silently ignored, + leading to incorrect None handling. ## [0.24.0] - 2024-11-19 diff --git a/src/graphql/node/input.rs b/src/graphql/node/input.rs index 1ea3694..2cb2cf2 100644 --- a/src/graphql/node/input.rs +++ b/src/graphql/node/input.rs @@ -1,7 +1,7 @@ use std::{collections::HashMap, fmt}; use anyhow::Context as AnyhowContext; -use async_graphql::{types::ID, InputObject, Result}; +use async_graphql::{types::ID, Error, InputObject, Result}; use super::{AgentKind, AgentStatus}; @@ -138,27 +138,50 @@ pub(super) fn create_draft_update( .map(|agent| (agent.key.clone(), agent.config.clone())) .collect(); - let agents: Vec = if let Some(new_agents) = new.agents { - new_agents - .into_iter() - .map(|agent_draft| { - let config = old_config_map - .get(&agent_draft.key) - .and_then(|config| config.as_ref().and_then(|c| c.clone().try_into().ok())); - - review_database::Agent { - node: u32::MAX, - key: agent_draft.key, - kind: agent_draft.kind.into(), - status: agent_draft.status.into(), - config, - draft: agent_draft.draft.and_then(|draft| draft.try_into().ok()), - } + let agents: Vec = + new.agents + .map(|new_agents| { + new_agents + .into_iter() + .map(|new_agent| { + let config = + match old_config_map.get(&new_agent.key) { + Some(config) => match config.as_ref() { + Some(c) => Some(c.clone().try_into().map_err(|_| { + Error::new("Failed to convert agent config") + })?), + None => None, + }, + None => { + return Err(Error::new(format!( + "Missing configuration for agent key: {}", + new_agent.key + ))) + } + }; + + let draft = match new_agent.draft { + Some(draft) => Some( + draft + .try_into() + .map_err(|_| Error::new("Failed to convert agent draft"))?, + ), + None => None, + }; + + Ok(review_database::Agent { + node: u32::MAX, + key: new_agent.key, + kind: new_agent.kind.into(), + status: new_agent.status.into(), + config, + draft, + }) + }) + .collect::, _>>() }) - .collect() - } else { - Vec::new() - }; + .transpose()? + .unwrap_or_default(); let giganto: Option = new.giganto.map(Into::into);