I’m trying to configure an exception to return a http 400: bad request with a custom message in quarkus.
By default a BAD_REQUEST returns without a message, and that is not what I want.
The exception I’m trying to get through
<code>public class InvalidRowNumberException extends BadRequestException {
public InvalidRowNumberException(String s) {
super(s);
}
}
</code>
<code>public class InvalidRowNumberException extends BadRequestException {
public InvalidRowNumberException(String s) {
super(s);
}
}
</code>
public class InvalidRowNumberException extends BadRequestException {
public InvalidRowNumberException(String s) {
super(s);
}
}
The mapper I currently have. It uses NOT_FOUND for diagnostics. The actual code I get is BAD_REQUEST, so I know the mapper is not used. This should become BAD_REQUEST.
<code>public class ExceptionMapperProvider {
@ServerExceptionMapper
public RestResponse<String> mapInvalidRowNumberExceptionException(InvalidRowNumberException e) {
return RestResponse.status(RestResponse.Status.NOT_FOUND, e.getMessage());
}
}
</code>
<code>public class ExceptionMapperProvider {
@ServerExceptionMapper
public RestResponse<String> mapInvalidRowNumberExceptionException(InvalidRowNumberException e) {
return RestResponse.status(RestResponse.Status.NOT_FOUND, e.getMessage());
}
}
</code>
public class ExceptionMapperProvider {
@ServerExceptionMapper
public RestResponse<String> mapInvalidRowNumberExceptionException(InvalidRowNumberException e) {
return RestResponse.status(RestResponse.Status.NOT_FOUND, e.getMessage());
}
}
Rest endpoint
<code>@POST
@Consumes({ "application/json" })
@Produces({ "text/plain" })
String shootCannon(@Valid @NotNull Shot shot){
if(shot.getCoordinate().getRowNumber() <= 0){
throw new InvalidRowNumberException("Row number can't be negative");
}
return "HitOrMiss";
}
</code>
<code>@POST
@Consumes({ "application/json" })
@Produces({ "text/plain" })
String shootCannon(@Valid @NotNull Shot shot){
if(shot.getCoordinate().getRowNumber() <= 0){
throw new InvalidRowNumberException("Row number can't be negative");
}
return "HitOrMiss";
}
</code>
@POST
@Consumes({ "application/json" })
@Produces({ "text/plain" })
String shootCannon(@Valid @NotNull Shot shot){
if(shot.getCoordinate().getRowNumber() <= 0){
throw new InvalidRowNumberException("Row number can't be negative");
}
return "HitOrMiss";
}
I’ve tried to notate the Mapper class with an @Provider annotation, but that didn’t work either.