Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gestion erreur timeout pour validateur GTFS #2378

Merged
merged 3 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion apps/shared/lib/validation/gtfs_validator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ defmodule Shared.Validation.GtfsValidator do
Return {:ok, validation_report} if validation succeed with or without errors.
Return {:error} if validation cannot be done.
"""
@spec validate(binary()) :: {:ok, map()}
@spec validate(binary()) :: {:ok, map()} | {:error, binary()}
def validate(gtfs),
do:
build_validate_url()
|> send_post_request(gtfs)
|> handle_validation_response()

@impl Validator
@spec validate_from_url(binary()) :: {:ok, map()} | {:error, binary()}
def validate_from_url(gtfs_url),
do:
gtfs_url
Expand Down Expand Up @@ -74,6 +75,10 @@ defmodule Shared.Validation.GtfsValidator do
{:error, "Error while requesting GTFS validator"}
end

defp handle_validation_response({:error, _}) do
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

La clause précédente faisait comme s'il y avait toujours un body, ce qui n'arrive pas en cas de timeout à la connexion ou à la réception.

{:error, "Error while requesting GTFS validator"}
end

defp http_client, do: Application.fetch_env!(:transport, :httpoison_impl)

defp send_get_request(url), do: http_client().get(url, [], recv_timeout: @timeout)
Expand Down
12 changes: 12 additions & 0 deletions apps/shared/test/validation/gtfs_validator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ defmodule GtfsValidatorTest do
|> assert_validation_report_is(expected_validation_report)
end

test "with a timeout" do
gtfs_url = "http://example.com/gtfs.zip"
expected_url = "https://validation.transport.data.gouv.fr/validate?url=#{URI.encode_www_form(gtfs_url)}"

Transport.HTTPoison.Mock
|> expect(:get, fn ^expected_url, [], [recv_timeout: 180_000] ->
{:error, %HTTPoison.Error{reason: :timeout}}
end)

assert {:error, "Error while requesting GTFS validator"} == GtfsValidator.validate_from_url(gtfs_url)
end

defp assert_validation_report_is({:ok, obtained_validation_report}, expected_validation_report),
do: assert(obtained_validation_report == expected_validation_report)

Expand Down