Skip to content

Commit bad983e

Browse files
authored
Update boot-http-service.md
1 parent 29257df commit bad983e

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

docs/boot-http-service.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,13 @@ public class ClientExampleInitializer {
485485

486486
#### Handling Errors: Post Not Found Exception
487487

488-
To gracefully handle scenarios where a requested post does not exist, add this snippet to the listener method:
488+
In the caller module, exceptions can be handled in the `onError()` callback or using an error handler in `subscribe()`:
489+
490+
```java
491+
postService.getById(id).doOnError(...).subscrbe(...);
492+
```
493+
494+
To demonstrate how to handle scenarios where a requested post does not exist, add this snippet to the listener method:
489495

490496
```java
491497
log.debug("get post by id that not existed.");
@@ -496,7 +502,7 @@ client.getById(UUID.randomUUID())
496502
);
497503
```
498504

499-
And add a custom status handler in the `WebClient` bean like this.
505+
Add a custom status handler to the `WebClient` bean as follows.
500506

501507
```java
502508
@Configuration
@@ -515,21 +521,15 @@ public class ClientConfig {
515521

516522
With this handler, all 4xx HTTP status codes will trigger a `WebClientResponseException` within the reactive flow.
517523

518-
In the caller module, exceptions can be handled in the `onError` callback:
519-
520-
```java
521-
postService.getById(id).onError().subscrbe(...);
522-
```
523-
524-
Alternatively, define a custom global exception like `PostServiceException`:
524+
Alternatively, define a custom global exception like `PostServiceException` and wrap the exception messages:
525525

526526
```java
527527
.defaultStatusHandler(HttpStatusCode::is4xxClientError,
528528
response -> response.createException()
529529
.map(it -> new PostServiceException(it.getResponseBodyAsString()))
530530
```
531531

532-
Or, restore the original exception from the server APIs:
532+
Or, restore to use the original exception that was used in the server APIs:
533533

534534
```java
535535
.defaultStatusHandler(status -> status == HttpStatus.NOT_FOUND,
@@ -550,6 +550,6 @@ Or, restore the original exception from the server APIs:
550550

551551

552552
>[!WARNING]
553-
>I noticed an issue when adding the status handler: if the application type is set to non-web, it raises a `WebClientRequestException` instead.
553+
>I encountered an issue in my example codes when adding the status handler. If the application type is set to `none`, it raises a `WebClientRequestException` instead in the client `ContextRefreshedEvent` handler.
554554

555555
Get the [complete example codes](https://github.com/hantsy/spring6-sandbox/tree/master/boot-http-service) from Github, and explore it yourself.

0 commit comments

Comments
 (0)