package com.atlassian.bitbucket.mesh.auth;

import com.atlassian.bitbucket.mesh.AbstractStatusException;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcError;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcErrorUtils;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcPermissionDenied;
import io.grpc.Metadata;
import io.grpc.Status;

import javax.annotation.Nullable;

/**
 * Exception that is thrown when the caller does not have sufficient permissions to perform the requested operation.
 *
 * @since 1.4
 */
public class AuthorisationException extends AbstractStatusException {

    public AuthorisationException(@Nullable String message) {
        super(message, Status.PERMISSION_DENIED);
    }

    @Override
    protected Metadata getTrailers() {
        return RpcErrorUtils.toMetadata(RpcError.newBuilder()
                .setPermissionDenied(RpcPermissionDenied.getDefaultInstance())
                .build());
    }
}
