Skip to content

Commit

Permalink
fix(notifications): more descriptive error responses for UI notificat…
Browse files Browse the repository at this point in the history
…ions (#260)
  • Loading branch information
mwangggg authored Feb 6, 2024
1 parent bd6f61d commit 85d60c7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
10 changes: 10 additions & 0 deletions src/main/java/io/cryostat/ExceptionMappers.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.openjdk.jmc.rjmx.ConnectionException;

import io.cryostat.targets.TargetConnectionManager;
import io.cryostat.util.EntityExistsException;

import io.netty.handler.codec.http.HttpResponseStatus;
import io.smallrye.mutiny.TimeoutException;
Expand All @@ -26,6 +27,7 @@
import org.hibernate.exception.ConstraintViolationException;
import org.jboss.logging.Logger;
import org.jboss.resteasy.reactive.RestResponse;
import org.jboss.resteasy.reactive.RestResponse.ResponseBuilder;
import org.jboss.resteasy.reactive.server.ServerExceptionMapper;
import org.projectnessie.cel.tools.ScriptException;
import software.amazon.awssdk.services.s3.model.NoSuchKeyException;
Expand Down Expand Up @@ -90,4 +92,12 @@ public RestResponse<Void> mapMutinyTimeoutException(TimeoutException ex) {
logger.warn(ex);
return RestResponse.status(HttpResponseStatus.GATEWAY_TIMEOUT.code());
}

@ServerExceptionMapper
public RestResponse<Object> mapEntityExistsException(EntityExistsException ex) {
logger.warn(ex);
return ResponseBuilder.create(HttpResponseStatus.CONFLICT.code())
.entity(ex.getMessage())
.build();
}
}
6 changes: 2 additions & 4 deletions src/main/java/io/cryostat/recordings/RecordingHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import io.cryostat.recordings.Recordings.Metadata;
import io.cryostat.targets.Target;
import io.cryostat.targets.TargetConnectionManager;
import io.cryostat.util.EntityExistsException;
import io.cryostat.util.HttpMimeType;
import io.cryostat.ws.MessagingServer;
import io.cryostat.ws.Notification;
Expand Down Expand Up @@ -154,10 +155,7 @@ public ActiveRecording startRecording(
boolean restart =
shouldRestartRecording(replace, previousState, recordingName);
if (!restart) {
throw new BadRequestException(
String.format(
"Recording with name \"%s\" already exists",
recordingName));
throw new EntityExistsException("Recording", recordingName);
}
if (!ActiveRecording.deleteFromTarget(target, recordingName)) {
logger.warnf(
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/io/cryostat/rules/Rules.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@

import io.cryostat.V2Response;
import io.cryostat.expressions.MatchExpression;
import io.cryostat.util.EntityExistsException;

import io.vertx.core.json.JsonObject;
import io.vertx.mutiny.core.eventbus.EventBus;
import jakarta.annotation.security.RolesAllowed;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.ClientErrorException;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
Expand Down Expand Up @@ -69,7 +69,7 @@ public RestResponse<V2Response> create(Rule rule) {
}
boolean ruleExists = Rule.getByName(rule.name) != null;
if (ruleExists) {
throw new RuleExistsException(rule.name);
throw new EntityExistsException("Rule", rule.name);
}
if (rule.description == null) {
rule.description = "";
Expand Down Expand Up @@ -146,10 +146,4 @@ public RestResponse<V2Response> delete(@RestPath String name, @RestQuery boolean
rule.delete();
return RestResponse.ok(V2Response.json(Response.Status.OK, null));
}

static class RuleExistsException extends ClientErrorException {
RuleExistsException(String ruleName) {
super("Rule with name " + ruleName + " already exists", Response.Status.CONFLICT);
}
}
}
29 changes: 29 additions & 0 deletions src/main/java/io/cryostat/util/EntityExistsException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright The Cryostat Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.cryostat.util;

import jakarta.ws.rs.ClientErrorException;
import jakarta.ws.rs.core.Response;

public class EntityExistsException extends ClientErrorException {
public EntityExistsException(String type, String name) {
super(
String.format(
"%s with name %s already exists. Try again with a different name",
type, name),
Response.Status.CONFLICT);
}
}

0 comments on commit 85d60c7

Please sign in to comment.