use of org.candlepin.common.exceptions.CandlepinParameterParseException in project candlepin by candlepin.
the class JaxRsExceptionResponseBuilder method getResponse.
/**
* Exceptions are thrown by the JAX-RS implementation when problems such as
* incorrect types of parameters for JAX-RS resources are passed to a
* method. Example would be string instead of a number passed as a
* parameter. This method takes such an exception and builds pretty
* undestandable HTTP resoponse for the client.
*
* @param exception
* JAX-RS implementation generated exception.
* @return Response for the client
*/
public final Response getResponse(final Exception exception) {
if (!canHandle(exception)) {
throw new IllegalArgumentException("ResponseBuilder cannot handle this exception, you should call canHandle first", exception);
}
Map<String, String> map = VersionUtil.getVersionMap();
ResponseBuilder bldr = Response.status(Status.BAD_REQUEST).type(cem.determineBestMediaType()).header(VersionUtil.VERSION_HEADER, map.get("version") + "-" + map.get("release"));
Throwable cause = exception.getCause();
if (cause instanceof CandlepinParameterParseException) {
String msg = i18n.get().tr("Invalid format for query parameter {0}. Expected format: {1}", ((CandlepinParameterParseException) cause).getParamName(), ((CandlepinParameterParseException) cause).getExpectedFormat());
bldr.entity(new ExceptionMessage(msg));
} else {
String msg = exception.getMessage();
Matcher paramMatcher = PARAM_REGEX.matcher(msg);
Matcher illegalValMatcher = ILLEGAL_VAL_REGEX.matcher(msg);
paramMatcher.find();
illegalValMatcher.find();
String errorMessage = i18n.get().tr("{0} is not a valid value for {1}", illegalValMatcher.group(1), paramMatcher.group(1));
bldr.entity(new ExceptionMessage(errorMessage));
}
return bldr.build();
}
use of org.candlepin.common.exceptions.CandlepinParameterParseException in project candlepin by candlepin.
the class JaxRsExceptionResponseBuilder method canHandle.
/**
* This method inspects exception and decides whether it relates to wrong
* parameters passed to a JAX-RS resource method.
* @param exception
* JAX-RS implementation generated exception.
* @return True if and only if the exception contains error regarding JAX-RS
* parameters
*/
public final boolean canHandle(final Exception exception) {
Throwable cause = exception.getCause();
if (cause instanceof CandlepinParameterParseException) {
return true;
}
String msg = exception.getMessage();
if (StringUtils.isNotEmpty(msg)) {
Matcher paramMatcher = PARAM_REGEX.matcher(msg);
Matcher illegalValMatcher = ILLEGAL_VAL_REGEX.matcher(msg);
if (paramMatcher.find() && illegalValMatcher.find()) {
if ((paramMatcher.groupCount() == 2) && (illegalValMatcher.groupCount() == 2)) {
return true;
}
}
}
return false;
}
use of org.candlepin.common.exceptions.CandlepinParameterParseException in project candlepin by candlepin.
the class JaxRsExceptionResponseBuilderTest method candlepinParserError.
@Test
public void candlepinParserError() {
CandlepinParameterParseException parseEx = new CandlepinParameterParseException("paramName", "thisFormat");
RuntimeException ex = new RuntimeException(parseEx);
Response resp = exceptionBuilder.getResponse(ex);
ExceptionMessage e = (ExceptionMessage) resp.getEntity();
assertTrue(exceptionBuilder.canHandle(ex));
assertTrue(e.getDisplayMessage().contains("paramName"));
assertTrue(e.getDisplayMessage().contains("thisFormat"));
}
Aggregations