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

Introduce Kubeclient::ResourceNotFoundError #233

Merged
merged 1 commit into from
Apr 28, 2017

Conversation

kirs
Copy link
Contributor

@kirs kirs commented Mar 10, 2017

First of all, thanks a lot for creating and maintaining the gem. It helps us a lot in building Kubernetes tools and scripts.

I'd like to to add a distinction between the generic error (KubeException) and the specific "not found" error. Right now in my code I have to do:

    def self.delete_namespace(namespace)
      kubeclient.delete_namespace(namespace)
    rescue KubeException => e
      raise unless e.to_s.include?("not found")
    end

This would be much easier if I could rescue only ResourceNotFoundError.

cc @simon3z @KnVerey

@simon3z
Copy link
Collaborator

simon3z commented Mar 13, 2017

@moolitayer @cben can you review? (It seems backward compatible to me but maybe you can spot something).

@cben
Copy link
Collaborator

cben commented Mar 13, 2017

Looks safe.
👍 to exposing Not Found in some way, 👍 to nesting it under Kubeclient:: rather than top-level like KubeException.

The problem this partially solves (as did #221) is we eat up specific RestClient exception class, replacing them all with a single class. Are we gonna want to re-expose more RestClient exceptions this way? Is there a better way?

Could we maybe inherit from RestClient's own exception classes? Can't inherit from 2 classes in ruby, so KubeException would have to become a module. In some cases like NotFound, we could simply "tag" the original exception object in-place by err.extend(KubeException); in other cases, like when we parse 'message' JSON field, we would use our own class (that also includes KubeException module).
See https://blog.jayway.com/2011/05/25/ruby-an-exceptional-language/ about this "tag with a module" technique.

In any case, given that RestClient has close to 70 exception classes: https://gist.github.com/cben/892458b3ca8f8fa28d4962503fb446f0, I'd at least stick to its naming.
If we need our own class, I'd call it Kubeclient::Exceptions::NotFound.

@simon3z
Copy link
Collaborator

simon3z commented Mar 13, 2017

@kirs BTW you can improve your code in the description by using the error_code (rather that the string error).

    def self.delete_namespace(namespace)
      kubeclient.delete_namespace(namespace)
    rescue KubeException => e
      raise unless e.error_code == 404
    end

Anyway it could be that NotFound is a common enough use-case to deserve its own exception.
Any thoughts on this @moolitayer @enoodle? I think you would use that in SSA.
@cben I didn't understand if you're in favor or not? (Beside naming).

@moolitayer
Copy link
Collaborator

moolitayer commented Mar 14, 2017

Thought the same about testing the error_code vs. the message.

I'm not sure how well this would scale in terms of different http error classes.

I generally liked the approach @grosser suggested in #195 since

  • we have a general HttpException
  • exceptions are in the same file (and should be in the same namespace, see below). Not hooked on file but some form of aggregation for all exceptions.

If we do decide to go this direction, I have a few nit pics:

  • all our exceptions should be name spaced the same, or code catching them would look inconsistent[1]
  • the name resource_not_found_error should be more consistent with kube_exception
  • we would need a test of backward compatibility of the new raised exception
    (Don't put any time into fixing just yet @kirs)

@kirs thanks for including the example code, it's explains clearly what you are trying to achieve which is good 👍

[1] If i remember correctly #195 was supposed to do that, in a backward compatible manner? i.e add correct scoping but also still support the non scope param and deprecate it

@grosser
Copy link
Contributor

grosser commented Mar 14, 2017

FYI would also be nice to use a http library that does not need native extensions (or depends on other gems)

@simon3z
Copy link
Collaborator

simon3z commented Mar 14, 2017

FYI would also be nice to use a http library that does not need native extensions (or depends on other gems)

@grosser any suggestion?

@grosser
Copy link
Contributor

grosser commented Mar 14, 2017 via email

@moolitayer
Copy link
Collaborator

we use faraday a lot the middleware layering is a nice way of adding
reusable addons ... httpclient looks decent too and has no dependencies

@grosser can you please create an issue with this info? we can then get more feedback on it (and we would also want to switch clients in some other gems)

@grosser
Copy link
Contributor

grosser commented Mar 15, 2017

rest-client issue moved to #237

@kirs
Copy link
Contributor Author

kirs commented Mar 25, 2017

Personally I don't think that there's any need for exception class per HTTP code. 404 is most common one, and for the rest it's enough to look into error.response.code.

the name resource_not_found_error should be more consistent with kube_exception

There's a convention in Ruby to name exception classes as "errors". Good examples are StandardError, ArgumentError, IndexError. Sometimes the error keyword is even omitted, like in case with ActiveRecord::RecordNotFound. If we want to bring consistency here, KubeException should be the one to rename.

Should I rebase my PR on top of #195?

@cben
Copy link
Collaborator

cben commented Apr 23, 2017

#195 has landed, please rebase (and inherit from the new Kubeclient::HttpError).
LGTM otherwise.

@kirs kirs force-pushed the not-found-error branch from eefb0a0 to 6918f5a Compare April 23, 2017 22:13
@kirs
Copy link
Contributor Author

kirs commented Apr 23, 2017

@cben thanks, I rebased it.

Copy link
Collaborator

@moolitayer moolitayer left a comment

Choose a reason for hiding this comment

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

Patch looks good, feature seems reasonable.
cc @abonas @simon3z

@simon3z simon3z merged commit 5e6035c into ManageIQ:master Apr 28, 2017
cben pushed a commit to cben/kubeclient that referenced this pull request Jan 22, 2018
Introduce Kubeclient::ResourceNotFoundError
cben added a commit to cben/kubeclient that referenced this pull request Jan 22, 2018
Reverts parts of commit 263ff40
from ManageIQ#271 that restored KubeException - no longer needed
after backporting ManageIQ#195 and ManageIQ#233, we now have
Kubeclient::HttpError and Kubeclient::ResourceNotFoundError.

In other words, v2.x branch is now closer to original ManageIQ#262.
cben added a commit to cben/kubeclient that referenced this pull request Jan 22, 2018
Reverts parts of commit 263ff40
from ManageIQ#271 that restored KubeException - no longer needed
after backporting ManageIQ#195 and ManageIQ#233, we now have
Kubeclient::HttpError and Kubeclient::ResourceNotFoundError.

In other words, v2.x branch is now closer to original ManageIQ#262.
cben added a commit to cben/kubeclient that referenced this pull request Jan 22, 2018
This reverts commit 263ff40 from ManageIQ#271.
Those backport changes are no longer needed:
- after backporting ManageIQ#195 and ManageIQ#233, v2.x now has
  Kubeclient::HttpError and Kubeclient::ResourceNotFoundError.
- after backporting ManageIQ#247, v2.x now tests with webmock 2.x,
  it takes `basic_auth` rather than user:pw in URL.
- ns/namespace change was just cosmetic.

In other words, v2.x branch is now closer to original ManageIQ#262.
cben pushed a commit to cben/kubeclient that referenced this pull request Jan 25, 2018
Introduce Kubeclient::ResourceNotFoundError
cben added a commit to cben/kubeclient that referenced this pull request Jan 25, 2018
This reverts commit 263ff40 from ManageIQ#271.
Those backport changes are no longer needed:
- after backporting ManageIQ#195 and ManageIQ#233, v2.x now has
  Kubeclient::HttpError and Kubeclient::ResourceNotFoundError.
- after backporting ManageIQ#247, v2.x now tests with webmock 2.x,
  it takes `basic_auth` rather than user:pw in URL.
- ns/namespace change was just cosmetic.

In other words, v2.x branch is now closer to original ManageIQ#262.
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.

5 participants