package Tak::Role::Service;

use Moo::Role;

sub start_request {
  my ($self, $req, $type, @args) = @_;
  unless ($type) {
    $req->mistake(request_type => "No request type given");
    return;
  }
  if (my $meth = $self->can("handle_${type}")) {
    my @result;
    if (eval { @result = $self->$meth(@args); 1 }) {
      $req->success(@result);
    } else {
      if (ref($@) eq 'ARRAY') {
        $req->result(@{$@});
      } else {
        $req->failure(exception => $@);
      }
    }
  } else {
    $req->mistake(request_type => "Unknown request type ${type}");
  }
}

sub receive { }

1;
