-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Reactor.Dsl.Flunk): Add a special step type which always fails. (#…
…125) This is especially useful for switch branches which should never be reached.
- Loading branch information
Showing
10 changed files
with
466 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
defmodule Reactor.Dsl.Flunk do | ||
@moduledoc """ | ||
The struct used to store flunk DSL entities. | ||
See `d:Reactor.flunk`. | ||
""" | ||
|
||
alias Reactor.{Dsl.Argument, Dsl.Build, Dsl.Flunk, Dsl.WaitFor, Step, Template} | ||
|
||
defstruct __identifier__: nil, | ||
arguments: [], | ||
name: nil, | ||
message: nil | ||
|
||
@type t :: %Flunk{ | ||
__identifier__: any, | ||
arguments: [Argument.t()], | ||
message: Template.t(), | ||
name: atom | ||
} | ||
|
||
@doc false | ||
def __entity__, | ||
do: %Spark.Dsl.Entity{ | ||
name: :flunk, | ||
describe: """ | ||
Creates a step which will always cause the Reactor to exit with an error. | ||
This step will flunk with a `Reactor.Error.Invalid.ForcedFailureError` with it's message set to the provided message. | ||
Additionally, any arguments to the step will be stored in the exception under the `arguments` key. | ||
""", | ||
examples: [ | ||
""" | ||
flunk :outaroad, "Ran out of road before reaching 88Mph" | ||
""" | ||
], | ||
args: [:name, :message], | ||
target: __MODULE__, | ||
entities: [arguments: [Argument.__entity__(), WaitFor.__entity__()]], | ||
recursive_as: :steps, | ||
schema: [ | ||
name: [ | ||
type: :atom, | ||
required: true, | ||
doc: """ | ||
A unique name for the step. Used when choosing the return value of the Reactor and for arguments into other steps. | ||
""" | ||
], | ||
message: [ | ||
type: {:or, [nil, :string, Template.type()]}, | ||
required: false, | ||
default: nil, | ||
doc: """ | ||
The message to to attach to the exception. | ||
""" | ||
] | ||
] | ||
} | ||
|
||
defimpl Build do | ||
require Reactor.Template | ||
alias Reactor.{Argument, Builder, Step} | ||
import Reactor.Utils | ||
|
||
def build(flunk, reactor) do | ||
with {:ok, reactor} <- | ||
Builder.add_step( | ||
reactor, | ||
{flunk.name, :arguments}, | ||
Step.ReturnAllArguments, | ||
flunk.arguments, | ||
async?: true, | ||
max_retries: 1, | ||
ref: :step_name | ||
) do | ||
arguments = | ||
[Argument.from_result(:arguments, {flunk.name, :arguments})] | ||
|> maybe_append(message_argument(flunk)) | ||
|
||
Builder.add_step(reactor, flunk.name, Step.Fail, arguments, | ||
max_retries: 0, | ||
ref: :step_name | ||
) | ||
end | ||
end | ||
|
||
defp message_argument(flunk) when is_binary(flunk.message), | ||
do: Argument.from_value(:message, flunk.message) | ||
|
||
defp message_argument(flunk) when Template.is_template(flunk.message), | ||
do: Argument.from_template(:message, flunk.message) | ||
|
||
defp message_argument(flunk) when is_nil(flunk.message), do: nil | ||
|
||
def verify(_, _), do: :ok | ||
def transform(_, dsl_state), do: {:ok, dsl_state} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule Reactor.Error.Invalid.ForcedFailureError do | ||
@moduledoc """ | ||
This error is returned when the `flunk` DSL entity or the `Reactor.Step.Fail` | ||
step are called. | ||
""" | ||
|
||
use Reactor.Error, | ||
fields: [:arguments, :step_name, :message, :context, :options], | ||
class: :invalid | ||
|
||
@type t :: %__MODULE__{ | ||
__exception__: true, | ||
arguments: %{atom => any}, | ||
step_name: any, | ||
message: String.t(), | ||
context: map, | ||
options: keyword | ||
} | ||
|
||
@doc false | ||
@impl true | ||
def message(error), do: error.message | ||
end |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
defmodule Reactor.Step.Fail do | ||
@moduledoc """ | ||
A very simple step which immediately returns an error. | ||
""" | ||
|
||
use Reactor.Step | ||
alias Reactor.Error.Invalid.ForcedFailureError | ||
|
||
@doc false | ||
@impl true | ||
@spec run(Reactor.inputs(), Reactor.context(), keyword) :: {:error, ForcedFailureError.t()} | ||
def run(arguments, context, options) do | ||
{:error, | ||
ForcedFailureError.exception( | ||
arguments: arguments.arguments, | ||
message: arguments.message, | ||
context: context, | ||
options: options, | ||
step_name: context.current_step.name | ||
)} | ||
end | ||
|
||
@doc false | ||
@impl true | ||
@spec can?(Reactor.Step.t(), Reactor.Step.capability()) :: boolean | ||
def can?(_step, _capability), do: false | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule Reactor.Dsl.FailTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias Reactor.{Error, Error.Invalid.ForcedFailureError} | ||
|
||
defmodule FailReactor do | ||
@moduledoc false | ||
use Reactor | ||
|
||
flunk(:flunk, "Fail") | ||
end | ||
|
||
test "it returns an forced failure error" do | ||
assert {:error, error} = Reactor.run(FailReactor, []) | ||
|
||
assert is_exception(error) | ||
|
||
assert {:ok, error} = Error.fetch_error(error, ForcedFailureError) | ||
assert error.message == "Fail" | ||
assert error.arguments == %{} | ||
assert error.step_name == :flunk | ||
end | ||
end |
Oops, something went wrong.