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

Allow Success and Failure to have more than one symbol #39

Closed
bvicenzo opened this issue Dec 8, 2022 · 1 comment
Closed

Allow Success and Failure to have more than one symbol #39

bvicenzo opened this issue Dec 8, 2022 · 1 comment
Assignees

Comments

@bvicenzo
Copy link
Contributor

bvicenzo commented Dec 8, 2022

In exceptions, we can catch or a single error, or a family of errors, using its superclass...

Ex:

ClientError = Class.new(RuntimeError)
UnprocessableEntity = Class.new(ClientError)
Conflict = Class.new(ClientError)

def foo
  create_user
  rescue UnprocessableEntity => error
    "User can not be created fix errors and try again #{error.message}"
  rescue Conflict
    "User already exist."
  rescue ClientError => error
    Sentry.capture_exception("Some unexpected client error happened: #{error.message}")
    "Unknow error happened, please contact system administration"
end

Nowadays it is impossible in FService, because we can create a result using just one symbol....
If we are able to put more than one symbol, we could, for example, pass a specific symbol and a more generic one.

Ex:

class User::Create < FService::Base
  attribute :attr

  def run
    response = create_user(attrs)
    return Success(:ok, :created, data: response.body) if response.status == 200

    if response.status.between?(400, 499)
      error_name = client_error_name(response.status)
      if error_name.error_name.present?
        Failure(:unprocessable_entity, :client_error, data: response.body)
      else
        Failure(:client_error, data: response.body)
      end
    end
  end

  private

  def client_error_name(status)
    return :unprocessable_entity if status = 422
    return :conflict if status = 409
  end
end

And the usage would be:

User::Create(attrs: { name: 'Joe' })
  .on_success(:created) { return 'User created' }
  .on_failure(:unprocessable_entity) { |error| return "User can not be created fix errors and try again #{error}" }
  .on_failure(:conflict) { return "User already exist." }
  .on_failure(:client_error) do
    Sentry.capture_exception("Some unexpected client error happened: #{error.message}")
    return "Unknow error happened, please contact system administration"
  end
@bvicenzo
Copy link
Contributor Author

Solved by: #41

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant