cn.hippo4j.common.web.base.Results Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.hippo4j.common.web.base;
import cn.hippo4j.common.web.exception.AbstractException;
import cn.hippo4j.common.web.exception.ErrorCode;
import cn.hippo4j.common.web.exception.ErrorCodeEnum;
import java.util.Optional;
/**
* Results.
*/
public final class Results {
public static Result success() {
return new Result()
.setCode(Result.SUCCESS_CODE);
}
public static Result success(T data) {
return new Result()
.setCode(Result.SUCCESS_CODE)
.setData(data);
}
public static Result failure() {
return failure(ErrorCodeEnum.SERVICE_ERROR.getCode(), ErrorCodeEnum.SERVICE_ERROR.getMessage());
}
public static Result failure(AbstractException abstractException) {
String errorCode = Optional.ofNullable(abstractException.getErrorCode())
.map(ErrorCode::getCode)
.orElse(ErrorCodeEnum.SERVICE_ERROR.getCode());
return new Result().setCode(errorCode)
.setMessage(abstractException.getMessage());
}
public static Result failure(Throwable throwable) {
return new Result().setCode(ErrorCodeEnum.SERVICE_ERROR.getCode())
.setMessage(throwable.getMessage());
}
public static Result failure(ErrorCode errorCode) {
return failure(errorCode.getCode(), errorCode.getMessage());
}
public static Result failure(String code, String message) {
return new Result()
.setCode(code)
.setMessage(message);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy