View Source EctoCommand.Middleware behaviour (EctoCommand v0.1.0)
Middleware provides an extension point to add functions that you want to be called for every command execution
Implement the EctoCommand.Middleware
behaviour in your module and define the
before_execution/2
, after_execution/2
, after_failure/2
and invalid/2
callback functions.
example-middleware
Example middleware
defmodule SampleMiddleware do
@behaviour EctoCommand.Middleware
@impl true
def before_execution(pipeline, _opts) do
pipeline
|> Pipeline.assign(:some_data, :some_value)
|> Pipeline.update!(:command, fn command -> %{command | name: "updated-name"} end)
end
def after_execution(pipeline, _opts) do
Logger.debug("Command executed successfully", command: pipeline.command, result: Pipeline.response(pipeline))
pipeline
end
def after_failure(pipeline, _opts) do
Logger.error("Command execution fails", command: pipeline.command, error: Pipeline.response(pipeline))
pipeline
end
def invalid(pipeline, _opts) do
Logger.error("invalid params received", params: pipeline.params, error: Pipeline.response(pipeline))
pipeline
end
end