generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 20
fix: Destination Service Error Handling #555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
97a88d1
fix: Destination Service Error Handling
MatKuhr 1268168
Add test
MatKuhr 06d5d59
Fix leftovers
MatKuhr 88ae969
Release note
MatKuhr 63c8cce
Merge branch 'main' into fix/destination-service-error-handling
MatKuhr 5eb4d12
Fomratting
MatKuhr 54eee18
Merge branch 'fix/destination-service-error-handling' of github.com:S…
MatKuhr bb561af
Change wiremock test to regular unit test (#563)
newtork File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,9 @@ | |
import org.apache.http.StatusLine; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.client.methods.HttpUriRequest; | ||
import org.slf4j.event.Level; | ||
|
||
import com.google.common.base.Strings; | ||
import com.sap.cloud.environment.servicebinding.api.DefaultServiceBindingAccessor; | ||
import com.sap.cloud.environment.servicebinding.api.ServiceBinding; | ||
import com.sap.cloud.environment.servicebinding.api.ServiceIdentifier; | ||
|
@@ -148,35 +150,37 @@ private static String handleResponse( final HttpUriRequest request, final HttpRe | |
final int statusCode = status.getStatusCode(); | ||
final String reasonPhrase = status.getReasonPhrase(); | ||
|
||
log.debug("Destination service returned HTTP status {} ({})", statusCode, reasonPhrase); | ||
|
||
if( statusCode != HttpStatus.SC_OK ) { | ||
final String requestUri = request.getURI().getPath(); | ||
if( statusCode == HttpStatus.SC_NOT_FOUND ) { | ||
throw new DestinationNotFoundException( | ||
null, | ||
"Destination could not be found for path " + requestUri + "."); | ||
} else { | ||
throw new DestinationAccessException( | ||
String | ||
.format( | ||
"Failed to get destinations: destination service returned HTTP status %s (%S) at '%s'.,", | ||
statusCode, | ||
reasonPhrase, | ||
requestUri)); | ||
} | ||
Try<String> maybeBody = Try.of(() -> HttpEntityUtil.getResponseBody(response)); | ||
String logMessage = "Destination service returned HTTP status %s (%s)"; | ||
if( maybeBody.isFailure() ) { | ||
final var ex = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (used |
||
new DestinationAccessException("Failed to read body from HTTP response", maybeBody.getCause()); | ||
maybeBody = Try.failure(ex); | ||
logMessage = String.format(logMessage, statusCode, reasonPhrase); | ||
} else { | ||
logMessage = String.format(logMessage + "and body '%s'", statusCode, reasonPhrase, maybeBody.get()); | ||
} | ||
|
||
try { | ||
final String responseBody = HttpEntityUtil.getResponseBody(response); | ||
if( responseBody == null ) { | ||
throw new DestinationAccessException("Failed to get destinations: no body returned in response."); | ||
} | ||
return responseBody; | ||
if( statusCode == HttpStatus.SC_OK ) { | ||
final var ex = new DestinationAccessException("Failed to get destinations: no body returned in response."); | ||
maybeBody = maybeBody.filter(it -> !Strings.isNullOrEmpty(it), () -> ex); | ||
log.atLevel(maybeBody.isSuccess() ? Level.DEBUG : Level.ERROR).log(logMessage); | ||
return maybeBody.get(); | ||
} | ||
catch( final IOException e ) { | ||
throw new DestinationAccessException(e); | ||
|
||
log.error(logMessage); | ||
final String requestUri = request.getURI().getPath(); | ||
if( statusCode == HttpStatus.SC_NOT_FOUND ) { | ||
throw new DestinationNotFoundException(null, "Destination could not be found for path " + requestUri + "."); | ||
} | ||
throw new DestinationAccessException( | ||
String | ||
.format( | ||
"Failed to get destinations: destination service returned HTTP status %s (%S) at '%s'.,", | ||
statusCode, | ||
reasonPhrase, | ||
requestUri)); | ||
|
||
} | ||
|
||
private HttpUriRequest prepareRequest( final String servicePath, final DestinationRetrievalStrategy strategy ) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Comment)
I'm not a fan of the dynamic string construction for log- and exception-messages. I know the format string
%s
is different from the log string template{}
but I'm not sure the solution is the "best" practice yet.The dynamic construction makes the code below complicated and during troubleshooting, we have to think twice, how a messages maps a corresponding piece of code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same, I felt this was a tradeoff between complex code vs. code duplication. Maybe at some point we switch to a more high-level HTTP client, where we don't have to re-implement this response handling every time 😄