org.zodiac.sentinel.base.client.callback.reactive.DefaultReactiveBlockFallback Maven / Gradle / Ivy
package org.zodiac.sentinel.base.client.callback.reactive;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ServerWebExchange;
import org.zodiac.commons.web.model.RestResult;
import org.zodiac.commons.web.model.RestResult.RestResultBuilder;
import org.zodiac.sdk.toolkit.util.lang.ArrayUtil;
import org.zodiac.sentinel.base.client.callback.BlockFallback;
import reactor.core.publisher.Mono;
class DefaultReactiveBlockFallback implements BlockFallback {
private final ServerWebExchange exchange;
public DefaultReactiveBlockFallback(ServerWebExchange exchange) {
this.exchange = exchange;
}
@Override
public Mono fallback(HttpStatus status, MediaType contentType, byte[] body, Throwable throwable) throws Exception {
RestResultBuilder builder = RestResult.builder();
builder.withSuccess(false).withStatusCode(status.value());
if (null != throwable) {
builder.withMsg(throwable.getMessage());
}
if (ArrayUtil.isNotEmptyArray(body)) {
builder.withData(body);
}
RestResult restResult = builder.build();
return ServerResponse.status(status).contentType(contentType).body(BodyInserters.fromValue(restResult));
}
}