|
18 | 18 | import org.apache.http.StatusLine; |
19 | 19 | import org.apache.http.client.methods.HttpGet; |
20 | 20 | import org.apache.http.client.methods.HttpUriRequest; |
21 | | -import org.slf4j.event.Level; |
22 | 21 |
|
23 | 22 | import com.google.common.base.Strings; |
24 | 23 | import com.sap.cloud.environment.servicebinding.api.DefaultServiceBindingAccessor; |
@@ -146,37 +145,31 @@ private static String handleResponse( final HttpUriRequest request, final HttpRe |
146 | 145 | final int statusCode = status.getStatusCode(); |
147 | 146 | final String reasonPhrase = status.getReasonPhrase(); |
148 | 147 |
|
| 148 | + log.debug("Destination service returned HTTP status {} ({})", statusCode, reasonPhrase); |
| 149 | + |
149 | 150 | Try<String> maybeBody = Try.of(() -> HttpEntityUtil.getResponseBody(response)); |
150 | | - String logMessage = "Destination service returned HTTP status %s (%s)"; |
151 | 151 | if( maybeBody.isFailure() ) { |
152 | 152 | final var ex = |
153 | 153 | new DestinationAccessException("Failed to read body from HTTP response", maybeBody.getCause()); |
154 | 154 | maybeBody = Try.failure(ex); |
155 | | - logMessage = String.format(logMessage, statusCode, reasonPhrase); |
156 | | - } else { |
157 | | - logMessage = String.format(logMessage + "and body '%s'", statusCode, reasonPhrase, maybeBody.get()); |
158 | 155 | } |
159 | 156 |
|
160 | 157 | if( statusCode == HttpStatus.SC_OK ) { |
161 | 158 | final var ex = new DestinationAccessException("Failed to get destinations: no body returned in response."); |
162 | 159 | maybeBody = maybeBody.filter(it -> !Strings.isNullOrEmpty(it), () -> ex); |
163 | | - log.atLevel(maybeBody.isSuccess() ? Level.DEBUG : Level.ERROR).log(logMessage); |
164 | 160 | return maybeBody.get(); |
165 | 161 | } |
166 | 162 |
|
167 | | - log.error(logMessage); |
168 | 163 | final String requestUri = request.getURI().getPath(); |
169 | 164 | if( statusCode == HttpStatus.SC_NOT_FOUND ) { |
170 | 165 | throw new DestinationNotFoundException(null, "Destination could not be found for path " + requestUri + "."); |
171 | 166 | } |
172 | | - throw new DestinationAccessException( |
173 | | - String |
174 | | - .format( |
175 | | - "Failed to get destinations: destination service returned HTTP status %s (%S) at '%s'.,", |
176 | | - statusCode, |
177 | | - reasonPhrase, |
178 | | - requestUri)); |
179 | | - |
| 167 | + final String message = |
| 168 | + "Failed to get destinations: destination service responded with HTTP status %s (%S) at '%s'." |
| 169 | + .formatted(statusCode, reasonPhrase, requestUri); |
| 170 | + final String messageWithBody = message + " Body: %s".formatted(maybeBody.getOrElseGet(Throwable::getMessage)); |
| 171 | + log.error(messageWithBody); |
| 172 | + throw new DestinationAccessException(message); |
180 | 173 | } |
181 | 174 |
|
182 | 175 | private HttpUriRequest prepareRequest( final String servicePath, final DestinationRetrievalStrategy strategy ) |
|
0 commit comments