Skip to content
Snippets Groups Projects
Commit 08e5dfa8 authored by Riabokon Stanislav(EPAM)[GCP]'s avatar Riabokon Stanislav(EPAM)[GCP]
Browse files

Merge branch 'gcp/feature/exception-mapping-for-invalid-body' into 'master'

Partition Empty 400 response (GONRG-4710)

See merge request !164
parents 191fd870 15e3746e
No related branches found
No related tags found
2 merge requests!229Merge branch 'dependency-upgrade' into 'master',!164Partition Empty 400 response (GONRG-4710)
Pipeline #105181 passed with warnings
/*
* Copyright 2020-2021 Google LLC
* Copyright 2020-2021 EPAM Systems, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opengroup.osdu.partition.provider.gcp.middleware;
import java.util.Arrays;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.partition.middleware.GlobalExceptionMapper;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.WebRequest;
@ControllerAdvice
@RestController
public class GcpExceptionMapper extends GlobalExceptionMapper {
private static final String NOT_VALID_REQUEST_BODY = "Passed arguments were not valid. Reasons: %s";
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
StringBuilder errors = new StringBuilder();
for (ObjectError error : ex.getAllErrors()) {
errors
.append('[')
.append("arguments: ")
.append(Arrays.toString(error.getArguments()))
.append(", message: ")
.append(error.getDefaultMessage())
.append("]");
}
return super.getErrorResponse(
new AppException(
status.value(),
"Validation error.",
String.format(NOT_VALID_REQUEST_BODY, errors)));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment