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 errors thrown by Faraday and Curb and re-raise a PDC:Error #20

Open
sthaha opened this issue Jan 18, 2017 · 7 comments
Open

Handle errors thrown by Faraday and Curb and re-raise a PDC:Error #20

sthaha opened this issue Jan 18, 2017 · 7 comments

Comments

@sthaha
Copy link
Contributor

sthaha commented Jan 18, 2017

In order to allow clients to handle all errors raised within PDC, lets provide a superclass PDC::Error than can be caught instead of having to understand the internals of pdc.

e.g.

begin
  PDC::V1::Release.find(release_id)
rescue Curl::Err::HostResolutionError, Faraday::ConnectionFailed
  # handle it ...
rescue PDC::TokenFetchFailed
  # handle it ...
rescue  Curl::Err::CurlError => e
  # handle it ...
end

The client must be able to handle it as

begin
  PDC::V1::Release.find(release_id)
rescue PDC::Error => e
  # handle it ...
end
@ycheng-aa
Copy link
Contributor

Added it to JIRA (PDC-1835) for future plan.

@xiangge
Copy link

xiangge commented Feb 23, 2018

Seems there already has a PDC:Error class, so no need to do the changes?

@sthaha
Copy link
Contributor Author

sthaha commented Feb 23, 2018

@xiangge yes, see the code in the bug report.

begin
  PDC::V1::Release.find(release_id)
rescue Curl::Err::HostResolutionError, Faraday::ConnectionFailed
  # handle it ...
rescue PDC::TokenFetchFailed
  # handle it ...
rescue  Curl::Err::CurlError => e
  # handle it ...
end

We want to replace the whole error handling with

begin
  PDC::V1::Release.find(release_id)
rescue PDC::Error => e
  # handle it ...
end

the Faraday ruby gem has a nice way of doing this see: https://github.com/lostisland/faraday/blob/master/lib/faraday/error.rb

@xiangge
Copy link

xiangge commented Feb 26, 2018

Looking into that, thanks :)

@xiangge
Copy link

xiangge commented Feb 28, 2018

@sthaha after we add the Faraday::ConnectionFailed with the PDC::Error.const_set, we still can't catch the errors with PDC:Error, we just can catch it with PDC::Error::ConnectionFailed

I have one question, why people need to write codes like that.
I think they can write the codes like below when we insert the codes in #54.

  begin
    data = PDC::V1::Release.find('rhel-7.1').sigkey
  rescue => e
    puts e
  end

@sthaha
Copy link
Contributor Author

sthaha commented Feb 28, 2018

@xiangge if you look into Faradays implementation closely, you would find that it has a way to
wrap the original error thrown inside the library as a Faraday error

So the idea is to catch errors generated by Faraday and Curl inside the pdc lib and wrap them as PDC::Error so that the user of PDC do not have to know the internals of the library (errors generated inside the pdc library) and only need to handle PDC::Error or its sub-classes.

Say you want to replace Faraday with something better or Curl with something else, now the client code will have to change according to our implmentation, instead if we wrapped all errors as PDC::Error or sub-class, then the client code will only have to handle the PDC::Errors

HTH

@xiangge
Copy link

xiangge commented Mar 1, 2018

@sthaha Yep, that's also what I am thinking, just handle the other Exceptions in pdc codes.
Thank you for the explanation, would you please help to review my new pr #54 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants