TrustFactor Errors
To make it easier to understand the causes of an error we made a specific error type with domain and parameters.
info
Swift: Error domains are enums making it easier to spot differences if any of them change. If switching, the compiler will force you to handle all the cases.
- iOS (Swift)
- Android (Java)
Error definition
struct Error: Error {
public let domain: Domain
public let parameters: [String: Any]
}
Domains
enum Error.Domain {
case client(code: ClientDomainCode)
case authentication(code: AuthenticationDomainCode)
case network(code: NetworkDomainCode)
case operationDecision(code: OperationDecisionDomainCode)
case profileAction(code: ProfileActionDomainCode)
case unknown
}
Error definition
public class Error extends Exception{
public Domain domain;
public Enum<?> domainCode;
public HashMap<String, String> parameters;
}
Domains
public enum Domain {
CLIENT("client"),
AUTHENTICATION("authentication"),
NETWORK("network"),
OPERATION_DECISION("operationDecision"),
PROFILE_ACTION("profileAction"),
UNKNOWN("unknown");
}
Example of usage
- iOS (Swift)
- Android (Java)
Error definition
let error: Error = ....
switch error.domain {
case .client(code: let code):
handleClientError(code)
case .authentication(code: let code):
// handle
case .network(code: let code):
// handle
case .operationDecision(code: let code):
// handle
case .profileAction(code: let code):
// handle
case .unknown:
break
}
func handleClientError(_ code: Error.ClientDomainCode) {
switch code {
case .notStarted:
// call TrustFactor.getClient(....)
case .deviceNotRegistered:
// call trustfactorClient.register(...)
.
.
.
.
.
}
}
switch (error.getDomain()) {
case CLIENT:
handleClientError(error.domainCode);
break;
case AUTHENTICATION:
// Handle authentication error
break;
case NETWORK:
// Handle network error
break;
case OPERATION_DECISION:
// Handle operation decision error
break;
case PROFILE_ACTION:
// Handle profile action error
break;
case UNKNOWN:
// Handle unknown error
break;
}
void handleClientError(Enum<?> domainCode){
if(domainCode instanceof ClientDomainCode){
switch ((ClientDomainCode) domainCode) {
case NOT_STARTED -> {
// Handle NOT_STARTED
break;
}
case DEVICE_NOT_REGISTERED -> {
// Handle DEVICE_NOT_REGISTERED
break;
}
...
}
} else
// Handle error
}