Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
mwangggg committed Feb 6, 2024
1 parent 17f6528 commit 23c0ac4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 33 deletions.
29 changes: 29 additions & 0 deletions src/main/java/io/cryostat/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;

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);
}
}
9 changes: 9 additions & 0 deletions src/main/java/io/cryostat/ExceptionMappers.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,4 +91,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 @@ -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;
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
32 changes: 4 additions & 28 deletions src/main/java/io/cryostat/rules/Rules.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.cryostat.rules;

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

Expand All @@ -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;
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 @@ -82,25 +81,6 @@ public RestResponse<V2Response> create(Rule rule) {
.build();
}

@ServerExceptionMapper
public RestResponse<V2Response> mapException(ClientErrorException e) {
if (e instanceof RuleExistsException) {
return ResponseBuilder.create(
Response.Status.CONFLICT,
V2Response.json(Response.Status.CONFLICT, e.getMessage()))
.build();
} else if (e instanceof NotFoundException) {
return ResponseBuilder.create(
Response.Status.NOT_FOUND,
V2Response.json(Response.Status.NOT_FOUND, e.getMessage()))
.build();
}
return ResponseBuilder.create(
Response.Status.BAD_REQUEST,
V2Response.json(Response.Status.BAD_REQUEST, e.getMessage()))
.build();
}

@Transactional
@PATCH
@RolesAllowed("write")
Expand Down Expand Up @@ -167,13 +147,9 @@ public RestResponse<V2Response> 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);
}
}
}

0 comments on commit 23c0ac4

Please sign in to comment.