Skip to content
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

Change error class from TypeError to TypeStruct::MultiTypeError on from_hash with other object than Hash #16

Conversation

okumud
Copy link
Contributor

@okumud okumud commented Apr 12, 2024

I have suggestions

This PR proposes changing the exception raised in the TypeStruct#from_hash method from TypeError to TypeStruct::MultiTypeError when the input is not a Hash object.

Background

The TypeStruct#from_hash method is used to construct a TypeStruct from a Hash object. In scenarios such as with Ruby on Rails controllers handling invalid JSON requests, the current implementation raises a TypeError when a non-Hash object is passed. This TypeError is widely used across different contexts.

Example

Current behavior in Ruby on Rails:

# app/controllers/application_controller.rb

class ApplicationController < ActionController::Base

  # Error handling TypeError <= This may receive errors due to other factors.
  rescue_from TypeError do |e|
    # Customize the error response as needed
    render status: :unprocessable_entity,
           json: { error: 'Invalid body error', message: e.message }
  end

  # ...

  def update
    TitleParser = TypeStruct.new(title: String)
    ContentParser = TypeStruct.new(content: TitleParser)

    request_hash = JSON.parse(request.body.read)
    # => {content: [{title: 'hoge'}]} # `content` is should be an object, but an array
    ContentParser.from_hash(request_hash) # => TypeError
    # ...
  end

  # ...
end

Solution

By changing TypeStruct::MultiTypeError, we can provide more specific error messages that detail the nature of the type mismatch, improving error traceability and specificity. This enhancement aids developers in pinpointing the exact nature of the issue, making debugging more straightforward and enhancing overall code maintainability.

Proposed changes in error handling:

# app/controllers/application_controller.rb

class ApplicationController < ActionController::Base

  # Example of using rescue_from with TypeStruct::MultiTypeError
  rescue_from TypeStruct::MultiTypeError do |e|
    # Customize the error response as needed
    render status: :unprocessable_entity,
           json: { error: 'Invalid body error', message: e.message }
  end

  # ...
end

Note

I believe this improvement will significantly benefit our project by making error handling more intuitive and debugging processes more efficient. If you agree with these changes, I kindly ask for your review and approval to merge this pull request.

Thank you for considering this enhancement.

Copy link
Owner

@ksss ksss left a comment

Choose a reason for hiding this comment

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

WHAT A GREAT!!!
THANKS!!!!!

@ksss ksss merged commit 7f2ce3c into ksss:master Apr 14, 2024
3 checks passed
@ksss
Copy link
Owner

ksss commented Apr 14, 2024

@okumud
v1.1.0 has been released. Please enjoy ✨

https://rubygems.org/gems/type_struct/versions/1.1.0
https://github.com/ksss/type_struct/releases/tag/v1.1.0

@okumud
Copy link
Contributor Author

okumud commented Apr 14, 2024

@ksss
I'll try to install it in this week.
Thank you all for the swift review and releasing!

Edited(2024-04-22):
-> It works fine!! Thankyou!

@okumud okumud deleted the change-multitype-error-for-from_hash_with_other_object branch April 15, 2024 01:49
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.

Improve TypeError identification by defining a custom error class
2 participants