mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Remove use of rescue nil.
* `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.
This commit is contained in:
@@ -316,8 +316,8 @@ module BackupRestore
|
||||
|
||||
def log(message)
|
||||
timestamp = Time.now.strftime("%Y-%m-%d %H:%M:%S")
|
||||
puts(message) rescue nil
|
||||
publish_log(message, timestamp) rescue nil
|
||||
puts(message)
|
||||
publish_log(message, timestamp)
|
||||
save_log(message, timestamp)
|
||||
end
|
||||
|
||||
|
||||
@@ -500,8 +500,8 @@ module BackupRestore
|
||||
|
||||
def log(message)
|
||||
timestamp = Time.now.strftime("%Y-%m-%d %H:%M:%S")
|
||||
puts(message) rescue nil
|
||||
publish_log(message, timestamp) rescue nil
|
||||
puts(message)
|
||||
publish_log(message, timestamp)
|
||||
save_log(message, timestamp)
|
||||
end
|
||||
|
||||
|
||||
@@ -128,7 +128,11 @@ module Discourse
|
||||
if Rails.env.development?
|
||||
plugin_hash = Digest::SHA1.hexdigest(all_plugins.map { |p| p.path }.sort.join('|'))
|
||||
hash_file = "#{Rails.root}/tmp/plugin-hash"
|
||||
old_hash = File.read(hash_file) rescue nil
|
||||
|
||||
old_hash = begin
|
||||
File.read(hash_file)
|
||||
rescue Errno::ENOENT
|
||||
end
|
||||
|
||||
if old_hash && old_hash != plugin_hash
|
||||
puts "WARNING: It looks like your discourse plugins have recently changed."
|
||||
@@ -236,7 +240,13 @@ module Discourse
|
||||
end
|
||||
|
||||
def self.route_for(uri)
|
||||
uri = URI(uri) rescue nil unless uri.is_a?(URI)
|
||||
unless uri.is_a?(URI)
|
||||
uri = begin
|
||||
URI(uri)
|
||||
rescue URI::InvalidURIError
|
||||
end
|
||||
end
|
||||
|
||||
return unless uri
|
||||
|
||||
path = uri.path || ""
|
||||
|
||||
@@ -823,7 +823,7 @@ module Email
|
||||
end
|
||||
end
|
||||
ensure
|
||||
tmp.try(:close!) rescue nil
|
||||
tmp&.close!
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -30,20 +30,12 @@ class FinalDestination
|
||||
|
||||
def initialize(url, opts = nil)
|
||||
@url = url
|
||||
@uri =
|
||||
begin
|
||||
URI(escape_url) if @url
|
||||
rescue URI::InvalidURIError
|
||||
end
|
||||
@uri = uri(escape_url) if @url
|
||||
|
||||
@opts = opts || {}
|
||||
@force_get_hosts = @opts[:force_get_hosts] || []
|
||||
@opts[:max_redirects] ||= 5
|
||||
@opts[:lookup_ip] ||= lambda do |host|
|
||||
begin
|
||||
FinalDestination.lookup_ip(host)
|
||||
end
|
||||
end
|
||||
@opts[:lookup_ip] ||= lambda { |host| FinalDestination.lookup_ip(host) }
|
||||
@ignored = [Discourse.base_url_no_prefix] + (@opts[:ignore_redirects] || [])
|
||||
@limit = @opts[:max_redirects]
|
||||
@status = :ready
|
||||
@@ -106,9 +98,8 @@ class FinalDestination
|
||||
|
||||
if result == :redirect
|
||||
old_port = uri.port
|
||||
|
||||
location = "#{uri.scheme}://#{uri.host}#{location}" if location[0] == "/"
|
||||
uri = URI(location) rescue nil
|
||||
uri = uri(location)
|
||||
|
||||
# https redirect, so just cache that whole new domain is https
|
||||
if old_port == 80 && uri&.port == 443 && (URI::HTTPS === uri)
|
||||
@@ -204,9 +195,8 @@ class FinalDestination
|
||||
|
||||
if location
|
||||
old_port = @uri.port
|
||||
|
||||
location = "#{@uri.scheme}://#{@uri.host}#{location}" if location[0] == "/"
|
||||
@uri = URI(location) rescue nil
|
||||
@uri = uri(location)
|
||||
@limit -= 1
|
||||
|
||||
# https redirect, so just cache that whole new domain is https
|
||||
@@ -243,7 +233,8 @@ class FinalDestination
|
||||
end
|
||||
|
||||
def hostname_matches?(url)
|
||||
@uri && url.present? && @uri.hostname == (URI(url) rescue nil)&.hostname
|
||||
url = uri(url)
|
||||
@uri && url.present? && @uri.hostname == url&.hostname
|
||||
end
|
||||
|
||||
def is_dest_valid?
|
||||
@@ -383,4 +374,13 @@ class FinalDestination
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def uri(location)
|
||||
begin
|
||||
URI(location)
|
||||
rescue URI::InvalidURIError, ArgumentError
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -161,7 +161,10 @@ module ImportExport
|
||||
end
|
||||
|
||||
def new_category_id(external_category_id)
|
||||
CategoryCustomField.where(name: "import_id", value: "#{external_category_id}#{import_source}").first.category_id rescue nil
|
||||
CategoryCustomField.where(
|
||||
name: "import_id",
|
||||
value: "#{external_category_id}#{import_source}"
|
||||
).first&.category_id
|
||||
end
|
||||
|
||||
def import_source
|
||||
|
||||
@@ -29,10 +29,12 @@ class InlineOneboxer
|
||||
return cached if cached.present?
|
||||
end
|
||||
|
||||
return unless url
|
||||
|
||||
if route = Discourse.route_for(url)
|
||||
if route[:controller] == "topics" &&
|
||||
route[:action] == "show" &&
|
||||
topic = (Topic.where(id: route[:topic_id].to_i).first rescue nil)
|
||||
topic = Topic.where(id: route[:topic_id].to_i).first
|
||||
|
||||
return onebox_for(url, topic.title, opts) if Guardian.new.can_see?(topic)
|
||||
end
|
||||
@@ -42,7 +44,10 @@ class InlineOneboxer
|
||||
domains = SiteSetting.inline_onebox_domains_whitelist&.split('|') unless always_allow
|
||||
|
||||
if always_allow || domains
|
||||
uri = URI(url) rescue nil
|
||||
uri = begin
|
||||
URI(url)
|
||||
rescue URI::InvalidURIError
|
||||
end
|
||||
|
||||
if uri.present? &&
|
||||
uri.hostname.present? &&
|
||||
|
||||
@@ -339,10 +339,15 @@ module SiteSettingExtension
|
||||
end
|
||||
|
||||
def get_hostname(url)
|
||||
unless (URI.parse(url).scheme rescue nil).nil?
|
||||
url = "http://#{url}" if URI.parse(url).scheme.nil?
|
||||
url = URI.parse(url).host
|
||||
uri = begin
|
||||
URI.parse(url)
|
||||
rescue URI::InvalidURIError
|
||||
end
|
||||
|
||||
unless uri.scheme.nil?
|
||||
url = uri.host
|
||||
end
|
||||
|
||||
url
|
||||
end
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ class SocketServer
|
||||
end
|
||||
|
||||
def stop
|
||||
@server&.close rescue nil
|
||||
@server&.close
|
||||
FileUtils.rm_f(@socket_path)
|
||||
@server = nil
|
||||
@blk = nil
|
||||
@@ -72,7 +72,7 @@ class SocketServer
|
||||
rescue => e
|
||||
Rails.logger.warn("Failed to handle connection in stats socket #{e}:\n#{e.backtrace.join("\n")}")
|
||||
ensure
|
||||
socket&.close rescue nil
|
||||
socket&.close
|
||||
end
|
||||
|
||||
def get_response(command)
|
||||
|
||||
@@ -110,7 +110,11 @@ class Stylesheet::Manager
|
||||
if File.exists?(stylesheet_fullpath)
|
||||
unless StylesheetCache.where(target: qualified_target, digest: digest).exists?
|
||||
begin
|
||||
source_map = File.read(source_map_fullpath) rescue nil
|
||||
source_map = begin
|
||||
File.read(source_map_fullpath)
|
||||
rescue Errno::ENOENT
|
||||
end
|
||||
|
||||
StylesheetCache.add(qualified_target, digest, File.read(stylesheet_fullpath), source_map)
|
||||
rescue => e
|
||||
Rails.logger.warn "Completely unexpected error adding contents of '#{stylesheet_fullpath}' to cache #{e}"
|
||||
|
||||
@@ -105,7 +105,7 @@ class UploadCreator
|
||||
@upload
|
||||
end
|
||||
ensure
|
||||
@file.close! rescue nil
|
||||
@file&.close
|
||||
end
|
||||
|
||||
def extract_image_info!
|
||||
@@ -149,7 +149,7 @@ class UploadCreator
|
||||
@opts[:content_type] = "image/jpeg"
|
||||
extract_image_info!
|
||||
else
|
||||
jpeg_tempfile.close! rescue nil
|
||||
jpeg_tempfile&.close
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
class UploadUrlValidator < ActiveModel::EachValidator
|
||||
def validate_each(record, attribute, value)
|
||||
if value.present?
|
||||
uri = URI.parse(value) rescue nil
|
||||
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'))
|
||||
|
||||
Reference in New Issue
Block a user