All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bidib.wizard.server.error.RestResponseEntityExceptionHandler Maven / Gradle / Ivy

There is a newer version: 2.0.29
Show newest version
package org.bidib.wizard.server.error;

import org.bidib.wizard.common.exception.ConnectionException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

    public RestResponseEntityExceptionHandler() {
        super();
    }

    // 404

    @ExceptionHandler(value = { ConnectionException.class })
    protected ResponseEntity handleNotFound(final RuntimeException ex, final WebRequest request) {
        String bodyOfResponse = ex.getMessage();
        // if (ex.getCause() != null && StringUtils.isNotBlank(ex.getCause().getMessage())) {
        // bodyOfResponse = ex.getCause().getMessage();
        // }
        return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
    }

    // 500

    @ExceptionHandler({ NullPointerException.class, IllegalArgumentException.class, IllegalStateException.class })
    /* 500 */public ResponseEntity handleInternal(final RuntimeException ex, final WebRequest request) {
        logger.error("500 Status Code", ex);
        final String bodyOfResponse = "This should be application specific";
        return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR,
            request);
    }
}