RestController throws 503 Service Unavailable, if the remote-server fails
[demos/testing] / src / main / java / de / juplo / demo / RestController.java
index 288fd9e..f7f63a6 100644 (file)
@@ -4,6 +4,8 @@ package de.juplo.demo;
 import org.springframework.http.MediaType;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.reactive.function.client.WebClientResponseException;
+import org.springframework.web.reactive.function.client.WebClientResponseException.NotFound;
 import reactor.core.publisher.Mono;
 
 
@@ -26,6 +28,22 @@ public class RestController
   @GetMapping(path = { "", "/" }, produces = MediaType.TEXT_PLAIN_VALUE)
   public Mono<String> fetch(@RequestParam String path)
   {
-    return service.getRemoteText(path);
+    return
+        service
+            .getRemoteText(path)
+            .onErrorResume(t ->
+            {
+              if(t.getClass().equals(NotFound.class))
+                return Mono.error(t);
+
+              return
+                  Mono.error(
+                      WebClientResponseException.create(
+                          503,
+                          "Service Unavailable - Cause: " + t.getMessage(),
+                          null,
+                          null,
+                          null));
+            });
   }
 }