prismicio-community/ruby-kit

Better errors on initialization

Closed this issue · 5 comments

If you call Prismic.api and pass nil or an empty string "", the library fails with cryptic errors.

Prismic.api(nil) 
# => NoMethodError: undefined method `+' for nil:NilClass

Prismic.api("")
# => NoMethodError: undefined method `request_uri' for #<URI::Generic ?>

We ran into this problem when passing the API URL as an ENV var.

api = Prismic.api(ENV['PRISMIC_API_URL'])

The var didn't get set on deploy, and when the page loaded, we saw that first error. After some quick investigation, we realized the issue was an empty ENV var. However, I'd recommend adding a custom error. If you pass nil or an empty string, the library could throw a EmptyKeyError or something similar so it's way more clear what the error actually is.

Great idea. @erwan maybe validate the input as a valid uri using something along these lines:

http://stackoverflow.com/a/1805788

erwan commented

Good idea, what do you think about this?

396729d

Looks good. Does the kit raise an error if the user inputs a valid uri BUT isn't a valid Prismic.io endpoint?

erwan commented

Not at that point, it's going to throw when it tries to parse the Json. We can't really hardcode things like the prismic.io domain because we have dev and stage environments we need to test against.

Feel free to test it, and if the error that happens when parsing the Json isn't clear enough we can work on that.

Definitely an improvement 👍🏻