FINERACT-1724: Fix error code for Batch API

This commit is contained in:
Adam Saghy
2023-04-15 10:29:42 +02:00
committed by Arnold Gálovics
parent 54ff12f824
commit d570e359ae
6 changed files with 181 additions and 62 deletions
@@ -19,6 +19,7 @@
package org.apache.fineract.batch.exception;
import com.google.gson.Gson;
import org.apache.fineract.infrastructure.core.data.ApiGlobalErrorResponse;
import org.apache.fineract.infrastructure.core.exception.AbstractPlatformDomainRuleException;
import org.apache.fineract.infrastructure.core.exception.AbstractPlatformResourceNotFoundException;
import org.apache.fineract.infrastructure.core.exception.PlatformApiDataValidationException;
@@ -32,8 +33,10 @@ import org.apache.fineract.infrastructure.core.exceptionmapper.PlatformInternalS
import org.apache.fineract.infrastructure.core.exceptionmapper.PlatformResourceNotFoundExceptionMapper;
import org.apache.fineract.infrastructure.core.exceptionmapper.UnsupportedParameterExceptionMapper;
import org.apache.fineract.infrastructure.core.serialization.GoogleGsonSerializerHelper;
import org.apache.fineract.infrastructure.jobs.exception.LoanIdsHardLockedException;
import org.apache.fineract.portfolio.loanaccount.exception.MultiDisbursementDataRequiredException;
import org.apache.fineract.portfolio.loanproduct.exception.LinkedAccountRequiredException;
import org.apache.http.HttpStatus;
import org.springframework.dao.NonTransientDataAccessException;
import org.springframework.transaction.TransactionException;
@@ -49,7 +52,7 @@ import org.springframework.transaction.TransactionException;
*/
public class ErrorHandler extends RuntimeException {
private static Gson jsonHelper = GoogleGsonSerializerHelper.createGsonBuilder(true).create();
private static final Gson jsonHelper = GoogleGsonSerializerHelper.createGsonBuilder(true).create();
/**
* Sole Constructor
@@ -69,63 +72,66 @@ public class ErrorHandler extends RuntimeException {
if (exception instanceof AbstractPlatformDomainRuleException) {
PlatformDomainRuleExceptionMapper mapper = new PlatformDomainRuleExceptionMapper();
final String errorBody = jsonHelper.toJson(mapper.toResponse((AbstractPlatformDomainRuleException) exception).getEntity());
return new ErrorInfo(500, 9999, errorBody);
return new ErrorInfo(HttpStatus.SC_INTERNAL_SERVER_ERROR, 9999, errorBody);
} else if (exception instanceof AbstractPlatformResourceNotFoundException) {
final PlatformResourceNotFoundExceptionMapper mapper = new PlatformResourceNotFoundExceptionMapper();
final String errorBody = jsonHelper
.toJson(mapper.toResponse((AbstractPlatformResourceNotFoundException) exception).getEntity());
return new ErrorInfo(404, 1001, errorBody);
return new ErrorInfo(HttpStatus.SC_NOT_FOUND, 1001, errorBody);
} else if (exception instanceof UnsupportedParameterException) {
final UnsupportedParameterExceptionMapper mapper = new UnsupportedParameterExceptionMapper();
final String errorBody = jsonHelper.toJson(mapper.toResponse((UnsupportedParameterException) exception).getEntity());
return new ErrorInfo(400, 2001, errorBody);
return new ErrorInfo(HttpStatus.SC_BAD_REQUEST, 2001, errorBody);
} else if (exception instanceof PlatformApiDataValidationException) {
final PlatformApiDataValidationExceptionMapper mapper = new PlatformApiDataValidationExceptionMapper();
final String errorBody = jsonHelper.toJson(mapper.toResponse((PlatformApiDataValidationException) exception).getEntity());
return new ErrorInfo(400, 2002, errorBody);
return new ErrorInfo(HttpStatus.SC_BAD_REQUEST, 2002, errorBody);
} else if (exception instanceof PlatformDataIntegrityException) {
final PlatformDataIntegrityExceptionMapper mapper = new PlatformDataIntegrityExceptionMapper();
final String errorBody = jsonHelper.toJson(mapper.toResponse((PlatformDataIntegrityException) exception).getEntity());
return new ErrorInfo(403, 3001, errorBody);
return new ErrorInfo(HttpStatus.SC_FORBIDDEN, 3001, errorBody);
} else if (exception instanceof LinkedAccountRequiredException) {
final PlatformDomainRuleExceptionMapper mapper = new PlatformDomainRuleExceptionMapper();
final String errorBody = jsonHelper.toJson(mapper.toResponse((LinkedAccountRequiredException) exception).getEntity());
return new ErrorInfo(403, 3002, errorBody);
return new ErrorInfo(HttpStatus.SC_FORBIDDEN, 3002, errorBody);
} else if (exception instanceof MultiDisbursementDataRequiredException) {
final PlatformDomainRuleExceptionMapper mapper = new PlatformDomainRuleExceptionMapper();
final String errorBody = jsonHelper.toJson(mapper.toResponse((MultiDisbursementDataRequiredException) exception).getEntity());
return new ErrorInfo(403, 3003, errorBody);
return new ErrorInfo(HttpStatus.SC_FORBIDDEN, 3003, errorBody);
} else if (exception instanceof TransactionException) {
return new ErrorInfo(400, 4001, "{\"Exception\": " + exception.getMessage() + "}");
return new ErrorInfo(HttpStatus.SC_BAD_REQUEST, 4001, "{\"Exception\": " + exception.getMessage() + "}");
} else if (exception instanceof PlatformInternalServerException) {
final PlatformInternalServerExceptionMapper mapper = new PlatformInternalServerExceptionMapper();
final String errorBody = jsonHelper.toJson(mapper.toResponse((PlatformInternalServerException) exception).getEntity());
return new ErrorInfo(500, 5001, errorBody);
return new ErrorInfo(HttpStatus.SC_INTERNAL_SERVER_ERROR, 5001, errorBody);
} else if (exception instanceof NonTransientDataAccessException) {
return new ErrorInfo(400, 4001, "{\"Exception\": " + exception.getMessage() + "}");
return new ErrorInfo(HttpStatus.SC_BAD_REQUEST, 4001, "{\"Exception\": " + exception.getMessage() + "}");
} else if (exception instanceof LoanIdsHardLockedException e) {
String message = ApiGlobalErrorResponse.loanIsLocked(e.getLoanIdFromRequest()).toJson();
return new ErrorInfo(HttpStatus.SC_CONFLICT, 4090, message);
}
return new ErrorInfo(500, 9999, "{\"Exception\": " + exception.toString() + "}");
return new ErrorInfo(HttpStatus.SC_INTERNAL_SERVER_ERROR, 9999, "{\"Exception\": " + exception.toString() + "}");
}
}
@@ -0,0 +1,29 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.fineract.infrastructure.jobs.exception;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
@Getter
public class LoanIdsHardLockedException extends RuntimeException {
private final Long loanIdFromRequest;
}
@@ -31,7 +31,6 @@ import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
@@ -45,6 +44,7 @@ import org.apache.fineract.infrastructure.core.data.ApiGlobalErrorResponse;
import org.apache.fineract.infrastructure.core.domain.ExternalId;
import org.apache.fineract.infrastructure.core.filters.BatchRequestPreprocessor;
import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
import org.apache.fineract.infrastructure.jobs.exception.LoanIdsHardLockedException;
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
import org.apache.fineract.portfolio.loanaccount.domain.GLIMAccountInfoRepository;
import org.apache.fineract.portfolio.loanaccount.domain.GroupLoanIndividualMonitoringAccount;
@@ -86,13 +86,6 @@ public class LoanCOBApiFilter extends OncePerRequestFilter implements BatchReque
private final PlatformTransactionManager transactionManager;
@RequiredArgsConstructor
@Getter
private static class LoanIdsHardLockedException extends RuntimeException {
private final Long loanIdFromRequest;
}
private static class Reject {
private final String message;