package com.zb.mom.ww.mxgateway.client;

import mxaccess_gateway.v1.MxaccessGateway.ProtocolStatus;

/**
 * Thrown when the gateway reports a worker-side protocol failure such as
 * {@code WORKER_UNAVAILABLE} or {@code PROTOCOL_VIOLATION}.
 */
public final class MxGatewayWorkerException extends MxGatewayException {
    private final ProtocolStatus protocolStatus;

    /**
     * Creates a new worker exception from a protocol status.
     *
     * @param operation human-readable name of the failing operation
     * @param protocolStatus protocol status returned by the gateway
     */
    public MxGatewayWorkerException(String operation, ProtocolStatus protocolStatus) {
        super(MxGatewayErrors.protocolStatusMessage(operation, protocolStatus));
        this.protocolStatus = protocolStatus;
    }

    /**
     * Creates a new worker exception with an already-built, verbatim message.
     * Used to re-surface a failure with a redacted message while preserving the
     * (already scrubbed) protocol status.
     *
     * @param message the exact message to surface (already formatted/redacted)
     * @param protocolStatus protocol status returned by the gateway
     * @param cause underlying error, or {@code null}
     */
    protected MxGatewayWorkerException(String message, ProtocolStatus protocolStatus, Throwable cause) {
        super(message, cause);
        this.protocolStatus = protocolStatus;
    }

    /**
     * Returns the gateway protocol status that triggered this exception.
     *
     * @return the protocol status, or {@code null} if none was supplied
     */
    public ProtocolStatus protocolStatus() {
        return protocolStatus;
    }
}
