24
24
import org .springframework .context .annotation .Configuration ;
25
25
import org .springframework .core .annotation .Order ;
26
26
import org .springframework .core .io .buffer .DataBufferFactory ;
27
- import org .springframework .http .HttpStatus ;
28
27
import org .springframework .http .MediaType ;
29
28
import org .springframework .http .server .reactive .ServerHttpRequest ;
30
29
import org .springframework .http .server .reactive .ServerHttpResponse ;
31
- import org .springframework .lang .NonNull ;
32
30
import org .springframework .web .server .ResponseStatusException ;
33
31
import org .springframework .web .server .ServerWebExchange ;
34
32
import reactor .core .publisher .Mono ;
35
33
34
+ import java .util .Map ;
35
+
36
36
/**
37
37
* 异常处理
38
38
*
@@ -45,43 +45,42 @@ public class ErrorExceptionHandler implements ErrorWebExceptionHandler {
45
45
46
46
private final ObjectMapper objectMapper ;
47
47
48
- @ NonNull
49
48
@ Override
50
- public Mono <Void > handle (ServerWebExchange exchange , @ NonNull Throwable ex ) {
49
+ public Mono <Void > handle (ServerWebExchange exchange , Throwable ex ) {
51
50
ServerHttpRequest request = exchange .getRequest ();
52
51
ServerHttpResponse response = exchange .getResponse ();
52
+
53
53
if (response .isCommitted ()) {
54
54
return Mono .error (ex );
55
55
}
56
+
56
57
response .getHeaders ().setContentType (MediaType .APPLICATION_JSON );
57
58
if (ex instanceof ResponseStatusException ) {
58
- response .setStatusCode (((ResponseStatusException ) ex ).getStatus ());
59
+ response .setStatusCode (((ResponseStatusException ) ex ).getStatusCode ());
59
60
}
61
+
60
62
return response .writeWith (Mono .fromSupplier (() -> {
61
63
DataBufferFactory bufferFactory = response .bufferFactory ();
62
64
try {
63
- HttpStatus status = HttpStatus . BAD_GATEWAY ;
64
- if (ex instanceof ResponseStatusException ) {
65
- status = (( ResponseStatusException ) ex ). getStatus ();
65
+ int status = 500 ;
66
+ if (response . getStatusCode () != null ) {
67
+ status = response . getStatusCode (). value ();
66
68
}
67
- return bufferFactory .wrap (objectMapper .writeValueAsBytes (ResponseProvider .response (status .value (), buildMessage (request , ex ))));
69
+ Map <String , Object > result = ResponseProvider .response (status , this .buildMessage (request , ex ));
70
+ return bufferFactory .wrap (objectMapper .writeValueAsBytes (result ));
68
71
} catch (JsonProcessingException e ) {
69
- e .printStackTrace ();
70
72
return bufferFactory .wrap (new byte [0 ]);
71
73
}
72
74
}));
73
75
}
74
76
77
+
75
78
/**
76
79
* 构建异常信息
77
80
*/
78
81
private String buildMessage (ServerHttpRequest request , Throwable ex ) {
79
- String uri = request .getURI ().toString ();
80
- if (uri .endsWith ("doc.html" )) {
81
- return "[Swagger聚合网关] 已迁移至 [blade-swagger] 服务,请开启 [blade-swagger] 服务并访问 [http://127.0.0.1:18000/doc.html]" ;
82
- }
83
82
StringBuilder message = new StringBuilder ("Failed to handle request [" );
84
- message .append (request .getMethodValue ());
83
+ message .append (request .getMethod (). name ());
85
84
message .append (" " );
86
85
message .append (request .getURI ());
87
86
message .append ("]" );
@@ -92,4 +91,5 @@ private String buildMessage(ServerHttpRequest request, Throwable ex) {
92
91
return message .toString ();
93
92
}
94
93
94
+
95
95
}
0 commit comments