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.
This commit is contained in:
Martin Brennan 2022-01-21 13:21:13 +10:00 committed by GitHub
parent e4e37257cc
commit c1ae214c7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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