Skip to content
This repository was archived by the owner on May 14, 2025. It is now read-only.

Commit b051232

Browse files
Corneil du Plessiscorneil
Corneil du Plessis
authored andcommitted
Improve error handling on failed post to http.
1 parent d82ce32 commit b051232

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

spring-cloud-dataflow-rest-client/src/main/java/org/springframework/cloud/dataflow/rest/client/RuntimeTemplate.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.http.HttpEntity;
2929
import org.springframework.http.HttpHeaders;
3030
import org.springframework.http.HttpMethod;
31+
import org.springframework.http.ResponseEntity;
3132
import org.springframework.util.Assert;
3233
import org.springframework.web.client.RestTemplate;
3334

@@ -131,6 +132,9 @@ public void postToUrl(String appId, String instanceId, byte[] data, HttpHeaders
131132
Assert.notNull(appUrlPostUriTemplate, "post endpoint not found");
132133
String uri = appUrlPostUriTemplate.expand(appId, instanceId).getHref();
133134
HttpEntity<byte[]> entity = new HttpEntity<>(data, headers);
134-
this.restTemplate.exchange(uri, HttpMethod.POST, entity, String.class);
135+
ResponseEntity<String> response = this.restTemplate.exchange(uri, HttpMethod.POST, entity, String.class);
136+
if(!response.getStatusCode().is2xxSuccessful()) {
137+
throw new RuntimeException("POST:exception:" + response.getStatusCode() + ":" + response.getBody());
138+
}
135139
}
136140
}

spring-cloud-dataflow-server/src/test/java/org/springframework/cloud/dataflow/integration/test/util/RuntimeApplicationHelper.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import org.springframework.cloud.dataflow.core.ApplicationType;
3232
import org.springframework.cloud.dataflow.core.StreamRuntimePropertyKeys;
33+
import org.springframework.cloud.dataflow.rest.client.DataFlowClientException;
3334
import org.springframework.cloud.dataflow.rest.client.DataFlowTemplate;
3435
import org.springframework.cloud.dataflow.rest.resource.AppInstanceStatusResource;
3536
import org.springframework.cloud.dataflow.rest.resource.AppStatusResource;
@@ -40,6 +41,7 @@
4041
import org.springframework.http.HttpHeaders;
4142
import org.springframework.http.HttpMethod;
4243
import org.springframework.http.MediaType;
44+
import org.springframework.http.ResponseEntity;
4345
import org.springframework.util.Assert;
4446
import org.springframework.util.CollectionUtils;
4547
import org.springframework.util.StringUtils;
@@ -278,7 +280,10 @@ public void httpPostJson(String streamName, String appName, Object obj) throws J
278280

279281

280282
public void httpPost(String url, String message) {
281-
dataFlowTemplate.getRestTemplate().postForObject(url, message, String.class);
283+
ResponseEntity<String> response = dataFlowTemplate.getRestTemplate().postForEntity(url, message, String.class);
284+
if(!response.getStatusCode().is2xxSuccessful()) {
285+
throw new RuntimeException("Exception posting:" + response.getStatusCode()+":" + response.getBody());
286+
}
282287
}
283288

284289
public void httpPost(String streamName, String appName, byte[] message, HttpHeaders headers) {
@@ -319,11 +324,22 @@ public void httpPost(String streamName, String appName, byte[] message, HttpHead
319324
String url = instance.getAttributes().get("url");
320325
if(StringUtils.hasText(url)) {
321326
HttpEntity<byte[]> httpEntity = new HttpEntity<>(message, headers);
322-
this.dataFlowTemplate.getRestTemplate().exchange(url, HttpMethod.POST, httpEntity, String.class);
327+
ResponseEntity<String> response = this.dataFlowTemplate.getRestTemplate().exchange(url, HttpMethod.POST, httpEntity, String.class);
328+
if(!response.getStatusCode().is2xxSuccessful()) {
329+
throw new RuntimeException("POST:exception:" + response.getStatusCode() + ":" + response.getBody());
330+
}
323331
} else {
324332
throw new RuntimeException("Cannot find url for " + streamName + ":" + appName);
325333
}
326-
}
334+
} else {
335+
if(x instanceof DataFlowClientException) {
336+
throw (DataFlowClientException) x;
337+
} else if(x instanceof RuntimeException) {
338+
throw (RuntimeException) x;
339+
} else {
340+
throw new RuntimeException(x.getMessage(), x);
341+
}
342+
}
327343
}
328344
}
329345

0 commit comments

Comments
 (0)