From d6743d6a7efca780295a368e025c515193157290 Mon Sep 17 00:00:00 2001 From: Ming Wang Date: Mon, 5 Feb 2024 13:15:15 -0500 Subject: [PATCH] map clientExceptionErrors --- .../io/cryostat/EntityExistsException.java | 29 +++++++++++++++++++ .../java/io/cryostat/ExceptionMappers.java | 9 ++++++ .../cryostat/recordings/RecordingHelper.java | 7 ++--- src/main/java/io/cryostat/rules/Rules.java | 21 +++----------- 4 files changed, 44 insertions(+), 22 deletions(-) create mode 100644 src/main/java/io/cryostat/EntityExistsException.java diff --git a/src/main/java/io/cryostat/EntityExistsException.java b/src/main/java/io/cryostat/EntityExistsException.java new file mode 100644 index 000000000..a7d5f7bb7 --- /dev/null +++ b/src/main/java/io/cryostat/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; + +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); + } +} diff --git a/src/main/java/io/cryostat/ExceptionMappers.java b/src/main/java/io/cryostat/ExceptionMappers.java index 1362e09bc..fbe9ec15c 100644 --- a/src/main/java/io/cryostat/ExceptionMappers.java +++ b/src/main/java/io/cryostat/ExceptionMappers.java @@ -26,6 +26,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 +91,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..a3210b4f8 100644 --- a/src/main/java/io/cryostat/recordings/RecordingHelper.java +++ b/src/main/java/io/cryostat/recordings/RecordingHelper.java @@ -45,6 +45,7 @@ import org.openjdk.jmc.rjmx.services.jfr.IRecordingDescriptor; import io.cryostat.ConfigProperties; +import io.cryostat.EntityExistsException; import io.cryostat.Producers; import io.cryostat.core.EventOptionsBuilder; import io.cryostat.core.net.JFRConnection; @@ -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..6dac7141c 100644 --- a/src/main/java/io/cryostat/rules/Rules.java +++ b/src/main/java/io/cryostat/rules/Rules.java @@ -15,6 +15,7 @@ */ package io.cryostat.rules; +import io.cryostat.EntityExistsException; import io.cryostat.V2Response; import io.cryostat.expressions.MatchExpression; @@ -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 { @@ -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") @@ -156,13 +147,9 @@ public RestResponse delete(@RestPath String name, @RestQuery boolean 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); + public static class RuleExistsException extends EntityExistsException { + RuleExistsException(String name) { + super("Rule ", name); } } }