Store failure exception with cause on result#220
Conversation
92040c3 to
582fd62
Compare
582fd62 to
0a437cf
Compare
sunny
left a comment
There was a problem hiding this comment.
Looks good! Before we merge this, can you explain a bit how you are patching ServiceActor::Failable?
|
This seems to be an incompatible change. For instance, it breaks actors with something like begin
rescue ExceptionClass => e
fail! exception: e
endwithout the patch, the result would have |
Sure! It's basically what is in this PR. Without this change the exception is lost. module ServiceActor::Failable::PrependedMethods
def _call
super
rescue *self.class.fail_ons => e
fail!(error: e.message, exception: e)
end
endThe exception is then available at For context, here's a simplified Rails example demonstrating how this is useful. class UpdatePost < ApplicationActor
input :id
input :attributes
fail_on ActiveRecord::RecordInvalid
def call
Post.find(id).update!(attributes)
end
end
class PostsController < ApplicationController
def update
result = UpdatePost.result(id: params[:id], attributes: post_params)
if result.success?
head :ok
elsif result[:exception].is_a?(ActiveRecord::RecordInvalid)
render json: { errors: result[:exception].record.errors.full_messages }, status: :unprocessable_content
else
render json: { error: 'Could not update post' }, status: :bad_request
end
end
end |
True! I guess the thing to decide is whether to:
|
Perhaps we could just emit a warning first? we used to follow this way e.g. here: #138 |
Another way to lower the surface of the incompatible change would be to only fill the |
Hello! 👋
The goal of this change is to make raised exceptions available to the caller of
Actor#result. Additionally, exceptions rescued withfail_onare captured as thecauseof the configured failure class.I'm not too attached to the implementation here, so I'd appreciate your thoughts! But I found this necessary for some work I'm doing and had to patch
ServiceActor::Failable.