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());
}
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());
}
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;
}
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;
}
Aggregations