Search in sources :

Example 6 with ApiError

use of com.poc.restfulpoc.config.ApiError in project POC by rajadilipkolli.

the class RestExceptionHandler method handleHttpMessageNotReadable.

/**
 * {@inheritDoc}
 *
 * Handle HttpMessageNotReadableException. Happens when request JSON is malformed.
 */
@Override
protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
    final ServletWebRequest servletWebRequest = (ServletWebRequest) request;
    log.info("{} to {}", servletWebRequest.getHttpMethod(), servletWebRequest.getRequest().getServletPath());
    final String error = "Malformed JSON request";
    return buildResponseEntity(new ApiError(BAD_REQUEST, error, ex));
}
Also used : ApiError(com.poc.restfulpoc.config.ApiError) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest)

Example 7 with ApiError

use of com.poc.restfulpoc.config.ApiError in project POC by rajadilipkolli.

the class RestExceptionHandler method handleMethodArgumentTypeMismatch.

/**
 * Handle Exception, handle generic Exception.class
 *
 * @param ex the Exception
 * @return the ApiError object
 * @param request a {@link org.springframework.web.context.request.WebRequest} object.
 */
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
protected ResponseEntity<Object> handleMethodArgumentTypeMismatch(MethodArgumentTypeMismatchException ex, WebRequest request) {
    final ApiError apiError = new ApiError(BAD_REQUEST);
    apiError.setMessage(String.format("The parameter '%s' of value '%s' could not be converted to type '%s'", ex.getName(), ex.getValue(), ex.getRequiredType().getSimpleName()));
    apiError.setDebugMessage(ex.getMessage());
    return buildResponseEntity(apiError);
}
Also used : ApiError(com.poc.restfulpoc.config.ApiError) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Aggregations

ApiError (com.poc.restfulpoc.config.ApiError)7 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)3 ResponseEntityExceptionHandler (org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler)3 HttpHeaders (org.springframework.http.HttpHeaders)1 ResponseEntity (org.springframework.http.ResponseEntity)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)1