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

Add context support in Servactory exceptions #25

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
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
24 changes: 13 additions & 11 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,42 @@ PATH
datory (2.1.0)
activesupport (>= 5.1, < 8.0)
i18n (~> 1.14)
servactory (>= 2.8.0)
servactory (= 2.9.0.rc3)
terminal-table (>= 3.0)
zeitwerk (~> 2.6)

GEM
remote: https://rubygems.org/
specs:
abbrev (0.1.2)
activesupport (7.1.3.4)
activesupport (7.2.1)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
concurrent-ruby (~> 1.0, >= 1.3.1)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
logger (>= 1.4.2)
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
securerandom (>= 0.3)
tzinfo (~> 2.0, >= 2.0.5)
appraisal (2.5.0)
bundler
rake
thor (>= 0.14.0)
ast (2.4.2)
base64 (0.2.0)
bigdecimal (3.1.8)
concurrent-ruby (1.3.3)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
diff-lcs (1.5.1)
drb (2.2.1)
i18n (1.14.5)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
json (2.7.2)
language_server-protocol (3.17.0.3)
minitest (5.24.1)
mutex_m (0.2.0)
logger (1.6.1)
minitest (5.25.1)
parallel (1.24.0)
parser (3.3.0.5)
ast (~> 2.4.1)
Expand Down Expand Up @@ -93,7 +94,8 @@ GEM
rubocop-rspec_rails (2.28.3)
rubocop (~> 1.40)
ruby-progressbar (1.13.0)
servactory (2.8.0)
securerandom (0.3.1)
servactory (2.9.0.rc3)
activesupport (>= 5.1, < 8.0)
i18n (~> 1.14)
zeitwerk (~> 2.6)
Expand All @@ -103,7 +105,7 @@ GEM
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
zeitwerk (2.6.16)
zeitwerk (2.6.18)

PLATFORMS
ruby
Expand Down
2 changes: 1 addition & 1 deletion datory.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Gem::Specification.new do |spec|

spec.add_runtime_dependency "activesupport", ">= 5.1", "< 8.0"
spec.add_runtime_dependency "i18n", "~> 1.14"
spec.add_runtime_dependency "servactory", ">= 2.8.0"
spec.add_runtime_dependency "servactory", "2.9.0.rc3"
spec.add_runtime_dependency "terminal-table", ">= 3.0"
spec.add_runtime_dependency "zeitwerk", "~> 2.6"

Expand Down
17 changes: 11 additions & 6 deletions lib/datory/context/callable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@ def form(model)
end

def serialize(model) # rubocop:disable Metrics/MethodLength
context = send(:new, _datory_to_model: false)

if [Set, Array].include?(model.class)
model.map do |model_item|
serialize(model_item)
end
else
context = send(:new, _datory_to_model: false)
model = Datory::Attributes::Serialization::Model.prepare(model)
_serialize(context, model)
end
rescue Datory::Service::Exceptions::Input,
Datory::Service::Exceptions::Internal,
Datory::Service::Exceptions::Output => e
raise Datory::Exceptions::SerializationError.new(message: e.message)
raise Datory::Exceptions::SerializationError.new(context: e.send(:context), message: e.message)
end

def deserialize(data) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
context = send(:new, _datory_to_model: false)

prepared_data =
if data.is_a?(Datory::Base)
Datory::Attributes::Serialization::Model.to_hash(data)
Expand All @@ -38,19 +41,21 @@ def deserialize(data) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
deserialize(item)
end
else
context = send(:new, _datory_to_model: false)

_deserialize(context, **prepared_data)
end
rescue Datory::Service::Exceptions::Input,
Datory::Service::Exceptions::Internal,
Datory::Service::Exceptions::Output => e
raise Datory::Exceptions::DeserializationError.new(message: e.message)
raise Datory::Exceptions::DeserializationError.new(context: e.send(:context), message: e.message)
rescue JSON::ParserError => e
# TODO: Needs to be moved to I18n
message = "Failed to parse data for deserialization: #{e.message}"

raise Datory::Exceptions::DeserializationError.new(message: message, meta: { original_exception: e })
raise Datory::Exceptions::DeserializationError.new(
context: context,
message: message,
meta: { original_exception: e }
)
end

def new(_datory_to_model: true, **attributes) # rubocop:disable Lint/UnderscorePrefixedVariableName
Expand Down
13 changes: 13 additions & 0 deletions lib/datory/context/workspace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
module Datory
module Context
module Workspace
class Actor
attr_reader :class_name

def initialize(context)
@class_name = context.class.name
end
end

private

def merge!(attributes)
Expand Down Expand Up @@ -46,6 +54,11 @@ def serialize(**); end
def deserialize(**); end

def to_model(**); end

# NOTE: To maintain context compatibility in Servactory exceptions.
def servactory_service_info
@servactory_service_info ||= self.class::Actor.new(self)
end
end
end
end
3 changes: 3 additions & 0 deletions spec/examples/usual/example2/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,9 @@
price_currency: "USD",
quantity: 5
}.to_json

# EXAMPLE: To test exception.
# "invalid-json"
end

it_behaves_like "successful results"
Expand Down
Loading