Skip to content

Commit 5e36884

Browse files
committed
improve message
1 parent d343343 commit 5e36884

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

cloudplatform/connectivity-oauth/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/OAuth2Service.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import io.vavr.CheckedFunction0;
4444
import io.vavr.control.Try;
4545
import lombok.AccessLevel;
46+
import lombok.AllArgsConstructor;
4647
import lombok.Getter;
4748
import lombok.RequiredArgsConstructor;
4849
import lombok.extern.slf4j.Slf4j;
@@ -51,6 +52,7 @@
5152
* This interface handles the communication with an OAuth2 service.
5253
*/
5354
@RequiredArgsConstructor( access = AccessLevel.PACKAGE )
55+
@AllArgsConstructor( access = AccessLevel.PRIVATE )
5456
@Slf4j
5557
class OAuth2Service
5658
{
@@ -89,6 +91,8 @@ class OAuth2Service
8991
@Nonnull
9092
@Getter( AccessLevel.PACKAGE )
9193
private final ResilienceConfiguration resilienceConfiguration;
94+
@Nullable
95+
private ServiceIdentifier serviceIdentifier;
9296

9397
// package-private for testing
9498
@Nonnull
@@ -196,16 +200,25 @@ private OAuth2TokenResponse executeClientCredentialsFlow( @Nullable final Tenant
196200

197201
private TokenRequestFailedException buildException( @Nonnull final Throwable e, @Nullable final Tenant tenant )
198202
{
199-
String msg = "Failed to resolve access token.";
203+
String message = "Failed to resolve access token.";
200204
// In case where tenant is not the provider tenant, and we get 401 error, add hint to error message.
201205
if( e instanceof OAuth2ServiceException
202206
&& ((OAuth2ServiceException) e).getHttpStatusCode().equals(401)
203207
&& tenant != null ) {
204-
msg +=
205-
" In case you are accessing a multi-tenant BTP service, ensure that the service instance is declared as dependency "
206-
+ "to SaaS Provisioning Service or Subscription Manager (SMS) and subscribed for the current tenant.";
208+
String extension;
209+
if( serviceIdentifier != null ) {
210+
extension =
211+
" In case you are accessing a multi-tenant BTP service on behalf of a subscriber tenant, ensure that the service instance (here, of the "
212+
+ serviceIdentifier
213+
+ " service) is declared as dependency to SaaS Provisioning Service or Subscription Manager (SMS) and subscribed for the current tenant.";
214+
} else {
215+
extension =
216+
" In case you are accessing a multi-tenant BTP service on behalf of a subscriber tenant, ensure that the service instance"
217+
+ " is declared as dependency to SaaS Provisioning Service or Subscription Manager (SMS) and subscribed for the current tenant.";
218+
}
219+
message += extension;
207220
}
208-
return new TokenRequestFailedException(msg, e);
221+
return new TokenRequestFailedException(message, e);
209222
}
210223

211224
private void setAppTidInCaseOfIAS( @Nullable final String tenantId )
@@ -335,6 +348,7 @@ static class Builder
335348
private TenantPropagationStrategy tenantPropagationStrategy = TenantPropagationStrategy.ZID_HEADER;
336349
private final Map<String, String> additionalParameters = new HashMap<>();
337350
private ResilienceConfiguration.TimeLimiterConfiguration timeLimiter = OAuth2Options.DEFAULT_TIMEOUT;
351+
private ServiceIdentifier serviceIdentifier;
338352

339353
@Nonnull
340354
Builder withTokenUri( @Nonnull final String tokenUri )
@@ -380,6 +394,7 @@ Builder withTenantPropagationStrategy( @Nonnull final TenantPropagationStrategy
380394
@Nonnull
381395
Builder withTenantPropagationStrategyFrom( @Nullable final ServiceIdentifier serviceIdentifier )
382396
{
397+
this.serviceIdentifier = serviceIdentifier;
383398
final TenantPropagationStrategy tenantPropagationStrategy;
384399
if( ServiceIdentifier.IDENTITY_AUTHENTICATION.equals(serviceIdentifier) ) {
385400
tenantPropagationStrategy = TenantPropagationStrategy.TENANT_SUBDOMAIN;
@@ -440,7 +455,8 @@ OAuth2Service build()
440455
onBehalfOf,
441456
tenantPropagationStrategy,
442457
additionalParameters,
443-
resilienceConfig);
458+
resilienceConfig,
459+
serviceIdentifier);
444460
}
445461
}
446462

cloudplatform/connectivity-oauth/src/test/java/com/sap/cloud/sdk/cloudplatform/connectivity/OAuth2IntegrationTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ void testExtended401ErrorMessage()
191191
TenantAccessor.executeWithTenant(new DefaultTenant("subscriber", "subscriber"), () -> {
192192
assertThatCode(destination::getHeaders)
193193
.isInstanceOf(DestinationAccessException.class)
194+
.hasMessageContaining("identity service")
194195
.hasMessageEndingWith("subscribed for the current tenant.")
195196
.hasRootCauseInstanceOf(OAuth2ServiceException.class);
196197
});

0 commit comments

Comments
 (0)