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 helpful error message when not returning a Dry::Monads::Result from step #16

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
7 changes: 6 additions & 1 deletion lib/dry/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "zeitwerk"
require "dry/monads"
require "dry/operation/errors"

module Dry
# DSL for chaining operations that can fail
Expand Down Expand Up @@ -136,7 +137,11 @@ def steps(&block)
# @return [Object] wrapped value
# @see #steps
def step(result)
result.value_or { throw_failure(result) }
if result.is_a?(Dry::Monads::Result)
result.value_or { throw_failure(result) }
else
raise InvalidStepResultError.new(result: result)
end
end

# Invokes a callable in case of block's failure
Expand Down
17 changes: 14 additions & 3 deletions lib/dry/operation/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

module Dry
class Operation
class Error < ::StandardError; end

# Methods to prepend have already been defined
class MethodsToPrependAlreadyDefinedError < ::StandardError
class MethodsToPrependAlreadyDefinedError < Error
def initialize(methods:)
super <<~MSG
'.operate_on' must be called before the given methods are defined.
Expand All @@ -13,7 +15,7 @@ def initialize(methods:)
end

# Configuring prepending after a method has already been prepended
class PrependConfigurationError < ::StandardError
class PrependConfigurationError < Error
def initialize(methods:)
super <<~MSG
'.operate_on' and '.skip_prepending' can't be called after any methods\
Expand All @@ -24,7 +26,7 @@ def initialize(methods:)
end

# Missing dependency required by an extension
class MissingDependencyError < ::StandardError
class MissingDependencyError < Error
def initialize(gem:, extension:)
super <<~MSG
To use the #{extension} extension, you first need to install the \
Expand All @@ -33,6 +35,15 @@ def initialize(gem:, extension:)
end
end

class InvalidStepResultError < Error
def initialize(result:)
super <<~MSG
Your step must return `Success(..)` or `Failure(..)`, \
from `Dry::Monads::Result`. Instead, it was `#{result.inspect}`.
MSG
end
end

# An error related to an extension
class ExtensionError < ::StandardError; end
end
Expand Down
12 changes: 12 additions & 0 deletions spec/unit/operation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ def foo(value)
described_class.new.step(failure)
}.to throw_symbol(:halt, failure)
end

it "raises helpful error when returning something that is not a Result" do
expect {
described_class.new.step(123)
}.to raise_error(Dry::Operation::InvalidStepResultError)
.with_message(
<<~MSG
Your step must return `Success(..)` or `Failure(..)`, \
from `Dry::Monads::Result`. Instead, it was `123`.
MSG
)
end
end

describe "#intercepting_failure" do
Expand Down
Loading