Skip to content

Commit

Permalink
Refactor error handling to centralize reply
Browse files Browse the repository at this point in the history
  • Loading branch information
jyeshe committed Dec 18, 2024
1 parent 28ada67 commit 0aa06ac
Showing 1 changed file with 36 additions and 52 deletions.
88 changes: 36 additions & 52 deletions lib/lightning_web/controllers/api/workflows_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 0aa06ac

Please sign in to comment.