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

Handle when the script fails due there is not internet connection or due an unexpected error #21

Merged
merged 7 commits into from Jun 21, 2020

Conversation

ivanhercaz
Copy link
Collaborator

This PR consists of two commits. One in which is added two exceptions in lib/ex_tldr/exceptions.ex and the another one in which the exceptions are applied to each case:

  1. If the script fails due there is not internet connection, raise NoInternetConnectionError.
  2. If the script fails due an unexpected error, raise UnexpectedError with the error message and encourage the user to report it on this repository.

This commit adds two exceptions, one te report there is not internet
connection and another to handle an unexpected error.
If the request failed first check if it is because there is not internet
connection, if true, raise an NoInternetConnectionError, if it is not
the error, it raise an UnexpectedError and report to the user the error,
encouraging to report it in the official repository.
@ivanhercaz ivanhercaz linked an issue May 14, 2020 that may be closed by this pull request
@ivanhercaz
Copy link
Collaborator Author

ivanhercaz commented May 14, 2020

The check that fails in this PR is the coverage test by Coveralls, because it reports a decrease of 4.2 %. The reason of this decrease are the two new conditions applied to raise the errors. I am very new performing coverage tests so I need to learn how to cover this part of the code inside tests. In addition, I have to learn how to test the condition applied when there is not internet connection.

Any idea? Either way, I will probably ask in Stack Overflow about this kind of tests before to go sleep.

CC. @owenvoke

P.S. I have think in the development of a mock, but I do not know how the mock could simulate a lack of internet connection.

@sbrl
Copy link
Member

sbrl commented May 14, 2020

I don't know Elixr, but achieving complete code coverage with tests is all about inversion of control. If your code is sufficiently decoupled, you should be able to swap parts out for other "mock" parts.

If you haven't already, the SOLID principles are great to read up on.

@ivanhercaz
Copy link
Collaborator Author

ivanhercaz commented May 14, 2020

I don't know Elixr, but achieving complete code coverage with tests is all about inversion of control. If your code is sufficiently decoupled, you should be able to swap parts out for other "mock" parts.

If you haven't already, the SOLID principles are great to read up on.

Very interesting point. I am going to read more about it. I ask to you in Gitter because although someone does not know about one or another language, sometimes the procedural and logic, the theories, may help to address what someone wants in another language. This is the reason why I am going to read more about what you commented. Thank you very much @sbrl!

I would like to test everything before merge it. This case is a bit different, but I still have this idea. However, it may delay the development of others task.

Anyway, I already asked in Stack Overflow.

It is a "possible test" because it can not be implemented as it is,
because HTTPoison does not raise the defined exception. But it is
interesting to keep the test for the future implementation of the
necessary behaviour to adress this issue.
@ivanhercaz
Copy link
Collaborator Author

Well, after a very interesting answer to the question I asked, several readings about how mocks work and how it would be possible to create a test with a mock, I finally use Mox for this issue (check 75fede7 although the test is commented for future reference, because now it will fails). Although I think I know how it might be address, at this moment it does not viable to apply because the methodology should be:

  • Define a behavior for the client wrapper.
  • Then apply the behavior.

With this it would be possible to create a mock for the behavior and then test the response of the behavior and not the response of HTTPotion. At least I have in my head how it could be done, although there may be something I am not considering. Now I can not define and apply the behavior, due I will not have enough time during this week, and I do not know if I could the next week. So I propose two options:

  1. Merge this PR, although the Coveralls test failed, and open two issues for the pending tasks (one for the behavior, another for the test).
  2. Keep this PR opened and IO.inspect will be there until we could create a good test to cover this issue.

I prefer the first option: we change the IO.inspect by meaningful errors, open an issue for the behavior and another one for the test, both will require one PR each one to track fine both.

What do you think about it?

CC. @sbrl @owenvoke (I mentioned both of you because you already participated in ExTldr).

Comment on lines +67 to +73
#test "should return lack of internet" do
# assert_raise NoInternetConnectionError,
# fn ->
# ClientMock
# |> expect(:get, fn _ -> {:error, %HTTPoison.Error{reason: :nxdomain}} end)
# end
#end
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is the test I referred in my last comment. I think that this one applied to a mock of a behavior defined for our wrapper, and not for HTTPoison.Base, will work (if am not wrong...).

@ivanhercaz
Copy link
Collaborator Author

I have finally decided to opt for the first option that I exposed in my last comment in this PR: merge this PR and open two new issues, one for the behavior and another one for the test.

@ivanhercaz ivanhercaz merged commit 99492e8 into master Jun 21, 2020
@ivanhercaz ivanhercaz deleted the replace_io_inspect/1 branch June 21, 2020 18:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exception when there is not an internet connection
2 participants