Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Sample controller
- def create
- ctx = Pipes::Context.new(…..)
- flow.call(ctx)
- render json: ctx.project, status: :ok
- # no need for handling specific exceptions of multiple types
- resuce Pipes::Error => e
- render json: {errors: e.message}
- end
- class SaveProject
- def call
- ctx.project.save!
- end
- rescue RecordInvalid => e
- # hide the internals of record invalid for the public pipes api
- ctx.fail(e, e.record.errors.full_messages)
- end
- end
- class Pipes::Context
- attr_reader :error
- def fail(error)
- @error = error
- fail Pipes::Error
- end
- def success?
- @error.nil?
- def error?
- !success?
- end
- end
- # May not be the best idea, but
- # returning a boolean and then accessing ctx.error instead of raising
- # an exception for controlling the flow sounds good to me
- module Pipe
- def method_missing
- rescue Pipes::Error
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement