Search in sources :

Example 21 with DefaultErrorModel

use of eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel in project CzechIdMng by bcvsolutions.

the class ExceptionControllerAdvice method handleError.

/**
 * Handle exceptions from http (~ authentication) filters.
 *
 * @see RestErrorAttributes
 * @param request
 * @param response
 * @return error models
 * @since 10.7.0
 */
@RequestMapping(path = { "/error" })
public ResponseEntity<?> handleError(HttpServletRequest request, HttpServletResponse response) {
    Object exception = request.getAttribute(javax.servlet.RequestDispatcher.ERROR_EXCEPTION);
    // 
    if (exception == null || !(exception instanceof Throwable)) {
        // if source exception is not set (e.g. from controller security ~ forbidden), return base error attributes.
        RestErrorAttributes attributes = new RestErrorAttributes();
        attributes.resolveException(request, response, null, null);
        Map<String, Object> errorAttributes = attributes.getErrorAttributes(new ServletWebRequest(request, response), false);
        // 
        // fill http status
        HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
        if (errorAttributes.containsKey(RestErrorAttributes.ATTRIBUTE_STATUS)) {
            int statusCode = (int) errorAttributes.get(RestErrorAttributes.ATTRIBUTE_STATUS);
            httpStatus = HttpStatus.valueOf(statusCode);
        }
        // move error as IdM errors
        if (errorAttributes.containsKey(RestErrorAttributes.ATTRIBUTE_ERROR)) {
            errorAttributes.put(ResultModels.ATTRIBUTE_ERRORS, Lists.newArrayList(errorAttributes.get(RestErrorAttributes.ATTRIBUTE_ERROR)));
            errorAttributes.remove(RestErrorAttributes.ATTRIBUTE_ERROR);
        }
        // Return original error attributes => to cover all cases, when even "unknown" exception can be thrown (without our error).
        return new ResponseEntity<>(errorAttributes, createHttpHeaders(request, response), httpStatus);
    }
    // 
    Throwable cause = Throwables.getRootCause((Throwable) exception);
    HttpHeaders httpHeaders = createHttpHeaders(request, response);
    // 
    if (cause instanceof ResultCodeException) {
        ResultCodeException resultCodeException = (ResultCodeException) cause;
        ExceptionUtils.log(LOG, resultCodeException);
        // 
        return new ResponseEntity<>(resultCodeException.getError(), httpHeaders, resultCodeException.getStatus());
    }
    // 
    ErrorModel errorModel = new DefaultErrorModel(CoreResultCode.INTERNAL_SERVER_ERROR, cause.getMessage());
    ExceptionUtils.log(LOG, errorModel, cause);
    // 
    return new ResponseEntity<>(new ResultModels(errorModel), httpHeaders, errorModel.getStatus());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpStatus(org.springframework.http.HttpStatus) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel) ResultModels(eu.bcvsolutions.idm.core.api.dto.ResultModels) ResponseEntity(org.springframework.http.ResponseEntity) DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel) ErrorModel(eu.bcvsolutions.idm.core.api.exception.ErrorModel) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 22 with DefaultErrorModel

use of eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel in project CzechIdMng by bcvsolutions.

the class ExceptionControllerAdvice method handle.

/**
 * Upload file size exceeded.
 *
 * @param ex handled  exception
 * @return error model
 * @since 10.2.0
 */
@ExceptionHandler(MaxUploadSizeExceededException.class)
public ResponseEntity<ResultModels> handle(MaxUploadSizeExceededException ex) {
    long maxUploadSize = ex.getMaxUploadSize();
    // 
    String effectiveMaxUploadSize;
    if (maxUploadSize <= 0) {
        effectiveMaxUploadSize = configurationService.getValue("spring.servlet.multipart.max-file-size", // -1 as default
        String.valueOf(maxUploadSize));
    } else {
        effectiveMaxUploadSize = FileUtils.byteCountToDisplaySize(maxUploadSize);
    }
    // 
    ErrorModel errorModel = new DefaultErrorModel(CoreResultCode.ATTACHMENT_SIZE_LIMIT_EXCEEDED, ex.getMessage(), ImmutableMap.of("actualSize", effectiveMaxUploadSize));
    LOG.warn("[" + errorModel.getId() + "] ", ex);
    return new ResponseEntity<>(new ResultModels(errorModel), new HttpHeaders(), errorModel.getStatus());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel) ErrorModel(eu.bcvsolutions.idm.core.api.exception.ErrorModel) DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel) ResultModels(eu.bcvsolutions.idm.core.api.dto.ResultModels) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 23 with DefaultErrorModel

use of eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel in project CzechIdMng by bcvsolutions.

the class RestErrorAttributes method getErrorAttributes.

@Override
public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
    Throwable error = getError(webRequest);
    Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, includeStackTrace);
    // 
    ErrorModel errorModel = null;
    if (error instanceof ResultCodeException) {
        errorModel = ((ResultCodeException) error).getError().getError();
    } else {
        if (errorAttributes.containsKey(ATTRIBUTE_STATUS)) {
            int status = (int) errorAttributes.get(ATTRIBUTE_STATUS);
            switch(status) {
                case 401:
                    {
                        errorModel = new DefaultErrorModel(CoreResultCode.LOG_IN, ImmutableMap.of("path", errorAttributes.get("path")));
                        break;
                    }
                case 403:
                    {
                        errorModel = new DefaultErrorModel(CoreResultCode.FORBIDDEN, ImmutableMap.of("path", errorAttributes.get("path"), "message", errorAttributes.get("message")));
                        break;
                    }
                case 404:
                    {
                        errorModel = new DefaultErrorModel(CoreResultCode.ENDPOINT_NOT_FOUND, ImmutableMap.of("path", errorAttributes.get("path"), "message", errorAttributes.get("message")));
                        break;
                    }
                case 400:
                case 405:
                    {
                        errorModel = new DefaultErrorModel(CoreResultCode.METHOD_NOT_ALLOWED, ImmutableMap.of("path", errorAttributes.get("path"), "message", errorAttributes.get("message")));
                        break;
                    }
                default:
                    {
                        errorModel = null;
                    }
            }
        }
    }
    if (errorModel == null) {
        LOG.error("Error not resolved - errorAttributes needs extension for error attrs [{}]", errorAttributes);
        return errorAttributes;
    }
    errorAttributes.put(ATTRIBUTE_ERROR, errorModel);
    LOG.warn(errorModel.toString());
    // 
    return errorAttributes;
}
Also used : ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel) ErrorModel(eu.bcvsolutions.idm.core.api.exception.ErrorModel) DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel)

Example 24 with DefaultErrorModel

use of eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel in project CzechIdMng by bcvsolutions.

the class NotificationTemplateRedeployBulkAction method prevalidate.

@Override
public ResultModels prevalidate() {
    ResultModels results = super.prevalidate();
    // 
    // add info message about classpath
    String redeployFolder = getConfigurationService().getValue(IdmNotificationTemplateService.TEMPLATE_FOLDER);
    if (StringUtils.isNotEmpty(redeployFolder)) {
        ResultModel result = new DefaultErrorModel(CoreResultCode.DEPLOY_TEMPLATE_FOLDER_FOUND, ImmutableMap.of("redeployFolder", redeployFolder));
        results.addInfo(result);
    }
    // 
    return results;
}
Also used : ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel) ResultModels(eu.bcvsolutions.idm.core.api.dto.ResultModels)

Aggregations

DefaultErrorModel (eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel)24 ResultModels (eu.bcvsolutions.idm.core.api.dto.ResultModels)14 ErrorModel (eu.bcvsolutions.idm.core.api.exception.ErrorModel)14 HttpHeaders (org.springframework.http.HttpHeaders)11 ResponseEntity (org.springframework.http.ResponseEntity)11 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)10 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)10 AbstractVerifiableUnitTest (eu.bcvsolutions.idm.test.api.AbstractVerifiableUnitTest)7 Test (org.junit.Test)7 CoreException (eu.bcvsolutions.idm.core.api.exception.CoreException)6 ResultModel (eu.bcvsolutions.idm.core.api.dto.ResultModel)3 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)2 DefaultResultModel (eu.bcvsolutions.idm.core.api.dto.DefaultResultModel)1 Recoverable (eu.bcvsolutions.idm.core.api.service.Recoverable)1 Method (java.lang.reflect.Method)1 AsyncUncaughtExceptionHandler (org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler)1 HttpStatus (org.springframework.http.HttpStatus)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)1