tidy up resource not found exception handling.

This commit is contained in:
Keith Woodlock
2012-05-29 13:46:56 +01:00
parent 41493b5c49
commit d413aadb4a
15 changed files with 62 additions and 48 deletions
@@ -20,8 +20,9 @@ import org.mifosng.data.AdditionalFieldsSet;
import org.mifosng.data.reports.GenericResultset; import org.mifosng.data.reports.GenericResultset;
import org.mifosng.data.reports.ResultsetColumnHeader; import org.mifosng.data.reports.ResultsetColumnHeader;
import org.mifosng.data.reports.ResultsetDataRow; import org.mifosng.data.reports.ResultsetDataRow;
import org.mifosng.platform.exceptions.AdditionalFieldsNotFoundException;
import org.mifosng.platform.exceptions.PlatformDataIntegrityException; import org.mifosng.platform.exceptions.PlatformDataIntegrityException;
import org.mifosng.platform.exceptions.PlatformResourceNotFoundException; import org.mifosng.platform.exceptions.ReportNotFoundException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -311,10 +312,7 @@ public class ReadExtraDataAndReportingServiceImpl implements
if (rs.next()) { if (rs.next()) {
sql = rs.getString("the_sql"); sql = rs.getString("the_sql");
} else { } else {
throw new PlatformResourceNotFoundException( throw new ReportNotFoundException(inputSql);
"error.msg.report.name.not.found",
"Reporting Meta Data Entry Not Found", "Input Sql: "
+ inputSql);
} }
db_statement.close(); db_statement.close();
@@ -476,18 +474,12 @@ public class ReadExtraDataAndReportingServiceImpl implements
} while (rs.next()); } while (rs.next());
result.setData(resultsetDataRows); result.setData(resultsetDataRows);
} else { } else {
throw new PlatformResourceNotFoundException( throw new AdditionalFieldsNotFoundException(type, id);
"error.msg.type.value.not.found",
"Additional Fields Type: " + type
+ " Id Not Found for " + id);
} }
db_statement3.close(); db_statement3.close();
db_statement3 = null; db_statement3 = null;
} else { } else {
throw new PlatformResourceNotFoundException( throw new AdditionalFieldsNotFoundException(type, set);
"error.msg.set.not.found",
"Additional Fields Set Not Found", "Type: " + type
+ " Set: " + set);
} }
db_statement1.close(); db_statement1.close();
db_statement1 = null; db_statement1 = null;
@@ -576,10 +568,7 @@ public class ReadExtraDataAndReportingServiceImpl implements
db_statement = null; db_statement = null;
db_connection.close(); db_connection.close();
db_connection = null; db_connection = null;
throw new PlatformResourceNotFoundException( throw new AdditionalFieldsNotFoundException(type, set);
"error.msg.set.not.found",
"Additional Fields Set Not Found", "Type: " + type
+ " Set: " + set);
} }
db_statement.close(); db_statement.close();
db_statement = null; db_statement = null;
@@ -618,10 +607,7 @@ public class ReadExtraDataAndReportingServiceImpl implements
db_statement = null; db_statement = null;
db_connection.close(); db_connection.close();
db_connection = null; db_connection = null;
throw new PlatformResourceNotFoundException( throw new AdditionalFieldsNotFoundException(type, id);
"error.msg.type.value.not.found",
"Additional Fields Type: " + type
+ " Id Not Found for " + id);
} }
db_statement.close(); db_statement.close();
db_statement = null; db_statement = null;
@@ -6,22 +6,22 @@ import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider; import javax.ws.rs.ext.Provider;
import org.mifosng.data.ApiGlobalErrorResponse; import org.mifosng.data.ApiGlobalErrorResponse;
import org.mifosng.platform.exceptions.PlatformResourceNotFoundException; import org.mifosng.platform.exceptions.AbstractPlatformResourceNotFoundException;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
* An {@link ExceptionMapper} to map {@link PlatformResourceNotFoundException} thrown by platform into a HTTP API friendly format. * An {@link ExceptionMapper} to map {@link AbstractPlatformResourceNotFoundException} thrown by platform into a HTTP API friendly format.
* *
* The {@link PlatformResourceNotFoundException} is thrown when an api call for a resource that is expected to exist does not. * The {@link AbstractPlatformResourceNotFoundException} is thrown when an api call for a resource that is expected to exist does not.
*/ */
@Provider @Provider
@Component @Component
@Scope("singleton") @Scope("singleton")
public class PlatformResourceNotFoundExceptionMapper implements ExceptionMapper<PlatformResourceNotFoundException> { public class PlatformResourceNotFoundExceptionMapper implements ExceptionMapper<AbstractPlatformResourceNotFoundException> {
@Override @Override
public Response toResponse(PlatformResourceNotFoundException exception) { public Response toResponse(AbstractPlatformResourceNotFoundException exception) {
ApiGlobalErrorResponse notFoundErrorResponse = ApiGlobalErrorResponse.notFound(exception.getGlobalisationMessageCode(), exception.getDefaultUserMessage(), exception.getDefaultUserMessageArgs()); ApiGlobalErrorResponse notFoundErrorResponse = ApiGlobalErrorResponse.notFound(exception.getGlobalisationMessageCode(), exception.getDefaultUserMessage(), exception.getDefaultUserMessageArgs());
return Response.status(Status.NOT_FOUND).entity(notFoundErrorResponse).build(); return Response.status(Status.NOT_FOUND).entity(notFoundErrorResponse).build();
@@ -3,13 +3,13 @@ package org.mifosng.platform.exceptions;
/** /**
* A {@link RuntimeException} thrown when resources that are queried for are not found. * A {@link RuntimeException} thrown when resources that are queried for are not found.
*/ */
public class PlatformResourceNotFoundException extends RuntimeException { public abstract class AbstractPlatformResourceNotFoundException extends RuntimeException {
private final String globalisationMessageCode; private final String globalisationMessageCode;
private final String defaultUserMessage; private final String defaultUserMessage;
private final Object[] defaultUserMessageArgs; private final Object[] defaultUserMessageArgs;
public PlatformResourceNotFoundException(String globalisationMessageCode, String defaultUserMessage, Object... defaultUserMessageArgs) { public AbstractPlatformResourceNotFoundException(String globalisationMessageCode, String defaultUserMessage, Object... defaultUserMessageArgs) {
this.globalisationMessageCode = globalisationMessageCode; this.globalisationMessageCode = globalisationMessageCode;
this.defaultUserMessage = defaultUserMessage; this.defaultUserMessage = defaultUserMessage;
this.defaultUserMessageArgs = defaultUserMessageArgs; this.defaultUserMessageArgs = defaultUserMessageArgs;
@@ -0,0 +1,17 @@
package org.mifosng.platform.exceptions;
/**
* A {@link RuntimeException} thrown when client resources are not found.
*/
public class AdditionalFieldsNotFoundException extends AbstractPlatformResourceNotFoundException {
public AdditionalFieldsNotFoundException(final String type, final Long id) {
super("error.msg.type.value.not.found", "Additional Fields Type: "
+ type + " Id Not Found for " + id);
}
public AdditionalFieldsNotFoundException(final String type, final String set) {
super("error.msg.set.not.found", "Additional Fields Set Not Found",
"Type: " + type + " Set: " + set);
}
}
@@ -3,7 +3,7 @@ package org.mifosng.platform.exceptions;
/** /**
* A {@link RuntimeException} thrown when client resources are not found. * A {@link RuntimeException} thrown when client resources are not found.
*/ */
public class ClientNotFoundException extends PlatformResourceNotFoundException { public class ClientNotFoundException extends AbstractPlatformResourceNotFoundException {
public ClientNotFoundException(Long id) { public ClientNotFoundException(Long id) {
super("error.msg.client.id.invalid", "Client with identifier " + id + " does not exist", id); super("error.msg.client.id.invalid", "Client with identifier " + id + " does not exist", id);
@@ -3,7 +3,7 @@ package org.mifosng.platform.exceptions;
/** /**
* A {@link RuntimeException} thrown when loan resources are not found. * A {@link RuntimeException} thrown when loan resources are not found.
*/ */
public class CurrencyNotFoundException extends PlatformResourceNotFoundException { public class CurrencyNotFoundException extends AbstractPlatformResourceNotFoundException {
public CurrencyNotFoundException(final String currencyCode) { public CurrencyNotFoundException(final String currencyCode) {
super("error.msg.currency.currencyCode.invalid", "Currency with identifier " + currencyCode + " does not exist", currencyCode); super("error.msg.currency.currencyCode.invalid", "Currency with identifier " + currencyCode + " does not exist", currencyCode);
@@ -3,7 +3,7 @@ package org.mifosng.platform.exceptions;
/** /**
* A {@link RuntimeException} thrown when loan resources are not found. * A {@link RuntimeException} thrown when loan resources are not found.
*/ */
public class LoanNotFoundException extends PlatformResourceNotFoundException { public class LoanNotFoundException extends AbstractPlatformResourceNotFoundException {
public LoanNotFoundException(Long id) { public LoanNotFoundException(Long id) {
super("error.msg.loan.id.invalid", "Loan with identifier " + id + " does not exist", id); super("error.msg.loan.id.invalid", "Loan with identifier " + id + " does not exist", id);
@@ -3,7 +3,7 @@ package org.mifosng.platform.exceptions;
/** /**
* A {@link RuntimeException} thrown when loan product resources are not found. * A {@link RuntimeException} thrown when loan product resources are not found.
*/ */
public class LoanProductNotFoundException extends PlatformResourceNotFoundException { public class LoanProductNotFoundException extends AbstractPlatformResourceNotFoundException {
public LoanProductNotFoundException(Long id) { public LoanProductNotFoundException(Long id) {
super("error.msg.loanproduct.id.invalid", "Loan product with identifier " + id + " does not exist", id); super("error.msg.loanproduct.id.invalid", "Loan product with identifier " + id + " does not exist", id);
@@ -3,7 +3,7 @@ package org.mifosng.platform.exceptions;
/** /**
* A {@link RuntimeException} thrown when loan resources are not found. * A {@link RuntimeException} thrown when loan resources are not found.
*/ */
public class LoanTransactionNotFoundException extends PlatformResourceNotFoundException { public class LoanTransactionNotFoundException extends AbstractPlatformResourceNotFoundException {
public LoanTransactionNotFoundException(Long id) { public LoanTransactionNotFoundException(Long id) {
super("error.msg.loan.id.invalid", "Loan with identifier " + id + " does not exist", id); super("error.msg.loan.id.invalid", "Loan with identifier " + id + " does not exist", id);
@@ -3,7 +3,7 @@ package org.mifosng.platform.exceptions;
/** /**
* A {@link RuntimeException} thrown when note resources are not found. * A {@link RuntimeException} thrown when note resources are not found.
*/ */
public class NoteNotFoundException extends PlatformResourceNotFoundException { public class NoteNotFoundException extends AbstractPlatformResourceNotFoundException {
public NoteNotFoundException(Long id) { public NoteNotFoundException(Long id) {
super("error.msg.note.id.invalid", "Note with identifier " + id + " does not exist", id); super("error.msg.note.id.invalid", "Note with identifier " + id + " does not exist", id);
@@ -3,7 +3,7 @@ package org.mifosng.platform.exceptions;
/** /**
* A {@link RuntimeException} thrown when office resources are not found. * A {@link RuntimeException} thrown when office resources are not found.
*/ */
public class OfficeNotFoundException extends PlatformResourceNotFoundException { public class OfficeNotFoundException extends AbstractPlatformResourceNotFoundException {
public OfficeNotFoundException(Long id) { public OfficeNotFoundException(Long id) {
super("error.msg.office.id.invalid", "Office with identifier " + id + " does not exist", id); super("error.msg.office.id.invalid", "Office with identifier " + id + " does not exist", id);
@@ -0,0 +1,11 @@
package org.mifosng.platform.exceptions;
/**
* A {@link RuntimeException} thrown when report resources are not found.
*/
public class ReportNotFoundException extends AbstractPlatformResourceNotFoundException {
public ReportNotFoundException(final String reportSql) {
super("error.msg.report.name.not.found", "Reporting Meta Data Entry Not Found", "Input Sql: " + reportSql);
}
}
@@ -3,7 +3,7 @@ package org.mifosng.platform.exceptions;
/** /**
* A {@link RuntimeException} thrown when role resources are not found. * A {@link RuntimeException} thrown when role resources are not found.
*/ */
public class RoleNotFoundException extends PlatformResourceNotFoundException { public class RoleNotFoundException extends AbstractPlatformResourceNotFoundException {
public RoleNotFoundException(Long id) { public RoleNotFoundException(Long id) {
super("error.msg.role.id.invalid", "Role with identifier " + id + " does not exist", id); super("error.msg.role.id.invalid", "Role with identifier " + id + " does not exist", id);
@@ -3,7 +3,7 @@ package org.mifosng.platform.exceptions;
/** /**
* A {@link RuntimeException} thrown when user resources are not found. * A {@link RuntimeException} thrown when user resources are not found.
*/ */
public class UserNotFoundException extends PlatformResourceNotFoundException { public class UserNotFoundException extends AbstractPlatformResourceNotFoundException {
public UserNotFoundException(Long id) { public UserNotFoundException(Long id) {
super("error.msg.user.id.invalid", "User with identifier " + id + " does not exist", id); super("error.msg.user.id.invalid", "User with identifier " + id + " does not exist", id);
@@ -24,9 +24,9 @@ import org.mifosng.platform.client.domain.Note;
import org.mifosng.platform.client.domain.NoteRepository; import org.mifosng.platform.client.domain.NoteRepository;
import org.mifosng.platform.currency.domain.MonetaryCurrency; import org.mifosng.platform.currency.domain.MonetaryCurrency;
import org.mifosng.platform.currency.domain.Money; import org.mifosng.platform.currency.domain.Money;
import org.mifosng.platform.exceptions.LoanNotFoundException;
import org.mifosng.platform.exceptions.NoAuthorizationException; import org.mifosng.platform.exceptions.NoAuthorizationException;
import org.mifosng.platform.exceptions.PlatformDomainRuleException; import org.mifosng.platform.exceptions.PlatformDomainRuleException;
import org.mifosng.platform.exceptions.PlatformResourceNotFoundException;
import org.mifosng.platform.loan.domain.AmortizationMethod; import org.mifosng.platform.loan.domain.AmortizationMethod;
import org.mifosng.platform.loan.domain.DefaultLoanLifecycleStateMachine; import org.mifosng.platform.loan.domain.DefaultLoanLifecycleStateMachine;
import org.mifosng.platform.loan.domain.InterestCalculationPeriodMethod; import org.mifosng.platform.loan.domain.InterestCalculationPeriodMethod;
@@ -164,7 +164,7 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
Loan loan = this.loanRepository.findOne(loansThatMatch(currentUser.getOrganisation(), loanId)); Loan loan = this.loanRepository.findOne(loansThatMatch(currentUser.getOrganisation(), loanId));
if (loan == null) { if (loan == null) {
throw new PlatformResourceNotFoundException("error.msg.loan.id.invalid", "Loan with identifier {0} does not exist", loanId); throw new LoanNotFoundException(loanId);
} }
if (loan.isNotSubmittedAndPendingApproval()) { if (loan.isNotSubmittedAndPendingApproval()) {
@@ -192,7 +192,7 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
Loan loan = this.loanRepository.findOne(loansThatMatch(currentUser.getOrganisation(), command.getLoanId())); Loan loan = this.loanRepository.findOne(loansThatMatch(currentUser.getOrganisation(), command.getLoanId()));
if (loan == null) { if (loan == null) {
throw new PlatformResourceNotFoundException("error.msg.loan.id.invalid", "Loan with identifier {0} does not exist", command.getLoanId()); throw new LoanNotFoundException(command.getLoanId());
} }
if (this.isBeforeToday(command.getEventDate()) && currentUser.canNotApproveLoanInPast()) { if (this.isBeforeToday(command.getEventDate()) && currentUser.canNotApproveLoanInPast()) {
@@ -218,7 +218,7 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
Loan loan = this.loanRepository.findOne(loansThatMatch(currentUser.getOrganisation(), command.getLoanId())); Loan loan = this.loanRepository.findOne(loansThatMatch(currentUser.getOrganisation(), command.getLoanId()));
if (loan == null) { if (loan == null) {
throw new PlatformResourceNotFoundException("error.msg.loan.id.invalid", "Loan with identifier {0} does not exist", command.getLoanId()); throw new LoanNotFoundException(command.getLoanId());
} }
loan.undoApproval(defaultLoanLifecycleStateMachine()); loan.undoApproval(defaultLoanLifecycleStateMachine());
@@ -243,7 +243,7 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
Loan loan = this.loanRepository.findOne(loansThatMatch(currentUser.getOrganisation(), command.getLoanId())); Loan loan = this.loanRepository.findOne(loansThatMatch(currentUser.getOrganisation(), command.getLoanId()));
if (loan == null) { if (loan == null) {
throw new PlatformResourceNotFoundException("error.msg.loan.id.invalid", "Loan with identifier {0} does not exist", command.getLoanId()); throw new LoanNotFoundException(command.getLoanId());
} }
if (this.isBeforeToday(command.getEventDate()) && currentUser.canNotRejectLoanInPast()) { if (this.isBeforeToday(command.getEventDate()) && currentUser.canNotRejectLoanInPast()) {
@@ -272,7 +272,7 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
Loan loan = this.loanRepository.findOne(loansThatMatch(currentUser.getOrganisation(), command.getLoanId())); Loan loan = this.loanRepository.findOne(loansThatMatch(currentUser.getOrganisation(), command.getLoanId()));
if (loan == null) { if (loan == null) {
throw new PlatformResourceNotFoundException("error.msg.loan.id.invalid", "Loan with identifier {0} does not exist", command.getLoanId()); throw new LoanNotFoundException(command.getLoanId());
} }
if (this.isBeforeToday(command.getEventDate()) && currentUser.canNotWithdrawByClientLoanInPast()) { if (this.isBeforeToday(command.getEventDate()) && currentUser.canNotWithdrawByClientLoanInPast()) {
@@ -301,7 +301,7 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
Loan loan = this.loanRepository.findOne(loansThatMatch(currentUser.getOrganisation(), command.getLoanId())); Loan loan = this.loanRepository.findOne(loansThatMatch(currentUser.getOrganisation(), command.getLoanId()));
if (loan == null) { if (loan == null) {
throw new PlatformResourceNotFoundException("error.msg.loan.id.invalid", "Loan with identifier {0} does not exist", command.getLoanId()); throw new LoanNotFoundException(command.getLoanId());
} }
if (this.isBeforeToday(command.getEventDate()) && currentUser.canNotDisburseLoanInPast()) { if (this.isBeforeToday(command.getEventDate()) && currentUser.canNotDisburseLoanInPast()) {
@@ -388,7 +388,7 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
Loan loan = this.loanRepository.findOne(loansThatMatch(currentUser.getOrganisation(), command.getLoanId())); Loan loan = this.loanRepository.findOne(loansThatMatch(currentUser.getOrganisation(), command.getLoanId()));
if (loan == null) { if (loan == null) {
throw new PlatformResourceNotFoundException("error.msg.loan.id.invalid", "Loan with identifier {0} does not exist", command.getLoanId()); throw new LoanNotFoundException(command.getLoanId());
} }
if (loan.isActualDisbursedOnDateEarlierOrLaterThanExpected()) { if (loan.isActualDisbursedOnDateEarlierOrLaterThanExpected()) {
@@ -418,7 +418,7 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
Loan loan = this.loanRepository.findOne(loansThatMatch(currentUser.getOrganisation(), command.getLoanId())); Loan loan = this.loanRepository.findOne(loansThatMatch(currentUser.getOrganisation(), command.getLoanId()));
if (loan == null) { if (loan == null) {
throw new PlatformResourceNotFoundException("error.msg.loan.id.invalid", "Loan with identifier {0} does not exist", command.getLoanId()); throw new LoanNotFoundException(command.getLoanId());
} }
if (this.isBeforeToday(command.getTransactionDate()) && currentUser.canNotMakeRepaymentOnLoanInPast()) { if (this.isBeforeToday(command.getTransactionDate()) && currentUser.canNotMakeRepaymentOnLoanInPast()) {
@@ -453,7 +453,7 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
Loan loan = this.loanRepository.findOne(loansThatMatch(currentUser.getOrganisation(), command.getLoanId())); Loan loan = this.loanRepository.findOne(loansThatMatch(currentUser.getOrganisation(), command.getLoanId()));
if (loan == null) { if (loan == null) {
throw new PlatformResourceNotFoundException("error.msg.loan.id.invalid", "Loan with identifier {0} does not exist", command.getLoanId()); throw new LoanNotFoundException(command.getLoanId());
} }
LoanTransaction transactionToAdjust = this.loanTransactionRepository LoanTransaction transactionToAdjust = this.loanTransactionRepository
@@ -495,7 +495,7 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
Loan loan = this.loanRepository.findOne(loansThatMatch(currentUser.getOrganisation(), command.getLoanId())); Loan loan = this.loanRepository.findOne(loansThatMatch(currentUser.getOrganisation(), command.getLoanId()));
if (loan == null) { if (loan == null) {
throw new PlatformResourceNotFoundException("error.msg.loan.id.invalid", "Loan with identifier {0} does not exist", command.getLoanId()); throw new LoanNotFoundException(command.getLoanId());
} }
Money waived = Money.of(loan.getLoanRepaymentScheduleDetail() Money waived = Money.of(loan.getLoanRepaymentScheduleDetail()