Search in sources :

Example 1 with CandlepinParameterParseException

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();
}
Also used : CandlepinParameterParseException(org.candlepin.common.exceptions.CandlepinParameterParseException) ExceptionMessage(org.candlepin.common.exceptions.ExceptionMessage) Matcher(java.util.regex.Matcher) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

Example 2 with CandlepinParameterParseException

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;
}
Also used : CandlepinParameterParseException(org.candlepin.common.exceptions.CandlepinParameterParseException) Matcher(java.util.regex.Matcher)

Example 3 with CandlepinParameterParseException

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"));
}
Also used : CandlepinParameterParseException(org.candlepin.common.exceptions.CandlepinParameterParseException) Response(javax.ws.rs.core.Response) ExceptionMessage(org.candlepin.common.exceptions.ExceptionMessage) Test(org.junit.Test)

Aggregations

CandlepinParameterParseException (org.candlepin.common.exceptions.CandlepinParameterParseException)3 Matcher (java.util.regex.Matcher)2 ExceptionMessage (org.candlepin.common.exceptions.ExceptionMessage)2 Response (javax.ws.rs.core.Response)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 Test (org.junit.Test)1