package org.xbib.logging.errormanager;

import java.util.logging.Handler;
import org.xbib.logging.ExtErrorManager;

/**
 * An error manager which publishes errors to a handler.
 */
public final class HandlerErrorManager extends ExtErrorManager {

    private static final ThreadLocal<Boolean> handlingError = new ThreadLocal<>();

    private final Handler handler;

    /**
     * Construct a new instance.
     *
     * @param handler the handler to set (must not be {@code null})
     */
    public HandlerErrorManager(final Handler handler) {
        this.handler = handler;
    }

    @Override
    public void error(final String msg, final Exception ex, final int code) {
        if (handlingError.get() != Boolean.TRUE) {
            handlingError.set(Boolean.TRUE);
            try {
                handler.publish(errorToLogRecord(msg, ex, code));
            } finally {
                handlingError.remove();
            }
        }
    }
}
