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

win.doyto.query.web.response.ErrorCode Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package win.doyto.query.web.response;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * ErrorCode
 *
 * @author f0rb on 2017-03-05.
 */
public interface ErrorCode extends Serializable {

    Integer getCode();

    String getMessage();

    default void addError(String fieldName, String message) {
    }

    default boolean isSuccess() {
        return Integer.valueOf(0).equals(getCode());
    }

    static  JsonResponse build(D data) {
        return new JsonResponse().setData(data);
    }

    static ErrorCode build(ErrorCode errorCode) {
        return build(errorCode.getCode(), errorCode.getMessage());
    }

    static ErrorCode build(String message) {
        return build(-1, message);
    }

    static ErrorCode build(Integer code, String message) {
        return new JsonResponse<>(code, message);
    }

    static void assertNotNull(Object target, ErrorCode errorCode, Object... messages) {
        assertFalse(target == null, errorCode, messages);
    }

    static void assertTrue(boolean condition, ErrorCode errorCode, Object... messages) {
        assertFalse(!condition, errorCode, messages);
    }

    static void assertFalse(boolean condition, ErrorCode errorCode, Object... messages) {
        if (condition) {
            fail(errorCode, messages);
        }
    }

    static void fail(ErrorCode errorCode, Object... messages) {
        Logger logger = LoggerFactory.getLogger(ErrorCode.class);
        if (logger.isWarnEnabled()) {
            logger.warn("[{}]{}{}", errorCode.getCode(), errorCode.getMessage(),
                        messages.length == 0 ? "" : ": " + StringUtils.join(messages, ", "));
        }
        throw new ErrorCodeException(errorCode);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy