Skip to content

Commit

Permalink
chore: improve error for missing secret (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
wtrocki authored Mar 16, 2021
1 parent 6cca5cd commit 132ef1b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public String getAccessToken(String accessTokenSecretName, String namespace)
var accessToken = exchangeToken(offlineToken);
return accessToken;
} catch (Exception ex) {
LOG.log(Level.SEVERE, ex.getMessage(), ex);
LOG.log(Level.SEVERE, ex.getMessage());
throw new ConditionAwareException(
ex.getMessage(),
ex,
Expand All @@ -65,17 +65,14 @@ public String getAccessToken(String accessTokenSecretName, String namespace)
}

private String getOfflineTokenFromSecret(String secretName, String namespace) {
var offlineToken =
k8sClient
.secrets()
.inNamespace(namespace)
.withName(secretName)
.get()
.getData()
.get(ACCESS_TOKEN_SECRET_KEY);
offlineToken = new String(Base64.getDecoder().decode(offlineToken));
var token = k8sClient.secrets().inNamespace(namespace).withName(secretName).get();
if (token != null) {
var offlineToken = token.getData().get(ACCESS_TOKEN_SECRET_KEY);
offlineToken = new String(Base64.getDecoder().decode(offlineToken));

return offlineToken;
return offlineToken;
}
throw new Error("Missing Offline Token Secret " + secretName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,29 @@ public KafkaRequest getKafkaById(String kafkaId, String accessToken)
try {
return createClient(accessToken).getKafkaById(kafkaId);
} catch (ApiException e) {
String message = getStandarizedErrorMessage(e);
throw new ConditionAwareException(
e.getMessage(),
message,
e,
KafkaCondition.Type.FoundKafkaById,
KafkaCondition.Status.False,
e.getClass().getName(),
e.getMessage());
message);
}
}

public KafkaRequestList listKafkas(String accessToken) throws ConditionAwareException {
try {
return createClient(accessToken).listKafkas(null, null, null, null);
} catch (ApiException e) {
String message = getStandarizedErrorMessage(e);
throw new ConditionAwareException(
e.getMessage(),
message,
e,
KafkaCondition.Type.UserKafkasUpToDate,
KafkaCondition.Status.False,
e.getClass().getName(),
e.getMessage());
message);
}
}

Expand All @@ -81,13 +83,14 @@ public ServiceAccount createServiceAccount(
serviceAccountRequest.setName(spec.getServiceAccountName());
return createClient(accessToken).createServiceAccount(serviceAccountRequest);
} catch (ApiException e) {
String message = getStandarizedErrorMessage(e);
throw new ConditionAwareException(
e.getMessage(),
message,
e,
KafkaCondition.Type.ServiceAccountCreated,
KafkaCondition.Status.False,
e.getClass().getName(),
e.getMessage());
message);
}
}

Expand Down Expand Up @@ -133,4 +136,26 @@ public void createSecretForServiceAccount(
e.getMessage());
}
}

private String getStandarizedErrorMessage(ApiException e) {
if (e.getCode() == 504) {
return "Server timeout. Server is not responding";
}
if (e.getCode() == 500) {
return "Unknown server error.";
}
if (e.getCode() == 500) {
return "Unknown server error.";
}
if (e.getCode() == 400) {
return "Invalid request " + e.getMessage();
}
if (e.getCode() == 401) {
return "Auth Token is invalid.";
}
if (e.getCode() == 403) {
return "User not authorized to access the service";
}
return e.getMessage();
}
}

0 comments on commit 132ef1b

Please sign in to comment.