From c1ae214c7bb48f8baea7635dc165467cc65aa20d Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Fri, 21 Jan 2022 13:21:13 +1000 Subject: [PATCH] DEV: Add more info to N1 custom field error (#15670) This commit makes a more specific N1NotPreLoadedError from StandardError to raise when a custom field is loaded before being preloaded, so it is easier to test that this does not happen from plugins. Also adds the name of the class trying to load the custom field to the error message. --- app/models/concerns/has_custom_fields.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/models/concerns/has_custom_fields.rb b/app/models/concerns/has_custom_fields.rb index f073544dbb1..12b404b96e2 100644 --- a/app/models/concerns/has_custom_fields.rb +++ b/app/models/concerns/has_custom_fields.rb @@ -160,9 +160,11 @@ module HasCustomFields @custom_fields_orig = nil end + class NotPreloadedError < StandardError; end class PreloadedProxy - def initialize(preloaded) + def initialize(preloaded, klass_with_custom_fields) @preloaded = preloaded + @klass_with_custom_fields = klass_with_custom_fields end def [](key) @@ -170,14 +172,14 @@ module HasCustomFields @preloaded[key] else # for now you can not mix preload an non preload, it better just to fail - raise StandardError, "Attempted to access the non preloaded custom field '#{key}'. This is disallowed to prevent N+1 queries." + raise NotPreloadedError, "Attempted to access the non preloaded custom field '#{key}' on the '#{@klass_with_custom_fields}' class. This is disallowed to prevent N+1 queries." end end end def custom_fields if @preloaded_custom_fields - return @preloaded_proxy ||= PreloadedProxy.new(@preloaded_custom_fields) + return @preloaded_proxy ||= PreloadedProxy.new(@preloaded_custom_fields, self.class.to_s) end @custom_fields ||= refresh_custom_fields_from_db.dup