in core/src/main/java/com/google/cloud/sql/core/DefaultConnectionInfoRepository.java [512:545]
private RuntimeException addExceptionContext(
Exception ex, String fallbackDesc, CloudSqlInstanceName instanceName) {
String reason = fallbackDesc;
int statusCode = 0;
// Verify we are able to extract a reason from an exception and the status code, or fallback to
// a generic desc
if (ex instanceof GoogleJsonResponseException) {
GoogleJsonResponseException gjrEx = (GoogleJsonResponseException) ex;
reason = gjrEx.getMessage();
statusCode = gjrEx.getStatusCode();
} else if (ex instanceof UnknownHostException) {
statusCode = 404;
reason = String.format("Host \"%s\" not found", ex.getMessage());
} else {
Matcher matcher = Pattern.compile("Error code (\\d+)").matcher(ex.getMessage());
reason = ex.getMessage();
if (matcher.find()) {
statusCode = Integer.parseInt(matcher.group(1));
}
}
// Check for commonly occurring user errors and add additional context
String message =
String.format(
"[%s] The Google Cloud SQL Admin API failed for the project \"%s\". Reason: %s",
instanceName.getConnectionName(), instanceName.getProjectId(), reason);
if (TERMINAL_STATUS_CODES.contains(statusCode)) {
return new TerminalException(message, ex);
}
// Fallback to the generic description
return new RuntimeException(message, ex);
}