Skip to content

Commit

Permalink
add specific error handling
Browse files Browse the repository at this point in the history
Signed-off-by: Max Cao <macao@redhat.com>
  • Loading branch information
maxcao13 committed Mar 28, 2023
1 parent 7656b1f commit 22d70b4
Showing 1 changed file with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import io.cryostat.net.ConnectionDescriptor;
import io.cryostat.net.TargetConnectionManager;
import io.cryostat.net.security.ResourceAction;
import io.cryostat.net.web.http.AbstractAuthenticatedRequestHandler;
import io.cryostat.net.web.http.HttpMimeType;
import io.cryostat.net.web.http.api.ApiVersion;
import io.cryostat.net.web.http.api.beta.CredentialTestPostHandler.CredentialTestResult;
Expand Down Expand Up @@ -143,8 +144,8 @@ public IntermediateResponse<CredentialTestResult> handle(RequestParameters param
conn.connect();
return CredentialTestResult.NA;
}));
} catch (Exception e) {
if (e.getCause() instanceof SecurityException) {
} catch (Exception e1) {
if (AbstractAuthenticatedRequestHandler.isJmxAuthFailure(e1)) {
ConnectionDescriptor creds =
new ConnectionDescriptor(targetId, new Credentials(username, password));
try {
Expand All @@ -157,17 +158,27 @@ public IntermediateResponse<CredentialTestResult> handle(RequestParameters param
return CredentialTestResult.SUCCESS;
}));
} catch (Exception e2) {
if (e2.getCause() instanceof SecurityException) {
if (AbstractAuthenticatedRequestHandler.isJmxAuthFailure(e2)) {
return new IntermediateResponse<CredentialTestResult>()
.body(CredentialTestResult.FAILURE);
}
throw new ApiException(500, e2);
throw resolveErrors(e2);
}
}
throw new ApiException(500, e);
throw resolveErrors(e1);
}
}

ApiException resolveErrors(Exception e) throws Exception {
if (AbstractAuthenticatedRequestHandler.isJmxSslFailure(e)) {
return new ApiException(502, "Target SSL Untrusted", e);
}
if (AbstractAuthenticatedRequestHandler.isServiceTypeFailure(e)) {
return new ApiException(504, "Non-JMX Port", e);
}
return new ApiException(500, "Internal Error", e);
}

static enum CredentialTestResult {
SUCCESS,
FAILURE,
Expand Down

0 comments on commit 22d70b4

Please sign in to comment.