Skip to content

Undefined SerializationTypeMismatch error #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

mnishiguchi
Copy link
Contributor

@mnishiguchi mnishiguchi commented Feb 2, 2022

Description

Currently GigoCoder::SerializationTypeMismatch is not defined, which causes the following error in the gem user app:

NameError: uninitialized constant GIGO::ActiveRecord::Base::GigoCoder::SerializationTypeMismatch

The error message is right. We need to define our custom error before using it.

def load(yaml)
return klass.new if yaml.nil?
Encoding.default_internal = GIGO.encoding
value = YAML.load(GIGO.load(yaml))
unless value.is_a?(klass)
raise SerializationTypeMismatch, "Attribute was supposed to be a #{klass.to_s}, but was a #{hash.class}."
end
value
ensure
Encoding.default_internal = @default_internal
end
def dump(value)
return klass.new.to_yaml if value.nil?
unless value.is_a?(klass)
raise SerializationTypeMismatch, "Attribute was supposed to be a #{klass.to_s}, but was a #{value.class}."
end
value.to_yaml
end

Related PR: #3

Changes

  • Define GigoCoder::SerializationTypeMismatch Use fully-qualified error name ::ActiveRecord::SerializationTypeMismatch
  • Correct the error message: hash is probably a typo, meant to be value
  • Show the value in the error message

Notes

  • Alternatively we could make the error be a generic RuntimeError
  • This bug was discovered in our Rails Backend Rails 5.0 QA.
  • It would be nice to be able to see actual value in the error message for debuggability

gigo error message improved Screen Shot 2022-02-02 at 3 25 32 PM

@mnishiguchi mnishiguchi requested review from a team and metaskills February 2, 2022 19:56
@mnishiguchi mnishiguchi marked this pull request as ready for review February 2, 2022 19:56
Copy link
Contributor

@metaskills metaskills left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the intent here was that we had implicit access to the ActiveRecord::SerializationTypeMismatch because this is an ActiveRecord gem. So if we are not getting into that scope correctly because of the way we have authored the gem or using it in a certain way, perhaps we should change the rails to ::ActiveRecord::SerializationTypeMismatch instead?

@metaskills
Copy link
Contributor

This is the error we were trying to raise. https://api.rubyonrails.org/classes/ActiveRecord/SerializationTypeMismatch.html It is old as dirt and consistent between Rails versions and meant to be the one to communicate a serialization error.

@mnishiguchi mnishiguchi changed the title Define currently undefined GigoCoder::SerializationTypeMismatch error Undefined SerializationTypeMismatch error Feb 2, 2022
@mnishiguchi mnishiguchi merged commit e935047 into master Feb 3, 2022
@mnishiguchi mnishiguchi deleted the mnishiguchi/undefined-serialization-type-mismatch-error branch February 3, 2022 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants