win.doyto.query.web.response.ErrorCode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of doyto-query-web Show documentation
Show all versions of doyto-query-web Show documentation
The web layer integrate with Spring Web
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);
}
}