Skip to content

Commit

Permalink
fix: fix error formatting and add error handling for apps
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis G committed Mar 15, 2022
1 parent cfa356d commit ca0c42c
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 26 deletions.
15 changes: 10 additions & 5 deletions apps/lenra/lib/lenra/services/openfaas_services.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ defmodule Lenra.OpenfaasServices do
)

Finch.build(:post, url, headers, body)
|> Finch.request(FaasHttp)
|> Finch.request(FaasHttp, receive_timeout: 1000)
|> response(:decode)
|> case do
{:ok, %{"data" => data}} -> {:ok, data}
{:error, :ressource_not_found} -> {:error, :listener_not_found}
err -> err
end
end
Expand All @@ -77,10 +78,11 @@ defmodule Lenra.OpenfaasServices do
body = Jason.encode!(%{widget: widget_name, data: data, props: props})

Finch.build(:post, url, headers, body)
|> Finch.request(FaasHttp)
|> Finch.request(FaasHttp, receive_timeout: 1000)
|> response(:decode)
|> case do
{:ok, %{"widget" => widget}} -> {:ok, widget}
{:error, :ressource_not_found} -> {:error, :widget_not_found}
err -> err
end
end
Expand All @@ -99,13 +101,16 @@ defmodule Lenra.OpenfaasServices do
headers = [{"Content-Type", "application/json"} | base_headers]

Finch.build(:post, url, headers)
|> Finch.request(FaasHttp)
|> Finch.request(FaasHttp, receive_timeout: 1000)
|> response(:decode)
|> case do
{:ok, %{"manifest" => manifest}} ->
Logger.debug("Got manifest : #{inspect(manifest)}")
{:ok, manifest}

{:error, :ressource_not_found} ->
{:error, :manifest_not_found}

err ->
Logger.error("Error while getting manifest : #{inspect(err)}")
err
Expand Down Expand Up @@ -153,7 +158,7 @@ defmodule Lenra.OpenfaasServices do
headers,
body
)
|> Finch.request(FaasHttp)
|> Finch.request(FaasHttp, receive_timeout: 1000)
|> response(:deploy_app)
end

Expand All @@ -172,7 +177,7 @@ defmodule Lenra.OpenfaasServices do
"functionName" => get_function_name(service_name, build_number)
})
)
|> Finch.request(FaasHttp)
|> Finch.request(FaasHttp, receive_timeout: 1000)
|> response(:delete_app)
end

Expand Down
2 changes: 1 addition & 1 deletion apps/lenra/test/lenra/services/openfaas_services_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ 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 {:error, :ressource_not_found} ==
assert {:error, :listener_not_found} ==
OpenfaasServices.run_listener(
@john_doe_application,
@john_doe_environment,
Expand Down
5 changes: 5 additions & 0 deletions apps/lenra_web/lib/lenra_web/application_runner_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule LenraWeb.ApplicationRunnerAdapter do
event
) do
Logger.info("Run listener for action #{action}")

OpenfaasServices.run_listener(application, environment, action, data, props, event)
end

Expand Down Expand Up @@ -98,4 +99,8 @@ defmodule LenraWeb.ApplicationRunnerAdapter do
) do
send(socket_pid, {:send, atom, ui_or_patches})
end

def on_ui_changed(session_state, message) do
raise "Error, not maching on_ui_changed/2 #{inspect(session_state)}, #{inspect(message)}"
end
end
30 changes: 23 additions & 7 deletions apps/lenra_web/lib/lenra_web/channels/app_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,31 @@ defmodule LenraWeb.AppChannel do
{:noreply, socket}
end

def handle_info({:send, :error, reason}, socket) do
Logger.debug("send error #{inspect(%{error: reason})}")
def handle_info({:send, :error, {:error, reason}}, socket) when is_atom(reason) do
Logger.error("Send error #{inspect(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
push(socket, "error", %{"errors" => ErrorHelpers.translate_error(reason)})
{:noreply, socket}
end

def handle_info({:send, :error, {:error, :invalid_ui, errors}}, socket) when is_list(errors) do
formatted_errors =
errors
|> Enum.map(fn {message, path} -> %{code: 0, message: "#{message} at path #{path}"} end)

push(socket, "error", %{"errors" => formatted_errors})
{:noreply, socket}
end

def handle_info({:send, :error, reason}, socket) when is_atom(reason) do
Logger.error("Send error atom #{inspect(reason)}")
push(socket, "error", %{"errors" => ErrorHelpers.translate_error(reason)})
{:noreply, socket}
end

def handle_info({:send, :error, malformated_error}, socket) do
Logger.error("Malformatted error #{inspect(malformated_error)}")
push(socket, "error", %{"errors" => ErrorHelpers.translate_error(:unknow_error)})
{:noreply, socket}
end

Expand Down
13 changes: 7 additions & 6 deletions apps/lenra_web/lib/lenra_web/views/error_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ 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."},
openfass_not_recheable: %{code: 16, message: "Openfaas could not be reached."},
application_not_found: %{code: 17, message: "The application was not found in Openfaas."},
listener_not_found: %{code: 18, message: "No listener found in app manifest."},
openfaas_delete_error: %{code: 19, message: "Openfaas could not delete the application."},
timeout: %{code: 20, message: "Openfaas timeout."},
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."},
widget_not_found: %{code: 23, message: "No Widget found in app manifest."},
no_app_authorization: %{code: 24, message: "You are not authorized to join this app."},
invalid_ui: %{code: 25, message: "Invalid UI"},
bad_request: %{code: 400, message: "Server cannot understand or process the request due to a client-side error."},
error_404: %{code: 404, message: "Not Found."},
error_500: %{code: 500, message: "Internal server error."},
Expand Down
15 changes: 8 additions & 7 deletions apps/lenra_web/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ defmodule LenraWeb.MixProject do
{:lenra, in_umbrella: true},
{:cors_plug, "~> 3.0", only: :dev, runtime: false},
{:bouncer, git: "https://github.com/lenra-io/bouncer.git", tag: "v1.0.0"},
private_git(
name: :application_runner,
host: "github.com",
project: "lenra-io/application-runner.git",
tag: "v1.0.0-beta.22",
credentials: "shiipou:#{System.get_env("GH_PERSONNAL_TOKEN")}"
)
{:application_runner, path: "../../../application-runner"}
# private_git(
# name: :application_runner,
# host: "github.com",
# project: "lenra-io/application-runner.git",
# tag: "v1.0.0-beta.22",
# credentials: "shiipou:#{System.get_env("GH_PERSONNAL_TOKEN")}"
# )
]
end

Expand Down

0 comments on commit ca0c42c

Please sign in to comment.