mirror of
https://github.com/discourse/discourse.git
synced 2024-11-24 20:51:50 +08:00
142571bba0
* `rescue nil` is a really bad pattern to use in our code base. We should rescue errors that we expect the code to throw and not rescue everything because we're unsure of what errors the code would throw. This would reduce the amount of pain we face when debugging why something isn't working as expexted. I've been bitten countless of times by errors being swallowed as a result during debugging sessions.
16 lines
392 B
Ruby
16 lines
392 B
Ruby
class UploadUrlValidator < ActiveModel::EachValidator
|
|
def validate_each(record, attribute, value)
|
|
if value.present?
|
|
uri =
|
|
begin
|
|
URI.parse(value)
|
|
rescue URI::InvalidURIError
|
|
end
|
|
|
|
unless uri && Upload.exists?(url: value)
|
|
record.errors[attribute] << (options[:message] || I18n.t('errors.messages.invalid'))
|
|
end
|
|
end
|
|
end
|
|
end
|