Skip to content

Commit

Permalink
fix: No Sentry alert on 500 error from the app
Browse files Browse the repository at this point in the history
  • Loading branch information
taorepoara committed Sep 4, 2024
1 parent f0d56c4 commit 811a508
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
8 changes: 8 additions & 0 deletions libs/application_runner/lib/services/application_services.ex
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,14 @@ defmodule ApplicationRunner.ApplicationServices do
TechnicalError.openfaas_not_reachable_tuple()
end

defp response(
{:ok, %Finch.Response{status: 500, body: body}},
listener
)
when listener in [:manifest, :view, :listener] do
TechnicalError.error_500_tuple(body)
end

defp response(
{:ok, %Finch.Response{status: status_code, body: body}},
_listener
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
defmodule ApplicationRunner.ApplicationServicesTest do
use ApplicationRunner.ConnCase, async: false
import TelemetryTest

alias ApplicationRunner.ApplicationServices

setup [:telemetry_listen]

@function_name Ecto.UUID.generate()

defp handle_resp(conn) do
Expand Down Expand Up @@ -67,6 +70,7 @@ defmodule ApplicationRunner.ApplicationServicesTest do
ApplicationServices.start_app(@function_name)
end

@tag telemetry_listen: [:application_runner, :alert, :event]
test "failing app info while stating app" do
bypass = Bypass.open(port: 1234)
Bypass.stub(bypass, "GET", "/system/function/#{@function_name}", &handle_error_resp/1)
Expand All @@ -79,8 +83,23 @@ defmodule ApplicationRunner.ApplicationServicesTest do
reason: :openfaas_not_reachable
}
} = result

assert_received(
{:telemetry_event,
%{
event: [:application_runner, :alert, :event],
measurements: %LenraCommon.Errors.TechnicalError{
__exception__: true,
message: "Internal server error.",
reason: :error_500,
status_code: 500
},
metadata: %{}
}}
)
end

@tag telemetry_listen: [:application_runner, :alert, :event]
test "failing app info while stoping app" do
bypass = Bypass.open(port: 1234)
Bypass.stub(bypass, "GET", "/system/function/#{@function_name}", &handle_error_resp/1)
Expand All @@ -93,6 +112,20 @@ defmodule ApplicationRunner.ApplicationServicesTest do
reason: :openfaas_not_reachable
}
} = result

assert_received(
{:telemetry_event,
%{
event: [:application_runner, :alert, :event],
measurements: %LenraCommon.Errors.TechnicalError{
__exception__: true,
message: "Internal server error.",
reason: :error_500,
status_code: 500
},
metadata: %{}
}}
)
end

test "fetch_view" do
Expand Down Expand Up @@ -142,4 +175,44 @@ defmodule ApplicationRunner.ApplicationServicesTest do

ApplicationServices.fetch_view(@function_name, "testé", %{}, %{}, %{})
end

@tag telemetry_listen: [:application_runner, :alert, :event]
test "failing fetch_view" do
bypass = Bypass.open(port: 1234)

Bypass.expect_once(
bypass,
"POST",
"/function/#{@function_name}",
&handle_error_resp/1
)

result = ApplicationServices.fetch_view(@function_name, "test", %{}, %{}, %{})

assert {
:error,
%LenraCommon.Errors.TechnicalError{
reason: :error_500,
__exception__: true,
message: "Internal server error.",
metadata: "error",
status_code: 500
}
} = result

refute_received(
{:telemetry_event,
%{
event: [:application_runner, :alert, :event],
measurements: %LenraCommon.Errors.TechnicalError{
__exception__: true,
message: "Internal server error.",
reason: :error_500,
status_code: 500
},
metadata: %{}
}},
"The alert event should not be sent when the fetch_view fails"
)
end
end

0 comments on commit 811a508

Please sign in to comment.