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

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

import javax.annotation.Nonnull;

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

public class InvalidMergeException extends AbstractGitException {

    private final Reason reason;

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

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

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

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