Skip to content

Commit 5e563be

Browse files
authored
Merge pull request #194 from giusdp/main
Return parsed error when fn already exists
2 parents bd93fa8 + ecc8a84 commit 5e563be

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed

Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
export SHELL:=/bin/bash
16+
export SHELLOPTS:=$(if $(SHELLOPTS),$(SHELLOPTS):)pipefail:errexit
17+
18+
.ONESHELL:
19+
1520
.PHONY: build-core-image build-worker-image credo dial test
1621

1722
SECRET_KEY_BASE ?= $(shell mix phx.gen.secret)
@@ -38,9 +43,12 @@ dial:
3843

3944
## Run test suite, launch Postgres with docker-compose
4045
test:
46+
function tearDown {
47+
docker compose -f docker-compose.yml down
48+
}
49+
trap tearDown EXIT
4150
mix deps.get
4251
docker compose -f docker-compose.yml up --detach
4352
mix core.utest
4453
mix worker.utest
4554
mix core.itest
46-
docker compose -f docker-compose.yml down

apps/core/lib/core_web/controllers/fallback_controller.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ defmodule CoreWeb.FallbackController do
4242
|> render(:"400")
4343
end
4444

45+
def call(conn, {:error, :conflict}) do
46+
conn
47+
|> put_status(:conflict)
48+
|> put_view(CoreWeb.ErrorView)
49+
|> render(:"409")
50+
end
51+
4552
def call(conn, {:error, :no_workers}) do
4653
res = %{errors: %{detail: "No worker available"}}
4754

apps/core/lib/core_web/controllers/function_controller.ex

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,36 @@ defmodule CoreWeb.FunctionController do
5858
{:error, :bad_params}
5959
else
6060
with {:ok, code} <- File.read(tmp_path),
61-
{:ok, module} <- Modules.get_module_by_name(module_name),
62-
{:ok, %Function{} = function} <-
63-
%{"name" => fn_name, "code" => code}
61+
{:ok, module} <- Modules.get_module_by_name(module_name) do
62+
case %{"name" => fn_name, "code" => code}
6463
|> Map.put_new("module_id", module.id)
6564
|> Functions.create_function() do
66-
event_results = Events.connect_events(fn_name, module_name, events_req)
67-
sinks_results = DataSink.plug_data_sinks(fn_name, module_name, sinks_req)
65+
{:ok, %Function{} = function} ->
66+
Logger.info(
67+
"Function Controller: function #{module_name}/#{fn_name} created successfully."
68+
)
6869

69-
{status, render_params} =
70-
build_render_params(%{function: function}, event_results, sinks_results, :created)
70+
event_results = Events.connect_events(fn_name, module_name, events_req)
71+
sinks_results = DataSink.plug_data_sinks(fn_name, module_name, sinks_req)
7172

72-
conn
73-
|> put_status(status)
74-
|> render("show.json", render_params)
73+
{status, render_params} =
74+
build_render_params(%{function: function}, event_results, sinks_results, :created)
75+
76+
conn
77+
|> put_status(status)
78+
|> render("show.json", render_params)
79+
80+
{:error, %{errors: [function_module_index_constraint: {"has already been taken", _}]}} ->
81+
Logger.error("Function Controller: #{module_name}/#{fn_name} already exists.")
82+
{:error, :conflict}
83+
84+
e ->
85+
Logger.error(
86+
"Function Controller: error while creating #{module_name}/#{fn_name}: #{inspect(e)}."
87+
)
88+
89+
e
90+
end
7591
end
7692
end
7793
end

apps/core/test/core_web/integration/controllers/function_controller_test.exs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,20 @@ defmodule CoreWeb.FunctionControllerTest do
209209
"sinks_metadata" => %{"successful" => 1, "failed" => 1}
210210
} = json_response(conn, 207)["data"]
211211
end
212+
213+
test "error when module doesn't exist", %{conn: conn} do
214+
conn = post(conn, Routes.function_path(conn, :create, "non_existing_module"), @create_attrs)
215+
assert json_response(conn, 404)["errors"] == %{"detail" => "Not Found"}
216+
end
217+
218+
test "error when creating already existing function", %{conn: conn} do
219+
module = module_fixture()
220+
conn = post(conn, Routes.function_path(conn, :create, module.name), @create_attrs)
221+
assert json_response(conn, 201)["data"] == %{"name" => "some_name"}
222+
223+
conn = post(conn, Routes.function_path(conn, :create, module.name), @create_attrs)
224+
assert json_response(conn, 409)["errors"] == %{"detail" => "Conflict"}
225+
end
212226
end
213227

214228
describe "update function" do

0 commit comments

Comments
 (0)