package cn.palmte.work;
import org.springframework.http.HttpStatus;
import org.springframework.util.Assert;
import java.util.function.Supplier;
/**
* @author Harry Yang
* @since 2.0 2022/12/30 15:25
*/
public class ErrorMessageException extends NoStackTraceRuntimeException {
private final HttpStatus status;
public ErrorMessageException(/*@Nullable*/ String msg) {
this(msg, null, HttpStatus.BAD_REQUEST);
}
public ErrorMessageException(/*@Nullable*/ String msg, /*@Nullable*/ Throwable cause, HttpStatus status) {
super(msg, cause);
Assert.notNull(status, "http status is required");
this.status = status;
}
public HttpStatus getStatus() {
return status;
}
public static ErrorMessageException failed(String message) {
return new ErrorMessageException(message);
}
public static ErrorMessageException failed(String message, HttpStatus status) {
return new ErrorMessageException(message, null, status);
}
public static void notNull(Object obj, String message) {
if (obj == null) {
throw ErrorMessageException.failed(message, HttpStatus.NOT_FOUND);
}
}
public static void notNull(Object obj, Supplier supplier) {
if (obj == null) {
throw new ErrorMessageException(supplier.get());
}
}
}