Search in sources :

Example 91 with BadRequestException

use of org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException in project carbon-apimgt by wso2.

the class GlobalThrowableMapper method toResponse.

@Override
public Response toResponse(Throwable e) {
    if (e instanceof ClientErrorException) {
        log.error("Client error", e);
        return ((ClientErrorException) e).getResponse();
    }
    if (e instanceof NotFoundException) {
        log.error("Resource not found", e);
        return ((NotFoundException) e).getResponse();
    }
    if (e instanceof PreconditionFailedException) {
        log.error("Precondition failed", e);
        return ((PreconditionFailedException) e).getResponse();
    }
    if (e instanceof BadRequestException) {
        log.error("Bad request", e);
        return ((BadRequestException) e).getResponse();
    }
    if (e instanceof ConstraintViolationException) {
        log.error("Constraint violation", e);
        return ((ConstraintViolationException) e).getResponse();
    }
    if (e instanceof ForbiddenException) {
        log.error("Resource forbidden", e);
        return ((ForbiddenException) e).getResponse();
    }
    if (e instanceof ConflictException) {
        log.error("Conflict", e);
        return ((ConflictException) e).getResponse();
    }
    if (e instanceof MethodNotAllowedException) {
        log.error("Method not allowed", e);
        return ((MethodNotAllowedException) e).getResponse();
    }
    if (e instanceof InternalServerErrorException) {
        String errorMessage = "The server encountered an internal error : " + e.getMessage();
        log.error(errorMessage, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).type(MediaType.APPLICATION_JSON_TYPE).entity(e500).build();
    }
    if (e instanceof JsonParseException) {
        String errorMessage = "Malformed request body.";
        log.error(errorMessage, e);
        // noinspection ThrowableResultOfMethodCallIgnored
        return RestApiUtil.buildBadRequestException(errorMessage).getResponse();
    }
    if (e instanceof JsonMappingException) {
        if (e instanceof UnrecognizedPropertyException) {
            UnrecognizedPropertyException unrecognizedPropertyException = (UnrecognizedPropertyException) e;
            String unrecognizedProperty = unrecognizedPropertyException.getPropertyName();
            String errorMessage = "Unrecognized property '" + unrecognizedProperty + "'";
            log.error(errorMessage, e);
            // noinspection ThrowableResultOfMethodCallIgnored
            return RestApiUtil.buildBadRequestException(errorMessage).getResponse();
        } else {
            String errorMessage = "One or more request body parameters contain disallowed values.";
            log.error(errorMessage, e);
            // noinspection ThrowableResultOfMethodCallIgnored
            return RestApiUtil.buildBadRequestException(errorMessage).getResponse();
        }
    }
    if (e instanceof AuthenticationException) {
        ErrorDTO errorDetail = new ErrorDTO();
        errorDetail.setCode((long) 401);
        errorDetail.setMoreInfo("");
        errorDetail.setMessage("");
        errorDetail.setDescription(e.getMessage());
        return Response.status(Response.Status.UNAUTHORIZED).type(MediaType.APPLICATION_JSON_TYPE).entity(errorDetail).build();
    }
    // This occurs when received an empty body in an occasion where the body is mandatory
    if (e instanceof EOFException) {
        String errorMessage = "Request payload cannot be empty.";
        log.error(errorMessage, e);
        // noinspection ThrowableResultOfMethodCallIgnored
        return RestApiUtil.buildBadRequestException(errorMessage).getResponse();
    }
    if (e instanceof APIManagementException) {
        ErrorHandler selectedErrorHandler = null;
        List<Throwable> throwableList = ExceptionUtils.getThrowableList(e);
        for (Throwable t : throwableList) {
            if (t instanceof APIManagementException) {
                APIManagementException apimException = (APIManagementException) t;
                ErrorHandler errorHandler = apimException.getErrorHandler();
                if (errorHandler != null) {
                    if (selectedErrorHandler == null) {
                        selectedErrorHandler = errorHandler;
                    } else {
                        selectedErrorHandler = errorHandler.getHttpStatusCode() < selectedErrorHandler.getHttpStatusCode() && errorHandler.getHttpStatusCode() > 0 ? errorHandler : selectedErrorHandler;
                    }
                }
            }
        }
        if (selectedErrorHandler != null) {
            // logs the error as the error may be not logged by the origin
            if (selectedErrorHandler.printStackTrace()) {
                log.error("A defined exception has been captured and mapped to an HTTP response " + "by the global exception mapper ", e);
            } else {
                // Not to log the stack trace due to error code was mark as not print stacktrace.
                log.error(e.getMessage());
                if (log.isDebugEnabled()) {
                    log.debug("A defined exception has been captured and mapped to an HTTP response " + "by the global exception mapper ", e);
                }
            }
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(selectedErrorHandler);
            return Response.status(Response.Status.fromStatusCode(selectedErrorHandler.getHttpStatusCode())).type(MediaType.APPLICATION_JSON_TYPE).entity(errorDTO).build();
        }
    }
    // unknown exception log and return
    log.error("An unknown exception has been captured by the global exception mapper.", e);
    return Response.status(Response.Status.INTERNAL_SERVER_ERROR).type(MediaType.APPLICATION_JSON_TYPE).entity(e500).build();
}
Also used : ErrorHandler(org.wso2.carbon.apimgt.api.ErrorHandler) AuthenticationException(org.apache.cxf.interceptor.security.AuthenticationException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) UnrecognizedPropertyException(com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) EOFException(java.io.EOFException) ClientErrorException(javax.ws.rs.ClientErrorException)

Example 92 with BadRequestException

use of org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException in project carbon-apimgt by wso2.

the class RestApiUtil method handleBadRequest.

/**
 * Logs the error, builds a BadRequestException with specified details and throws it
 *
 * @param msg error message
 * @param log Log instance
 * @throws BadRequestException
 */
public static void handleBadRequest(String msg, Log log) throws BadRequestException {
    BadRequestException badRequestException = buildBadRequestException(msg);
    log.error(msg);
    throw badRequestException;
}
Also used : BadRequestException(org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException)

Example 93 with BadRequestException

use of org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException in project carbon-apimgt by wso2.

the class RestApiUtil method handleBadRequest.

/**
 * Logs the error, builds a BadRequestException with specified details and throws it
 *
 * @param msg error message
 * @param e throwable to log
 * @param log Log instance
 * @throws BadRequestException
 */
public static void handleBadRequest(String msg, Throwable e, Log log) throws BadRequestException {
    BadRequestException badRequestException = buildBadRequestException(msg);
    log.error(msg, e);
    throw badRequestException;
}
Also used : BadRequestException(org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException)

Example 94 with BadRequestException

use of org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException in project carbon-apimgt by wso2.

the class RestApiUtil method handleBadRequest.

/**
 * Logs the error, builds a BadRequestException with specified details and throws it
 *
 * @param msg error message
 * @param log Log instance
 * @param code error code
 * @throws BadRequestException
 */
public static void handleBadRequest(String msg, Long code, Log log) throws BadRequestException {
    BadRequestException badRequestException = buildBadRequestException(msg, code);
    log.error(msg);
    throw badRequestException;
}
Also used : BadRequestException(org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException)

Aggregations

BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)63 CharonException (org.wso2.charon3.core.exceptions.CharonException)31 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)30 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)27 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)23 HashMap (java.util.HashMap)22 Attribute (org.wso2.charon3.core.attributes.Attribute)20 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)19 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)19 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)19 NotFoundException (org.wso2.charon3.core.exceptions.NotFoundException)18 JSONObject (org.json.JSONObject)16 AbstractSCIMObject (org.wso2.charon3.core.objects.AbstractSCIMObject)16 JSONException (org.json.JSONException)15 JSONEncoder (org.wso2.charon3.core.encoder.JSONEncoder)15 JSONDecoder (org.wso2.charon3.core.encoder.JSONDecoder)14 NotImplementedException (org.wso2.charon3.core.exceptions.NotImplementedException)14 User (org.wso2.charon3.core.objects.User)12 JSONArray (org.json.JSONArray)11 BadRequestException (org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException)10