| package com.doumee.config; | 
|   | 
| import com.doumee.core.exception.BusinessException; | 
| import org.springframework.cloud.gateway.filter.GatewayFilterChain; | 
| import org.springframework.cloud.gateway.filter.GlobalFilter; | 
| import org.springframework.core.io.buffer.DataBufferFactory; | 
| import org.springframework.core.io.buffer.DataBufferUtils; | 
| import org.springframework.http.HttpStatus; | 
| import org.springframework.http.MediaType; | 
| import org.springframework.http.server.reactive.ServerHttpResponse; | 
| import org.springframework.web.server.ServerWebExchange; | 
| import reactor.core.publisher.Mono; | 
|   | 
| import java.nio.charset.StandardCharsets; | 
|   | 
| public class GlobalErrorFilter implements GlobalFilter { | 
|   | 
|     @Override | 
|     public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { | 
|         return chain.filter(exchange).onErrorResume(throwable -> { | 
|             ServerHttpResponse response = exchange.getResponse(); | 
|             response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR); | 
|   | 
|             response.getHeaders().setContentType(MediaType.APPLICATION_JSON); | 
|   | 
|             String errorMessage = "{\"message\": \"" + throwable.getMessage() + "\"}"; | 
|             byte[] bytes = errorMessage.getBytes(StandardCharsets.UTF_8); | 
|             DataBufferFactory bufferFactory = response.bufferFactory(); | 
|             response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR); | 
|             response.getHeaders().setContentType(MediaType.APPLICATION_JSON); | 
|             return response.writeWith(Mono.just(bufferFactory.wrap(bytes))); | 
|         }); | 
|     } | 
| } |