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

Simplifies error serializer, reduces complex logic #136

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions app/serializers/error_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@

class ErrorSerializer
def self.serialize(error)
error_hash = serialize_argument_error(error) if error.class == ArgumentError
error_hash = serialize_record_not_found_error(error) if error.class == ActiveRecord::RecordNotFound
error_hash = serialize_visit_not_allowed_error(error) if error.class == GroundGame::VisitNotAllowed
error_hash = serialize_invalid_best_canvass_response_error(error) if error.class == GroundGame::InvalidBestCanvassResponse
error_hash = serialize_easypost_error(error) if error.class == EasyPost::Error
error_hash = serialize_address_unmatched_error(error) if error.class == GroundGame::AddressUnmatched
error_hash = serialize_facebook_authentication_error(error) if error.class == Koala::Facebook::AuthenticationError
error_hash = serialize_doorkeeper_oauth_invalid_token_response(error) if error.class == Doorkeeper::OAuth::InvalidTokenResponse
error_hash = serialize_doorkeeper_oauth_error_response(error) if error.class == Doorkeeper::OAuth::ErrorResponse
error_hash = serialize_validation_errors(error) if error.class == ActiveModel::Errors
error_hash = send(serializer_mapping[error.class], error)

{ errors: Array.wrap(error_hash) }
end

private
def self.serializer_mapping
{
ArgumentError => :serialize_argument_error,
ActiveRecord::RecordNotFound => :serialize_record_not_found_error,
GroundGame::VisitNotAllowed => :serialize_visit_not_allowed_error,
GroundGame::InvalidBestCanvassResponse => :serialize_invalid_best_canvass_response_error,
EasyPost::Error => :serialize_easypost_error,
GroundGame::AddressUnmatched => :serialize_address_unmatched_error,
Koala::Facebook::AuthenticationError => :serialize_facebook_authentication_error,
Doorkeeper::OAuth::InvalidTokenResponse => :serialize_doorkeeper_oauth_invalid_token_response,
Doorkeeper::OAuth::ErrorResponse => :serialize_doorkeeper_oauth_error_response,
ActiveModel::Errors => :serialize_validation_errors,
}
end

def self.serialize_argument_error(error)
{
Expand Down