diff --git a/src/main/java/io/cryostat/ExceptionMappers.java b/src/main/java/io/cryostat/ExceptionMappers.java index 1362e09bc..593ea1484 100644 --- a/src/main/java/io/cryostat/ExceptionMappers.java +++ b/src/main/java/io/cryostat/ExceptionMappers.java @@ -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; @@ -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; @@ -90,4 +92,12 @@ public RestResponse mapMutinyTimeoutException(TimeoutException ex) { logger.warn(ex); return RestResponse.status(HttpResponseStatus.GATEWAY_TIMEOUT.code()); } + + @ServerExceptionMapper + public RestResponse mapEntityExistsException(EntityExistsException ex) { + logger.warn(ex); + return ResponseBuilder.create(HttpResponseStatus.CONFLICT.code()) + .entity(ex.getMessage()) + .build(); + } } diff --git a/src/main/java/io/cryostat/recordings/RecordingHelper.java b/src/main/java/io/cryostat/recordings/RecordingHelper.java index 336121f16..aecb9a073 100644 --- a/src/main/java/io/cryostat/recordings/RecordingHelper.java +++ b/src/main/java/io/cryostat/recordings/RecordingHelper.java @@ -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; @@ -154,11 +155,7 @@ public ActiveRecording startRecording( boolean restart = shouldRestartRecording(replace, previousState, recordingName); if (!restart) { - throw new BadRequestException( - String.format( - "Recording with name \"%s\" already exists. Rename" - + " the recording and try again.", - recordingName)); + throw new EntityExistsException("Recording", recordingName); } if (!ActiveRecording.deleteFromTarget(target, recordingName)) { logger.warnf( diff --git a/src/main/java/io/cryostat/rules/Rules.java b/src/main/java/io/cryostat/rules/Rules.java index 2ad91878a..32aadcb34 100644 --- a/src/main/java/io/cryostat/rules/Rules.java +++ b/src/main/java/io/cryostat/rules/Rules.java @@ -17,6 +17,7 @@ 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; @@ -24,7 +25,6 @@ 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; @@ -39,7 +39,6 @@ import org.jboss.resteasy.reactive.RestQuery; import org.jboss.resteasy.reactive.RestResponse; import org.jboss.resteasy.reactive.RestResponse.ResponseBuilder; -import org.jboss.resteasy.reactive.server.ServerExceptionMapper; @Path("/api/v2/rules") public class Rules { @@ -70,7 +69,7 @@ public RestResponse 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 = ""; @@ -82,14 +81,6 @@ public RestResponse create(Rule rule) { .build(); } - @ServerExceptionMapper - public RestResponse mapException(RuleExistsException e) { - return ResponseBuilder.create( - Response.Status.CONFLICT, - V2Response.json(Response.Status.CONFLICT, e.getMessage())) - .build(); - } - @Transactional @PATCH @RolesAllowed("write") @@ -155,14 +146,4 @@ public RestResponse delete(@RestPath String name, @RestQuery boolean rule.delete(); return RestResponse.ok(V2Response.json(Response.Status.OK, null)); } - - public static class RuleExistsException extends ClientErrorException { - RuleExistsException(String ruleName) { - super( - "Rule with name " - + ruleName - + " already exists. Rename the rule and try again.", - Response.Status.CONFLICT); - } - } } diff --git a/src/main/java/io/cryostat/util/EntityExistsException.java b/src/main/java/io/cryostat/util/EntityExistsException.java new file mode 100644 index 000000000..b1ac7d3b3 --- /dev/null +++ b/src/main/java/io/cryostat/util/EntityExistsException.java @@ -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); + } +}