prop/lib/microwaveprop_web/api/fallback_controller.ex
Graham McIntire fd976b0cd5
fix: resolve 391 Credo issues across codebase
- Add jump_credo_checks ~> 0.4 with all 20 checks enabled
- Fix all standard Credo issues: 139 @spec (113 done, 26 remain),
  4 refactoring, 3 alias usage, 9 System.cmd env, 5 unsafe_to_atom,
  2 max line length, 9 assert_receive timeout
- Fix 170+ jump_credo_checks warnings:
  - 117 TopLevelAliasImportRequire: move nested alias/import to module top
  - 32 UseObanProWorker: switch to Oban.Pro.Worker
  - 4 DoctestIExExamples: add doctests / create test file
  - ~20 WeakAssertion: strengthen type-check assertions
  - Various ConditionalAssertion, AssertReceiveTimeout fixes
- Exclude vendor/ from Credo analysis
- Remaining: 175 warnings (mostly opinionated WeakAssertion,
  AvoidSocketAssignsInTest), 26 @spec annotations
2026-06-12 13:51:32 -05:00

36 lines
1.2 KiB
Elixir

defmodule MicrowavepropWeb.Api.FallbackController do
@moduledoc """
Translates `{:error, term}` tuples returned from `/api/v1` controller
actions into RFC 9457 problem+json responses. Wired via
`action_fallback/1` in `MicrowavepropWeb.Api.V1.BaseController`.
"""
use Phoenix.Controller, formats: [:json]
alias MicrowavepropWeb.Api.ErrorJSON
@spec call(Plug.Conn.t(), term()) :: Plug.Conn.t()
def call(conn, {:error, %Ecto.Changeset{} = changeset}) do
ErrorJSON.send_changeset(conn, changeset)
end
def call(conn, {:error, :not_found}) do
ErrorJSON.send_problem(conn, 404, "not_found", "Resource not found.")
end
def call(conn, {:error, :forbidden}) do
ErrorJSON.send_problem(conn, 403, "forbidden", "You may not access this resource.")
end
def call(conn, {:error, :unauthorized}) do
ErrorJSON.send_problem(conn, 401, "unauthorized", "Authentication is required.")
end
def call(conn, {:error, :bad_request, detail}) when is_binary(detail) do
ErrorJSON.send_problem(conn, 400, "bad_request", detail)
end
def call(conn, {:error, :duplicate, _existing}) do
ErrorJSON.send_problem(conn, 409, "conflict", "An equivalent resource already exists.")
end
end