public final class BusinessException extends Exception
Under normal conditions client data is validated by client logic and reach the server side normalized. Anyway, there may be complex business constrains between data items, data that is persisted on server. In order to avoid server communication delays and adding complexity to client, data validation logic uses this exception to signal a constrain is not fulfilled. Client side logic will display an alert based on this exception error code.
Developers note: business exception instance is sent back to client code via HTTP-RMI using status code 400 - HttpServletResponse.SC_BAD_REQUEST and JSON serialization.
public void saveEmployer(Employer employer) throws BusinessException { BusinessRules.uniqueEmployerClientCode(employer); BusinessRules.uniqueEmployerName(employer); dao.saveEmployer(employer); } public class BusinessRules { private static final int NOT_UNIQUE_CLIENT_CODE = 0x0001; private static final int NOT_UNIQUE_EMPLOYER_NAME = 0x0002; public static void uniqueEmployerClientCode(Employer employer) throws BusinessException { if (dao.getEmployerIdByClientCode(clientCode) != employer.getId()) { throw new BusinessException(NOT_UNIQUE_CLIENT_CODE); } } public static void uniqueEmployerName(Employer employer) throws BusinessException { if (dao.getEmployerIdByName(name) != employer.getId()) { throw new BusinessException(NOT_UNIQUE_EMPLOYER_NAME); } } }Note that this exception is checked since it is desirable to explicitly state we are going to use it.
Modifier and Type | Field and Description |
---|---|
private int |
errorCode
Business logic error code.
|
private static long |
serialVersionUID
Java serialization version.
|
Constructor and Description |
---|
BusinessException()
Default constructor for JSON serialization.
|
BusinessException(int errorCode)
Create immutable instance.
|
Modifier and Type | Method and Description |
---|---|
int |
getErrorCode()
Get business logic error code.
|
String |
getMessage()
Get this business error code formated as 8 digits hexadecimal number.
|
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
private static final long serialVersionUID
private final int errorCode
public BusinessException()
public BusinessException(int errorCode)
errorCode
- business logic error code.public int getErrorCode()
public String getMessage()
getMessage
in class Throwable
Copyright © 2019. All rights reserved.