Skip to content

Commit

Permalink
map clientExceptionErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
mwangggg committed Feb 6, 2024
1 parent 3abaf7f commit 2858693
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 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();
}
}
7 changes: 2 additions & 5 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,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(
Expand Down
23 changes: 2 additions & 21 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 All @@ -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 {
Expand Down Expand Up @@ -70,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 All @@ -82,14 +81,6 @@ public RestResponse<V2Response> create(Rule rule) {
.build();
}

@ServerExceptionMapper
public RestResponse<V2Response> mapException(RuleExistsException e) {
return ResponseBuilder.create(
Response.Status.CONFLICT,
V2Response.json(Response.Status.CONFLICT, e.getMessage()))
.build();
}

@Transactional
@PATCH
@RolesAllowed("write")
Expand Down Expand Up @@ -155,14 +146,4 @@ public RestResponse<V2Response> 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);
}
}
}
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 2858693

Please sign in to comment.