work through cleaner implementation of error handling support for http api for the office resource.

This commit is contained in:
Keith Woodlock
2012-05-09 23:33:33 +01:00
parent 7ad484bc2e
commit 008ec86c9a
32 changed files with 1071 additions and 297 deletions
@@ -0,0 +1,32 @@
package org.mifosng.data;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="messageArg")
public class ApiErrorMessageArg {
/**
* The actual value of the parameter (if any) as passed to API.
*/
private Object value;
public static ApiErrorMessageArg from(Object object) {
return new ApiErrorMessageArg(object);
}
protected ApiErrorMessageArg() {
//
}
public ApiErrorMessageArg(Object object) {
this.value = object;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
}
@@ -0,0 +1,174 @@
package org.mifosng.data;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
*/
@XmlRootElement
public class ApiGlobalErrorResponse {
/**
* A developer friendly plain English description of why the HTTP error response was returned from the API.
*/
private String developerMessage;
/**
* A link to a URL which provides more information on the details of this type of error.
*/
private String developerDocLink;
/**
* The HTTP status code of the response.
*/
private String httpStatusCode;
/**
* A user friendly plain English description of why the HTTP error response was returned from the API that can be presented to end users.
*/
private String defaultUserMessage;
/**
* A code that can be used for globalisation support by client applications of the API.
*/
private String userMessageGlobalisationCode;
/**
* A list of zero or more of the actual reasons for the HTTP error should they be needed. Typically used to express data validation errors with parameters passed to API.
*/
private List<ApiParameterError> errors = new ArrayList<ApiParameterError>();
public static ApiGlobalErrorResponse unAuthenticated() {
ApiGlobalErrorResponse globalErrorResponse = new ApiGlobalErrorResponse();
globalErrorResponse.setHttpStatusCode("401");
globalErrorResponse.setDeveloperMessage("No authentication details were passed in api request.");
globalErrorResponse.setDeveloperDocLink("https://github.com/keithwoodlock/mifosx/wiki/HTTP-API-Error-codes");
globalErrorResponse.setUserMessageGlobalisationCode("error.msg.not.authenticated");
globalErrorResponse.setDefaultUserMessage("Unauthenticated. Please login.");
return globalErrorResponse;
}
public static ApiGlobalErrorResponse unAuthorized() {
ApiGlobalErrorResponse globalErrorResponse = new ApiGlobalErrorResponse();
globalErrorResponse.setHttpStatusCode("403");
globalErrorResponse.setDeveloperMessage("The user associated with credentials passed on this request does not have sufficient privileges to perform this action.");
globalErrorResponse.setDeveloperDocLink("https://github.com/keithwoodlock/mifosx/wiki/HTTP-API-Error-codes");
globalErrorResponse.setUserMessageGlobalisationCode("error.msg.not.authorized");
globalErrorResponse.setDefaultUserMessage("Insufficient priviledges to perform this action.");
return globalErrorResponse;
}
public static ApiGlobalErrorResponse notFound(String globalisationMessageCode, String defaultUserMessage, Object... defaultUserMessageArgs) {
ApiGlobalErrorResponse globalErrorResponse = new ApiGlobalErrorResponse();
globalErrorResponse.setHttpStatusCode("404");
globalErrorResponse.setDeveloperMessage("The requested resource is not available.");
globalErrorResponse.setDeveloperDocLink("https://github.com/keithwoodlock/mifosx/wiki/HTTP-API-Error-codes");
globalErrorResponse.setUserMessageGlobalisationCode("error.msg.resource.not.found");
globalErrorResponse.setDefaultUserMessage("The requested resource is not available.");
List<ApiParameterError> errors = new ArrayList<ApiParameterError>();
errors.add(ApiParameterError.resourceIdentifierNotFound(globalisationMessageCode, defaultUserMessage, defaultUserMessageArgs));
globalErrorResponse.setErrors(errors);
return globalErrorResponse;
}
public static ApiGlobalErrorResponse dataIntegrityError(final String globalisationMessageCode, final String defaultUserMessage, final String parameterName, final Object... defaultUserMessageArgs) {
ApiGlobalErrorResponse globalErrorResponse = new ApiGlobalErrorResponse();
globalErrorResponse.setHttpStatusCode("400");
globalErrorResponse.setDeveloperMessage("The request caused a data integrity issue to be fired by the database.");
globalErrorResponse.setDeveloperDocLink("https://github.com/keithwoodlock/mifosx/wiki/HTTP-API-Error-codes");
globalErrorResponse.setUserMessageGlobalisationCode(globalisationMessageCode);
globalErrorResponse.setDefaultUserMessage(defaultUserMessage);
List<ApiParameterError> errors = new ArrayList<ApiParameterError>();
errors.add(ApiParameterError.parameterError(globalisationMessageCode, defaultUserMessage, parameterName, defaultUserMessageArgs));
globalErrorResponse.setErrors(errors);
return globalErrorResponse;
}
public static ApiGlobalErrorResponse badClientRequest(final String globalisationMessageCode, final String defaultUserMessage,
List<ApiParameterError> errors) {
ApiGlobalErrorResponse globalErrorResponse = new ApiGlobalErrorResponse();
globalErrorResponse.setHttpStatusCode("400");
globalErrorResponse.setDeveloperMessage("The request was invalid. This typically will happen due to validation errors which are provided.");
globalErrorResponse.setDeveloperDocLink("https://github.com/keithwoodlock/mifosx/wiki/HTTP-API-Error-codes");
globalErrorResponse.setUserMessageGlobalisationCode(globalisationMessageCode);
globalErrorResponse.setDefaultUserMessage(defaultUserMessage);
globalErrorResponse.setErrors(errors);
return globalErrorResponse;
}
protected ApiGlobalErrorResponse() {
//
}
public ApiGlobalErrorResponse(final List<ApiParameterError> errors) {
this.errors = errors;
}
@XmlElementWrapper(name="errors")
@XmlElement(name="errorResponse")
public List<ApiParameterError> getErrors() {
return errors;
}
public void setErrors(List<ApiParameterError> errors) {
this.errors = errors;
}
public String getDeveloperMessage() {
return developerMessage;
}
public void setDeveloperMessage(String developerMessage) {
this.developerMessage = developerMessage;
}
public String getHttpStatusCode() {
return httpStatusCode;
}
public void setHttpStatusCode(String httpStatusCode) {
this.httpStatusCode = httpStatusCode;
}
public String getDefaultUserMessage() {
return defaultUserMessage;
}
public void setDefaultUserMessage(String defaultUserMessage) {
this.defaultUserMessage = defaultUserMessage;
}
public String getUserMessageGlobalisationCode() {
return userMessageGlobalisationCode;
}
public void setUserMessageGlobalisationCode(String userMessageGlobalisationCode) {
this.userMessageGlobalisationCode = userMessageGlobalisationCode;
}
public String getDeveloperDocLink() {
return developerDocLink;
}
public void setDeveloperDocLink(String developerDocLink) {
this.developerDocLink = developerDocLink;
}
}
@@ -0,0 +1,120 @@
package org.mifosng.data;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="apiParameterError")
public class ApiParameterError {
/**
* A developer friendly plain English description of why the HTTP error response was returned from the API.
*/
private String developerMessage;
/**
* A user friendly plain English description of why the HTTP error response was returned from the API that can be presented to end users.
*/
private String defaultUserMessage;
/**
* A code that can be used for globalisation support by client applications of the API.
*/
private String userMessageGlobalisationCode;
/**
* The name of the field or parameter passed to the API that this error relates to.
*/
private String parameterName;
/**
* The actual value of the parameter (if any) as passed to API.
*/
private Object value;
/**
* Arguments related to the user error message.
*/
private List<ApiErrorMessageArg> args = new ArrayList<ApiErrorMessageArg>();
public static ApiParameterError resourceIdentifierNotFound(String globalisationMessageCode, String defaultUserMessage, Object... defaultUserMessageArgs) {
return new ApiParameterError(globalisationMessageCode, defaultUserMessage, defaultUserMessageArgs);
}
public static ApiParameterError parameterError(final String globalisationMessageCode, final String defaultUserMessage,
final String parameterName, final Object... defaultUserMessageArgs) {
ApiParameterError error = new ApiParameterError(globalisationMessageCode, defaultUserMessage, defaultUserMessageArgs);
error.setParameterName(parameterName);
return error;
}
protected ApiParameterError() {
//
}
private ApiParameterError(final String globalisationMessageCode, final String defaultUserMessage, final Object[] defaultUserMessageArgs) {
this.userMessageGlobalisationCode = globalisationMessageCode;
this.developerMessage = defaultUserMessage;
this.defaultUserMessage = defaultUserMessage;
List<ApiErrorMessageArg> messageArgs = new ArrayList<ApiErrorMessageArg>();
if (defaultUserMessageArgs != null) {
for (Object object : defaultUserMessageArgs) {
messageArgs.add(ApiErrorMessageArg.from(object));
}
}
this.args = messageArgs;
this.parameterName="identifier";
}
public String getDeveloperMessage() {
return developerMessage;
}
public void setDeveloperMessage(String developerMessage) {
this.developerMessage = developerMessage;
}
public String getDefaultUserMessage() {
return defaultUserMessage;
}
public void setDefaultUserMessage(String defaultUserMessage) {
this.defaultUserMessage = defaultUserMessage;
}
public String getUserMessageGlobalisationCode() {
return userMessageGlobalisationCode;
}
public void setUserMessageGlobalisationCode(String userMessageGlobalisationCode) {
this.userMessageGlobalisationCode = userMessageGlobalisationCode;
}
public String getParameterName() {
return parameterName;
}
public void setParameterName(String parameterName) {
this.parameterName = parameterName;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public List<ApiErrorMessageArg> getArgs() {
return args;
}
public void setArgs(List<ApiErrorMessageArg> args) {
this.args = args;
}
}
@@ -33,6 +33,7 @@ public class UpdateLoanProductCommand implements LoanProductCommandData {
repaymentEvery, repaymentFrequency, numberOfRepayments, amortizationMethod, toleranceAmount, flexibleRepaymentSchedule, interestRebateAllowed);
}
@Override
public String getName() {
return this.name;
}
@@ -41,6 +42,7 @@ public class UpdateLoanProductCommand implements LoanProductCommandData {
this.name = name;
}
@Override
public String getDescription() {
return this.description;
}
@@ -49,6 +51,7 @@ public class UpdateLoanProductCommand implements LoanProductCommandData {
this.description = description;
}
@Override
public String getCurrencyCode() {
return this.commonLoanProperties.getCurrencyCode();
}
@@ -57,6 +60,7 @@ public class UpdateLoanProductCommand implements LoanProductCommandData {
this.commonLoanProperties.setCurrencyCode(currencyCode);
}
@Override
public Integer getDigitsAfterDecimal() {
return this.commonLoanProperties.getDigitsAfterDecimal();
}
@@ -65,6 +69,7 @@ public class UpdateLoanProductCommand implements LoanProductCommandData {
this.commonLoanProperties.setDigitsAfterDecimal(digitsAfterDecimal);
}
@Override
public Integer getRepaymentEvery() {
return this.commonLoanProperties.getRepaymentEvery();
}
@@ -73,6 +78,7 @@ public class UpdateLoanProductCommand implements LoanProductCommandData {
this.commonLoanProperties.setRepaymentEvery(repaymentEvery);
}
@Override
public Integer getRepaymentFrequency() {
return this.commonLoanProperties.getRepaymentFrequency();
}
@@ -81,6 +87,7 @@ public class UpdateLoanProductCommand implements LoanProductCommandData {
this.commonLoanProperties.setRepaymentFrequency(repaymentFrequency);
}
@Override
public Boolean isFlexibleRepaymentSchedule() {
return this.commonLoanProperties.isFlexibleRepaymentSchedule();
}
@@ -90,6 +97,7 @@ public class UpdateLoanProductCommand implements LoanProductCommandData {
this.commonLoanProperties.setFlexibleRepaymentSchedule(flexibleRepaymentSchedule);
}
@Override
public Boolean isInterestRebateAllowed() {
return this.commonLoanProperties.isInterestRebateAllowed();
}
@@ -98,6 +106,7 @@ public class UpdateLoanProductCommand implements LoanProductCommandData {
this.commonLoanProperties.setInterestRebateAllowed(interestRebateAllowed);
}
@Override
public BigDecimal getPrincipal() {
return this.commonLoanProperties.getPrincipal();
}
@@ -114,6 +123,7 @@ public class UpdateLoanProductCommand implements LoanProductCommandData {
// return this.commonLoanProperties.getInterestRebateAllowed();
// }
@Override
public Integer getInterestRateFrequencyMethod() {
return commonLoanProperties.getInterestRateFrequencyMethod();
}
@@ -122,6 +132,7 @@ public class UpdateLoanProductCommand implements LoanProductCommandData {
this.commonLoanProperties.setInterestRateFrequencyMethod(interestRateFrequencyMethod);
}
@Override
public Integer getInterestMethod() {
return commonLoanProperties.getInterestMethod();
}
@@ -130,6 +141,7 @@ public class UpdateLoanProductCommand implements LoanProductCommandData {
this.commonLoanProperties.setInterestMethod(interestMethod);
}
@Override
public Integer getAmortizationMethod() {
return commonLoanProperties.getAmortizationMethod();
}
@@ -138,6 +150,7 @@ public class UpdateLoanProductCommand implements LoanProductCommandData {
this.commonLoanProperties.setAmortizationMethod(amortizationMethod);
}
@Override
public Integer getNumberOfRepayments() {
return commonLoanProperties.getNumberOfRepayments();
}
@@ -146,6 +159,7 @@ public class UpdateLoanProductCommand implements LoanProductCommandData {
this.commonLoanProperties.setNumberOfRepayments(numberOfRepayments);
}
@Override
public BigDecimal getInterestRatePerPeriod() {
return commonLoanProperties.getInterestRatePerPeriod();
}
@@ -154,6 +168,7 @@ public class UpdateLoanProductCommand implements LoanProductCommandData {
this.commonLoanProperties.setInterestRatePerPeriod(interestRatePerPeriod);
}
@Override
public CommonLoanProperties getCommonLoanProperties() {
return commonLoanProperties;
}
@@ -162,6 +177,7 @@ public class UpdateLoanProductCommand implements LoanProductCommandData {
this.commonLoanProperties = commonLoanProperties;
}
@Override
public BigDecimal getInArrearsToleranceAmount() {
return this.commonLoanProperties.getInArrearsToleranceAmount();
}