ichenhe commented on issue #2190:
URL: 
https://github.com/apache/incubator-shenyu/issues/2190#issuecomment-933150098


   After some research, it seems that there is no place that can be set up 
directly. Here's my workaround, if no one comes up with a better idea, I will 
close the issue after a while.
   
   ### Inherit the upstream http code (e.g. 400)
   
   We have two ways:
   
   1. Add a custom plugin that wrap the `ServerWebExchange` with 
`ServerHttpResponseDecorator`, override its `writeWith` method to change the 
http status code. Please refer to 
[`ModifyResponsePlugin`](https://github.com/apache/incubator-shenyu/blob/dac22f51c7b834ac7a901998a159c7bc407740db/shenyu-plugin/shenyu-plugin-modify-response/src/main/java/org/apache/shenyu/plugin/modify/response/ModifyResponsePlugin.java).
   2. Add a custom plugin that change the final response directly.
   
   Either way, you can get the original response that from upstream through 
`exchange.getAttribute<ClientResponse>(Constants.CLIENT_RESPONSE_ATTR)`.
   
   The first approach is more standardized but a little complicated. So I 
chosen the second one. 
   
   ```kotlin
   @Component
   class SyncHttpStatusCodePlugin : ShenyuPlugin {
       override fun named(): String = "sync_http_status_code"
       override fun getOrder(): Int = PluginEnum.RESPONSE.code + 1 // make sure 
our plugin is finally executed
   
       override fun execute(exchange: ServerWebExchange, chain: 
ShenyuPluginChain): Mono<Void> {
           
exchange.getAttribute<ClientResponse>(Constants.CLIENT_RESPONSE_ATTR)?.also { 
resp ->
               exchange.response.statusCode = resp.statusCode()
           }
           return chain.execute(exchange)
       }
   }
   ```
   
   ### Custom http code if any errors in Shenyu.
   
   The relevant classes are 
[`WebFluxResultUtils`](https://github.com/apache/incubator-shenyu/blob/4cdcfa67918b3628d03a9f2a645429354450a24c/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/WebFluxResultUtils.java),
 custom plugin is useless at this time.
   
   I use `WebFilter` to prevent this issue from its root causes, but it may not 
be suitable for your usecase. Maybe we can submit a PR to add an interface that 
allows custom status codes?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to