add API validation
This commit is contained in:
@@ -5,10 +5,12 @@ import lombok.Getter;
|
||||
import org.iban4j.Iban4jException;
|
||||
import org.springframework.core.NestedExceptionUtils;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.orm.jpa.JpaObjectRetrievalFailureException;
|
||||
import org.springframework.orm.jpa.JpaSystemException;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
@@ -65,21 +67,38 @@ public class RestResponseEntityExceptionHandler
|
||||
@ExceptionHandler(Throwable.class)
|
||||
protected ResponseEntity<CustomErrorResponse> handleOtherExceptions(
|
||||
final Throwable exc, final WebRequest request) {
|
||||
final var message = firstLine(NestedExceptionUtils.getMostSpecificCause(exc).getMessage());
|
||||
final var message = firstMessageLine(NestedExceptionUtils.getMostSpecificCause(exc));
|
||||
return errorResponse(request, httpStatus(message).orElse(HttpStatus.INTERNAL_SERVER_ERROR), message);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked,rawtypes")
|
||||
protected ResponseEntity handleMethodArgumentNotValid(
|
||||
MethodArgumentNotValidException exc,
|
||||
HttpHeaders headers,
|
||||
HttpStatus status,
|
||||
WebRequest request) {
|
||||
final var errorList = exc
|
||||
.getBindingResult()
|
||||
.getFieldErrors()
|
||||
.stream()
|
||||
.map(fieldError -> fieldError.getField() + " " + fieldError.getDefaultMessage() + " but is \""
|
||||
+ fieldError.getRejectedValue() + "\"")
|
||||
.toList();
|
||||
return errorResponse(request, HttpStatus.BAD_REQUEST, errorList.toString());
|
||||
}
|
||||
|
||||
private String userReadableEntityClassName(final String exceptionMessage) {
|
||||
final var regex = "(net.hostsharing.hsadminng.[a-z0-9_.]*.[A-Za-z0-9_$]*Entity) ";
|
||||
final var pattern = Pattern.compile(regex);
|
||||
final var matcher = pattern.matcher(exceptionMessage);
|
||||
if (matcher.find()) {
|
||||
final var entityName = matcher.group(1);
|
||||
final var entityClass = resolveClassOrNull(entityName);
|
||||
if (entityClass != null) {
|
||||
return (entityClass.isAnnotationPresent(DisplayName.class)
|
||||
? exceptionMessage.replace(entityName, entityClass.getAnnotation(DisplayName.class).value())
|
||||
: exceptionMessage.replace(entityName, entityClass.getSimpleName()))
|
||||
final var entityClass = resolveClass(entityName);
|
||||
if (entityClass.isPresent()) {
|
||||
return (entityClass.get().isAnnotationPresent(DisplayName.class)
|
||||
? exceptionMessage.replace(entityName, entityClass.get().getAnnotation(DisplayName.class).value())
|
||||
: exceptionMessage.replace(entityName, entityClass.get().getSimpleName()))
|
||||
.replace(" with id ", " with uuid ");
|
||||
}
|
||||
|
||||
@@ -87,11 +106,11 @@ public class RestResponseEntityExceptionHandler
|
||||
return exceptionMessage;
|
||||
}
|
||||
|
||||
private static Class<?> resolveClassOrNull(final String entityName) {
|
||||
private static Optional<Class<?>> resolveClass(final String entityName) {
|
||||
try {
|
||||
return ClassLoader.getSystemClassLoader().loadClass(entityName);
|
||||
return Optional.of(ClassLoader.getSystemClassLoader().loadClass(entityName));
|
||||
} catch (ClassNotFoundException e) {
|
||||
return null;
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,6 +134,13 @@ public class RestResponseEntityExceptionHandler
|
||||
new CustomErrorResponse(request.getContextPath(), httpStatus, message), httpStatus);
|
||||
}
|
||||
|
||||
private String firstMessageLine(final Throwable exception) {
|
||||
if (exception.getMessage() != null) {
|
||||
return firstLine(exception.getMessage());
|
||||
}
|
||||
return "ERROR: [500] " + exception.getClass().getName();
|
||||
}
|
||||
|
||||
private String firstLine(final String message) {
|
||||
return message.split("\\r|\\n|\\r\\n", 0)[0];
|
||||
}
|
||||
|
@@ -0,0 +1,6 @@
|
||||
@NonNullApi
|
||||
@NonNullFields
|
||||
package net.hostsharing.hsadminng.errors;
|
||||
|
||||
import org.springframework.lang.NonNullApi;
|
||||
import org.springframework.lang.NonNullFields;
|
@@ -19,7 +19,6 @@ import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBui
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.UUID;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
|
@@ -22,7 +22,7 @@ components:
|
||||
type: string
|
||||
vatCountryCode:
|
||||
type: string
|
||||
pattern: '^[A_Z][A-Z]$'
|
||||
pattern: '^[A-Z][A-Z]$'
|
||||
vatBusiness:
|
||||
type: boolean
|
||||
refundBankAccount:
|
||||
@@ -40,7 +40,7 @@ components:
|
||||
nullable: true
|
||||
vatCountryCode:
|
||||
type: string
|
||||
pattern: '^[A_Z][A-Z]$'
|
||||
pattern: '^[A-Z][A-Z]$'
|
||||
nullable: true
|
||||
vatBusiness:
|
||||
type: boolean
|
||||
@@ -56,9 +56,11 @@ components:
|
||||
partnerUuid:
|
||||
type: string
|
||||
format: uuid
|
||||
nullable: false
|
||||
billingContactUuid:
|
||||
type: string
|
||||
format: uuid
|
||||
nullable: false
|
||||
debitorNumber:
|
||||
type: integer
|
||||
format: int32
|
||||
@@ -68,7 +70,7 @@ components:
|
||||
type: string
|
||||
vatCountryCode:
|
||||
type: string
|
||||
pattern: '^[A_Z][A-Z]$'
|
||||
pattern: '^[A-Z][A-Z]$'
|
||||
vatBusiness:
|
||||
type: boolean
|
||||
refundBankAccountUuid:
|
||||
|
@@ -15,16 +15,21 @@ components:
|
||||
$ref: './hs-office-contact-schemas.yaml#/components/schemas/HsOfficeContact'
|
||||
registrationOffice:
|
||||
type: string
|
||||
nullable: true
|
||||
registrationNumber:
|
||||
type: string
|
||||
nullable: true
|
||||
birthName:
|
||||
type: string
|
||||
nullable: true
|
||||
birthday:
|
||||
type: string
|
||||
format: date
|
||||
nullable: true
|
||||
dateOfDeath:
|
||||
type: string
|
||||
format: date
|
||||
nullable: true
|
||||
|
||||
HsOfficePartnerPatch:
|
||||
type: object
|
||||
@@ -84,8 +89,3 @@ components:
|
||||
required:
|
||||
- personUuid
|
||||
- contactUuid
|
||||
- registrationOffice
|
||||
- registrationNumber
|
||||
- birthName
|
||||
- birthday
|
||||
- dateOfDeath
|
||||
|
Reference in New Issue
Block a user