From 0aa06ac1d4b061ab9a644ef1caddd796d42517f2 Mon Sep 17 00:00:00 2001 From: Rogerio Pontual Date: Wed, 18 Dec 2024 22:53:58 +0100 Subject: [PATCH] Refactor error handling to centralize reply --- .../controllers/api/workflows_controller.ex | 88 ++++++++----------- 1 file changed, 36 insertions(+), 52 deletions(-) diff --git a/lib/lightning_web/controllers/api/workflows_controller.ex b/lib/lightning_web/controllers/api/workflows_controller.ex index ad72b505f2..85be1724d5 100644 --- a/lib/lightning_web/controllers/api/workflows_controller.ex +++ b/lib/lightning_web/controllers/api/workflows_controller.ex @@ -32,28 +32,7 @@ defmodule LightningWeb.API.WorkflowsController do save_workflow(params, conn.assigns.current_resource) do json(conn, %{id: workflow_id, error: nil}) end - |> then(fn result -> - case result do - {:error, :too_many_workflows} -> - conn - |> put_status(:unprocessable_entity) - |> json(%{ - id: nil, - error: "Your plan has reached the limit of active workflows." - }) - - {:error, :too_many_active_triggers} -> - conn - |> put_status(:unprocessable_entity) - |> json(%{ - id: nil, - error: "A workflow can have only one trigger enabled at a time." - }) - - result -> - result - end - end) + |> then(&maybe_handle_error(conn, &1)) end def show(conn, %{"project_id" => project_id, "id" => workflow_id}) do @@ -71,36 +50,7 @@ defmodule LightningWeb.API.WorkflowsController do save_workflow(workflow, params, conn.assigns.current_resource) do json(conn, %{id: workflow_id, error: nil}) end - |> then(fn result -> - case result do - {:error, :cannot_replace_trigger} -> - conn - |> put_status(:unprocessable_entity) - |> json(%{ - id: workflow_id, - error: "The triggers cannot be replaced, only edited or added." - }) - - {:error, :too_many_workflows} -> - conn - |> put_status(:unprocessable_entity) - |> json(%{ - id: workflow_id, - error: "Your plan has reached the limit of active workflows." - }) - - {:error, :too_many_active_triggers} -> - conn - |> put_status(:unprocessable_entity) - |> json(%{ - id: workflow_id, - error: "A workflow can have only one trigger enabled at a time." - }) - - result -> - result - end - end) + |> then(&maybe_handle_error(conn, &1, workflow_id)) end defp save_workflow(%{"project_id" => project_id} = params, user) do @@ -191,4 +141,38 @@ defmodule LightningWeb.API.WorkflowsController do project ) end + + defp maybe_handle_error(conn, result, workflow_id \\ nil) do + case result do + {:error, :cannot_replace_trigger} -> + reply_422( + conn, + workflow_id, + "The triggers cannot be replaced, only edited or added." + ) + + {:error, :too_many_workflows} -> + reply_422( + conn, + workflow_id, + "Your plan has reached the limit of active workflows." + ) + + {:error, :too_many_active_triggers} -> + reply_422( + conn, + workflow_id, + "A workflow can have only one trigger enabled at a time." + ) + + result -> + result + end + end + + defp reply_422(conn, workflow_id, msg) do + conn + |> put_status(:unprocessable_entity) + |> json(%{id: workflow_id, error: msg}) + end end