All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.reactor.exception.I18nSupportException Maven / Gradle / Ivy

The newest version!
package org.zodiac.reactor.exception;

import java.util.Locale;

import org.zodiac.reactor.util.SpringLocaleUtil;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;

import reactor.core.publisher.Mono;

public class I18nSupportException extends TraceSourceException {

    private static final long serialVersionUID = -1641306092980520423L;

    /*Message code, the key defined in the message.properties file.*/
    private String i18nCode;

    /*Message parameters.*/
    private Object[] args;

    protected I18nSupportException() {
    }

    public I18nSupportException(String code, Object... args) {
        super(code);
        this.i18nCode = code;
        this.args = args;
    }

    public I18nSupportException(String code, Throwable cause, Object... args) {
        super(code, cause);
        this.args = args;
        this.i18nCode = code;
    }

    public String getOriginalMessage() {
        return super.getMessage() != null ? super.getMessage() : getI18nCode();
    }

    @Override
    public String getMessage() {
        return getLocalizedMessage();
    }

    @Override
    public final String getLocalizedMessage() {
        return getLocalizedMessage(SpringLocaleUtil.current());
    }

    public String getLocalizedMessage(Locale locale) {
        return SpringLocaleUtil.resolveMessage(i18nCode, locale, getOriginalMessage(), args);
    }

    public final Mono getLocalizedMessageReactive() {
        return SpringLocaleUtil.currentReactive().map(this::getLocalizedMessage);
    }

    public String getI18nCode() {
        return i18nCode;
    }

    public Object[] getArgs() {
        return args;
    }

    protected I18nSupportException setI18nCode(String i18nCode) {
        this.i18nCode = i18nCode;
        return this;
    }

    protected I18nSupportException setArgs(Object[] args) {
        this.args = args;
        return this;
    }

    public static String tryGetLocalizedMessage(Throwable error, Locale locale) {
        if (error instanceof I18nSupportException) {
            return ((I18nSupportException) error).getLocalizedMessage(locale);
        }
        String msg = error.getMessage();

        if (StrUtil.isBlank(msg)) {
            msg = "error." + error.getClass().getSimpleName();
        }
        if (msg.contains(".")) {
            return SpringLocaleUtil.resolveMessage(msg, locale, msg);
        }
        return msg;
    }

    public static String tryGetLocalizedMessage(Throwable error) {
        return tryGetLocalizedMessage(error, SpringLocaleUtil.current());
    }

    public static Mono tryGetLocalizedMessageReactive(Throwable error) {
        return SpringLocaleUtil.currentReactive().map(locale -> tryGetLocalizedMessage(error, locale));
    }

    /**
     * Exception that do not populate the thread stack, in some cases that are not sensitive to the thread stack and are uncontrollable for exceptions (e.g., exceptions from unauthenticated requests) are good for performance.
     */
    public static class NoStackTrace extends I18nSupportException {

        private static final long serialVersionUID = 5522502116432884262L;

        public NoStackTrace(String code, Object... args) {
            super(code, args);
        }

        public NoStackTrace(String code, Throwable cause, Object... args) {
            super(code, cause, args);
        }

        @Override
        public final synchronized Throwable fillInStackTrace() {
            return this;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy