com.vmware.connectors.common.web.ExceptionHandlers Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of connectors-config Show documentation
Show all versions of connectors-config Show documentation
Common config code for developing connectors
/*
* Copyright © 2017 VMware, Inc. All Rights Reserved.
* SPDX-License-Identifier: BSD-2-Clause
*/
package com.vmware.connectors.common.web;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.http.ResponseEntity.BodyBuilder;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.support.WebExchangeBindException;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Collectors;
import static com.vmware.connectors.common.utils.CommonUtils.BACKEND_STATUS;
import static org.springframework.http.HttpStatus.*;
import static org.springframework.http.MediaType.APPLICATION_JSON;
/**
* Created by Rob Worsnop on 4/11/17.
*/
@ControllerAdvice
public class ExceptionHandlers {
private final static Logger logger = LoggerFactory.getLogger(ExceptionHandlers.class);
// Handles validation exceptions
@ExceptionHandler(WebExchangeBindException.class)
@ResponseStatus(BAD_REQUEST)
@ResponseBody
public Map> handleValidationException(WebExchangeBindException e) {
Map errorMap = e.getBindingResult().getFieldErrors().stream()
.collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
return Collections.singletonMap("errors", errorMap);
}
@ExceptionHandler(UserException.class)
@ResponseBody
public ResponseEntity