package com.atlassian.bitbucket.mesh.git.exception;

import com.atlassian.bitbucket.mesh.rpc.v1.RpcGitError;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcInvalidRebase;

import javax.annotation.Nonnull;

import static com.atlassian.bitbucket.mesh.rpc.v1.RpcInvalidRebase.Reason;
import static io.grpc.Status.INVALID_ARGUMENT;
import static java.util.Objects.requireNonNull;

public class InvalidRebaseException extends AbstractGitException {

    private final Reason reason;

    public InvalidRebaseException(@Nonnull Reason reason) {
        super("Invalid rebase: " + reason, INVALID_ARGUMENT);

        this.reason = requireNonNull(reason, "reason");
    }

    @Nonnull
    public Reason getReason() {
        return reason;
    }

    @Nonnull
    @Override
    public RpcGitError toError() {
        return RpcGitError.newBuilder()
                .setInvalidRebase(RpcInvalidRebase.newBuilder()
                        .setReason(reason))
                .build();
    }
}
