Skip to content

A platform for community discussion. Free, open, simple.

DEV: Improve error logging for external uploads and URL validation (#38910)

Alan Guo Xiang Tan46b4223fe54f
diff --git a/lib/external_upload_helpers.rb b/lib/external_upload_helpers.rb
index 75a5ee368da..f4d90f4c699 100644
--- a/lib/external_upload_helpers.rb
+++ b/lib/external_upload_helpers.rb
@@ -334,9 +334,8 @@ module ExternalUploadHelpers
status: 422,
)
rescue => err
- Discourse.warn_exception(
- err,
- message: "Complete external upload failed unexpectedly for user #{current_user.id}",
+ Rails.logger.error(
+ "Complete external upload failed unexpectedly for user #{current_user.id}: #{err.class} #{err.message}\n#{err.backtrace.join("\n")}",
)
render_json_error(I18n.t("upload.failed"), status: 422)
diff --git a/lib/url_helper.rb b/lib/url_helper.rb
index a15da00b6bc..b24c01f10f5 100644
--- a/lib/url_helper.rb
+++ b/lib/url_helper.rb
@@ -93,7 +93,9 @@ class UrlHelper
url = uri.to_s
if url.length > MAX_URL_LENGTH
- raise ArgumentError.new("URL starting with #{url[0..100]} is too long")
+ raise ArgumentError.new(
+ "URL is too long (#{url.length} characters): #{url.truncate(MAX_URL_LENGTH)}",
+ )
end
# Ideally we will jump straight to `Addressable::URI.normalized_encode`. However,
diff --git a/spec/lib/url_helper_spec.rb b/spec/lib/url_helper_spec.rb
index bf36e0c3de5..c4b5fdadd12 100644
--- a/spec/lib/url_helper_spec.rb
+++ b/spec/lib/url_helper_spec.rb
@@ -169,7 +169,7 @@ RSpec.describe UrlHelper do
long_url = "https://#{"a" * 2_000}.com"
expect do UrlHelper.normalized_encode(long_url) end.to raise_error(
ArgumentError,
- "URL starting with #{long_url[0..100]} is too long",
+ "URL is too long (#{long_url.length} characters): #{long_url.truncate(UrlHelper::MAX_URL_LENGTH)}",
)
end
end