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

Raise a meaningful error if ActiveStorage::Current.host is blank #41996

Conversation

kennyadsl
Copy link
Contributor

@kennyadsl kennyadsl commented Apr 16, 2021

Summary

It's very hard to understand what happens with the following exception

URI::InvalidURIError:
  bad URI(is not URI?): nil

that is raised when trying to generate a URL for Disk service without setting the ActiveStorage::Current.host first.

This can happen when the ActiveStorage::SetCurrent is not included in a controller, or when testing URL generation outside of the controllers layer (eg. testing URL generation in a model).

cc @elia @filippoliverani

@kennyadsl kennyadsl force-pushed the kennyadsl/custom-error-without-as-current-host branch from aab8a35 to db7767e Compare April 16, 2021 14:36
@kennyadsl kennyadsl changed the title Raise CurrentHostNotDefinedError if ActiveStorage::Current.host is blank Raise a meaningful error if ActiveStorage::Current.host is blank Apr 16, 2021
@kennyadsl kennyadsl force-pushed the kennyadsl/custom-error-without-as-current-host branch from db7767e to 4f30eaf Compare April 16, 2021 14:36
@@ -124,6 +124,10 @@ def generate_url(key, expires_in:, filename:, content_type:, disposition:)
purpose: :blob_key
)

if current_host.blank?
raise "Cannot generate URL for #{filename} using Disk service, please set ActiveStorage::Current.host."
Copy link
Member

Choose a reason for hiding this comment

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

I think this should use argument error instead:

Suggested change
raise "Cannot generate URL for #{filename} using Disk service, please set ActiveStorage::Current.host."
raise ArgumentError, "Cannot generate URL for #{filename} using Disk service, please set ActiveStorage::Current.host."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@eileencodes thanks for the review. Your suggestion makes perfect sense to me, I've updated the PR accordingly. 🙂

@kennyadsl kennyadsl force-pushed the kennyadsl/custom-error-without-as-current-host branch from 4f30eaf to 10f2a9d Compare May 10, 2021 15:26
It's very hard to understand what happens with the following exception

  URI::InvalidURIError:
    bad URI(is not URI?): nil

that is raised when trying to generate a URL for Disk service without
setting the ActiveStorage::Current.host first.

This can happen when the ActiveStorage::SetCurrent is not included
in a controller, or when testing URL generation outside of the
controllers layer (eg. testing URL generation in a model).

Co-authored-by: elia <elia@schito.me>
Co-authored-by: filippo <dev@mailvore.com>
@kennyadsl kennyadsl force-pushed the kennyadsl/custom-error-without-as-current-host branch from 10f2a9d to f971a3f Compare May 10, 2021 15:27
@eileencodes eileencodes merged commit 2d465c5 into rails:main May 10, 2021
@eileencodes
Copy link
Member

Thanks for the PR and congrats on your first Rails commit 💖

@kennyadsl kennyadsl deleted the kennyadsl/custom-error-without-as-current-host branch May 11, 2021 07:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants