From 9f4410622fbc235719b1184c1313aa32a87dd43d Mon Sep 17 00:00:00 2001 From: Zhaochen Li Date: Tue, 1 Jul 2025 12:14:37 +1000 Subject: [PATCH] Handle agent config not found gracefully --- .../agent_config_validator.rb | 23 +++++++++++++++++++ .../agent_prerequisites_operations/main.rb | 13 ++++------- ee/lib/remote_development/messages.rb | 3 +++ 3 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 ee/lib/remote_development/agent_prerequisites_operations/agent_config_validator.rb diff --git a/ee/lib/remote_development/agent_prerequisites_operations/agent_config_validator.rb b/ee/lib/remote_development/agent_prerequisites_operations/agent_config_validator.rb new file mode 100644 index 00000000000000..56d992c16d9481 --- /dev/null +++ b/ee/lib/remote_development/agent_prerequisites_operations/agent_config_validator.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module RemoteDevelopment + module AgentPrerequisitesOperations + class AgentConfigValidator + include Messages + + # @param [Hash] context + # @return [Hash] + def self.build(context) + # NOTE: We do not type-check here because we want to use the fast_spec_helper in the specs. + # We trust that we will get the right object type from the API layer above. + context => { agent: agent } + + if agent.unversioned_latest_workspaces_agent_config.present? + Gitlab::Fp::Result.ok(context) + else + Gitlab::Fp::Result.err(AgentPrerequisitesAgentConfigNotFound.new('Agent config not found')) + end + end + end + end +end diff --git a/ee/lib/remote_development/agent_prerequisites_operations/main.rb b/ee/lib/remote_development/agent_prerequisites_operations/main.rb index d73501b8a9f3e0..81d75eb50856c1 100644 --- a/ee/lib/remote_development/agent_prerequisites_operations/main.rb +++ b/ee/lib/remote_development/agent_prerequisites_operations/main.rb @@ -14,6 +14,7 @@ def self.main(context) result = initial_result + .and_then(AgentConfigValidator.method(:validate)) .map(ResponseBuilder.method(:build)) .map( # As the final step, return the response_payload content in a WorkspaceReconcileSuccessful message @@ -30,14 +31,10 @@ def self.main(context) shared_namespace: String } { status: :success, payload: message.content } - - # NOTE: This ROP chain currently consists of only `map` steps, there are no `and_then` steps. Therefore it - # is not possible for anything other than the AgentPrerequisitesSuccessful message from the last lambda - # step to be returned. If we ever add an `and_then` step, we should uncomment the else case below, and - # add an appropriate spec example named: "when an unmatched error is returned, an exception is raised" - # - # else - # raise Gitlab::Fp::UnmatchedResultError.new(result: result) + in { err: AgentPrerequisitesAgentConfigNotFound => message } + generate_error_response_from_message(message: message, reason: :bad_request) + else + raise Gitlab::Fp::UnmatchedResultError.new(result: result) end end end diff --git a/ee/lib/remote_development/messages.rb b/ee/lib/remote_development/messages.rb index e5040511365156..719747941d219e 100644 --- a/ee/lib/remote_development/messages.rb +++ b/ee/lib/remote_development/messages.rb @@ -12,6 +12,9 @@ module Messages # AgentConfigOperations errors AgentConfigUpdateFailed = Class.new(Gitlab::Fp::Message) + # AgentPrerequisitesOperations errors + AgentPrerequisitesAgentConfigNotFound = Class.new(Gitlab::Fp::Message) + # Workspace create errors WorkspaceCreateParamsValidationFailed = Class.new(Gitlab::Fp::Message) WorkspaceCreateDevfileLoadFailed = Class.new(Gitlab::Fp::Message) -- GitLab