Skip to content

Commit

Permalink
feat: Handle openfass error (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
pichoemr authored Feb 21, 2022
1 parent 93ade9a commit 37595be
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 44 deletions.
31 changes: 22 additions & 9 deletions apps/lenra/lib/lenra/services/openfaas_services.ex
Original file line number Diff line number Diff line change
Expand Up @@ -186,24 +186,37 @@ defmodule Lenra.OpenfaasServices do
end

defp response({:ok, %Finch.Response{status: status_code}}, :delete_app)
when status_code in [200, 202, 404] do
if status_code == 404 do
Logger.error("The application was not found in Openfaas. It should not happen.")
end

when status_code in [200, 202] do
{:ok, status_code}
end

defp response({:ok, %Finch.Response{}}, :delete_app) do
raise "Openfaas could not delete the application. It should not happen."
Logger.error("Openfaas could not delete the application. It should not happen.")
{:error, :openfaas_delete_error}
end

defp response({:error, %Mint.TransportError{reason: _reason}}, _action) do
raise "Openfaas could not be reached. It should not happen."
Logger.error("Openfaas could not be reached. It should not happen.")
{:error, :openfass_not_recheable}
end

defp response({:ok, %Finch.Response{status: status_code, body: body}}, _action)
defp response(
{:ok, %Finch.Response{status: status_code, body: body}},
_action
)
when status_code not in [200, 202] do
raise "Openfaas error (#{status_code}) #{body}"
case status_code do
404 ->
Logger.error(body)
{:error, :ressource_not_found}

500 ->
Logger.error(body)
{:error, body}

504 ->
Logger.error(body)
{:error, :timeout}
end
end
end
47 changes: 20 additions & 27 deletions apps/lenra/test/lenra/services/openfaas_services_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,15 @@ defmodule Lenra.OpenfaasServicesTest do
test "Openfaas correctly handle 404 not found", %{app: app} do
FaasStub.stub_action_once(app, "InitData", {:error, 404, "Not Found"})

assert_raise(RuntimeError, "Openfaas error (404) Not Found", fn ->
OpenfaasServices.run_listener(
@john_doe_application,
@john_doe_environment,
"InitData",
%{},
%{},
%{}
)
end)
assert {:error, :ressource_not_found} ==
OpenfaasServices.run_listener(
@john_doe_application,
@john_doe_environment,
"InitData",
%{},
%{},
%{}
)
end
end

Expand All @@ -87,12 +86,11 @@ defmodule Lenra.OpenfaasServicesTest do

describe "deploy" do
test "app but openfaas unreachable" do
assert_raise(RuntimeError, "Openfaas could not be reached. It should not happen.", fn ->
OpenfaasServices.deploy_app(
@john_doe_application.service_name,
@john_doe_build.build_number
)
end)
assert {:error, :openfass_not_recheable} ==
OpenfaasServices.deploy_app(
@john_doe_application.service_name,
@john_doe_build.build_number
)
end

test "app and openfaas reachable" do
Expand Down Expand Up @@ -127,16 +125,11 @@ defmodule Lenra.OpenfaasServicesTest do
FaasStub.create_faas_stub()
|> FaasStub.expect_delete_app_once({:error, 400, "Bad request"})

assert_raise(
RuntimeError,
"Openfaas could not delete the application. It should not happen.",
fn ->
OpenfaasServices.delete_app_openfaas(
@john_doe_application.service_name,
@john_doe_build.build_number
)
end
)
assert {:error, :openfaas_delete_error} ==
OpenfaasServices.delete_app_openfaas(
@john_doe_application.service_name,
@john_doe_build.build_number
)
end

@tag capture_log: true
Expand All @@ -150,7 +143,7 @@ defmodule Lenra.OpenfaasServicesTest do
@john_doe_build.build_number
)

assert res == {:ok, 404}
assert res == {:error, :openfaas_delete_error}
end
end

Expand Down
27 changes: 22 additions & 5 deletions apps/lenra_web/lib/lenra_web/channels/app_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defmodule LenraWeb.AppChannel do
use Phoenix.Channel
alias ApplicationRunner.{SessionManager, SessionManagers}
alias Lenra.{Environment, LenraApplication, LenraApplicationServices, Repo}
alias LenraWeb.ErrorHelpers

require Logger

def join("app", %{"app" => app_name}, socket) do
Expand Down Expand Up @@ -44,16 +46,20 @@ defmodule LenraWeb.AppChannel do
:ok <- SessionManager.init_data(session_pid) do
{:ok, assign(socket, session_pid: session_pid)}
else
{:error, reason} ->
{:error, %{reason: reason}}
# Application error
{:error, reason} when is_bitstring(reason) ->
{:error, %{reason: [%{code: -1, message: reason}]}}

{:error, reason} when is_atom(reason) ->
{:error, %{reason: ErrorHelpers.translate_error(reason)}}
end
else
_err -> {:error, %{reason: "No app found"}}
_err -> {:error, %{reason: ErrorHelpers.translate_error(:no_app_found)}}
end
end

def join("app", _any, _socket) do
{:error, %{reason: "No App Name"}}
{:error, %{reason: ErrorHelpers.translate_error(:no_app_found)}}
end

defp select_env(%LenraApplication{} = app) do
Expand All @@ -63,7 +69,6 @@ defmodule LenraWeb.AppChannel do
defp start_session(env_id, session_id, session_assigns, env_assigns) do
case SessionManagers.start_session(session_id, env_id, session_assigns, env_assigns) do
{:ok, session_pid} -> {:ok, session_pid}
{:error, {:already_started, session_pid}} -> {:ok, session_pid}
{:error, message} -> {:error, message}
end
end
Expand All @@ -81,6 +86,18 @@ defmodule LenraWeb.AppChannel do
{:noreply, socket}
end

def handle_info({:send, :error, reason}, socket) do
Logger.debug("send error #{inspect(%{error: reason})}")

case is_atom(reason) do
true -> push(socket, "error", %{"errors" => ErrorHelpers.translate_error(reason)})
# Application error
false -> push(socket, "error", %{"errors" => [%{code: -1, message: reason}]})
end

{:noreply, socket}
end

def handle_in("run", %{"code" => code, "event" => event}, socket) do
handle_run(socket, code, event)
end
Expand Down
8 changes: 8 additions & 0 deletions apps/lenra_web/lib/lenra_web/views/error_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule LenraWeb.ErrorHelpers do
"""

@errors [
unknow_error: %{code: 0, message: "Unknown error"},
password_not_equals: %{code: 1, message: "Password must be equals."},
parameters_null: %{code: 2, message: "Parameters can't be null."},
no_validation_code: %{
Expand All @@ -22,7 +23,14 @@ defmodule LenraWeb.ErrorHelpers do
invalid_uuid: %{code: 13, message: "The code is not a valid UUID"},
invalid_code: %{code: 14, message: "The code is invalid"},
invalid_build_status: %{code: 15, message: "The build status should be success or failure."},
openfass_not_recheable: %{code: 16, message: "Openfaas could not be reached. This should not happen."},
application_not_found: %{code: 17, message: "The application was not found in Openfaas. This should not happen."},
listener_not_found: %{code: 18, message: "No listener found in app manifest. This should not happen."},
openfaas_delete_error: %{code: 19, message: "Openfaas could not delete the application. This should not happen."},
timeout: %{code: 20, message: "Openfaas timeout. This should not happen."},
no_app_found: %{code: 21, message: "No application found for the current link."},
environement_not_build: %{code: 22, message: "This application was not yet build."},
widget_not_found: %{code: 23, message: "No Widget found in app manifest. This should not happen."},
error_404: %{code: 404, message: "Not Found."},
error_500: %{code: 500, message: "Internal server error."},
openfaas_not_reachable: %{code: 1000, message: "Openfaas is not accessible"},
Expand Down
2 changes: 1 addition & 1 deletion apps/lenra_web/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule LenraWeb.MixProject do
name: :application_runner,
host: "github.com",
project: "lenra-io/application-runner.git",
tag: "v1.0.0-beta.17",
tag: "v1.0.0-beta.20",
credentials: "shiipou:#{System.get_env("GH_PERSONNAL_TOKEN")}"
)
]
Expand Down
2 changes: 1 addition & 1 deletion apps/lenra_web/test/channels/app_channel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ defmodule LenraWeb.AppChannelTest do

test "No app called, should return an error", %{socket: socket} do
res = my_subscribe_and_join(socket)
assert {:error, %{reason: "No App Name"}} == res
assert {:error, %{reason: [%{code: 21, message: "No application found for the current link."}]}} == res
refute_push("ui", _)
end

Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
%{
"application_runner": {:git, "git@github.com:lenra-io/application-runner.git", "8375d360123bd900fb9a712769af1ac5466f2065", [tag: "v1.0.0-beta.17", submodules: true]},
"application_runner": {:git, "git@github.com:lenra-io/application-runner.git", "0676d9c484dfea671b600c61fb3e23352fcb6409", [tag: "v1.0.0-beta.20", submodules: true]},
"argon2_elixir": {:hex, :argon2_elixir, "2.4.0", "2a22ea06e979f524c53b42b598fc6ba38cdcbc977a155e33e057732cfb1fb311", [:make, :mix], [{:comeonin, "~> 5.3", [hex: :comeonin, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "4ea82e183cf8e7f66dab1f767fedcfe6a195e140357ef2b0423146b72e0a551d"},
"bamboo": {:hex, :bamboo, "2.1.0", "3c58f862efd74fa8c8d48a410ac592b41f7d24785e828566f7a0af549269ddc3", [:mix], [{:hackney, ">= 1.15.2", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.4", [hex: :mime, repo: "hexpm", optional: false]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "f0ad2623b9a1d2dc06dcf289b59df9ebc522f49f3a21971ec87a8fce04e6d33e"},
"bamboo_smtp": {:hex, :bamboo_smtp, "4.0.1", "7e48188663f6164a81183688bb263be4c3952648fcd3ce52164f44d68777f9cd", [:mix], [{:bamboo, "~> 2.1.0", [hex: :bamboo, repo: "hexpm", optional: false]}, {:gen_smtp, "~> 1.1.1", [hex: :gen_smtp, repo: "hexpm", optional: false]}], "hexpm", "7ff1d62ae39bfb1c14f6d3cddba0fa1482a45c2a2b497a2da601eff7099605c8"},
Expand Down

0 comments on commit 37595be

Please sign in to comment.