Skip to content

Commit

Permalink
Merge pull request #62 from auth0/chore-add-error-code-getter
Browse files Browse the repository at this point in the history
Add method to get errorCode and isLoginTransactionNotFound()
  • Loading branch information
hzalaz authored Dec 7, 2016
2 parents e345535 + 8ee88a0 commit 6a808c3
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

package com.auth0.android.guardian.sdk;

import android.support.annotation.Nullable;

import java.util.Map;

public class GuardianException extends RuntimeException {
Expand All @@ -31,6 +33,7 @@ public class GuardianException extends RuntimeException {
private static final String ERROR_DEVICE_ACCOUNT_NOT_FOUND = "device_account_not_found";
private static final String ERROR_ENROLLMENT_NOT_FOUND = "enrollment_not_found";
private static final String ERROR_ENROLLMENT_TRANSACTION_NOT_FOUND = "enrollment_transaction_not_found";
private static final String ERROR_LOGIN_TRANSACTION_NOT_FOUND = "login_transaction_not_found";

private final Map<String, Object> errorResponse;
private final String errorCode;
Expand All @@ -53,6 +56,16 @@ public GuardianException(Map<String, Object> errorResponse) {
this.errorResponse = errorResponse;
}

/**
* Returns the `errorCode` value, if available.
*
* @return the error code, or null
*/
@Nullable
public String getErrorCode() {
return errorCode;
}

/**
* Whether the error is caused by the use of an invalid OTP code
*
Expand Down Expand Up @@ -90,6 +103,15 @@ public boolean isEnrollmentTransactionNotFound() {
return ERROR_ENROLLMENT_TRANSACTION_NOT_FOUND.equals(errorCode);
}

/**
* Whether the error is caused by the login transaction being invalid, expired or not found
*
* @return true if error is caused by the login transaction being invalid, expired or not found
*/
public boolean isLoginTransactionNotFound() {
return ERROR_LOGIN_TRANSACTION_NOT_FOUND.equals(errorCode);
}

@Override
public String toString() {
if (errorResponse != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,58 +29,134 @@

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertThat;

public class GuardianExceptionTest {

@Test
public void shouldBeUnknownError() throws Exception {
GuardianException exception = new GuardianException("Some error message");

assertThat(exception.getMessage(), is(equalTo("Some error message")));

assertThat(exception.getErrorCode(), is(nullValue()));

assertThat(exception.isInvalidOTP(), is(equalTo(false)));
assertThat(exception.isInvalidToken(), is(equalTo(false)));
assertThat(exception.isEnrollmentNotFound(), is(equalTo(false)));
assertThat(exception.isEnrollmentTransactionNotFound(), is(equalTo(false)));
assertThat(exception.isLoginTransactionNotFound(), is(equalTo(false)));
}

@Test
public void shouldBeUnknownErrorWithCause() throws Exception {
Throwable cause = new RuntimeException("The exception cause");
GuardianException exception = new GuardianException("Some error message", cause);

assertThat(exception.getMessage(), is(equalTo("Some error message")));

assertThat(exception.getCause(), is(sameInstance(cause)));

assertThat(exception.getErrorCode(), is(nullValue()));

assertThat(exception.isInvalidOTP(), is(equalTo(false)));
assertThat(exception.isInvalidToken(), is(equalTo(false)));
assertThat(exception.isEnrollmentNotFound(), is(equalTo(false)));
assertThat(exception.isEnrollmentTransactionNotFound(), is(equalTo(false)));
assertThat(exception.isLoginTransactionNotFound(), is(equalTo(false)));
}

@Test
public void shouldHaveCustomErrorCode() throws Exception {
GuardianException exception = new GuardianException(createErrorMap("some_unknown_error_code"));

assertThat(exception.getErrorCode(), is(equalTo("some_unknown_error_code")));

assertThat(exception.isInvalidOTP(), is(equalTo(false)));
assertThat(exception.isInvalidToken(), is(equalTo(false)));
assertThat(exception.isEnrollmentNotFound(), is(equalTo(false)));
assertThat(exception.isEnrollmentTransactionNotFound(), is(equalTo(false)));
assertThat(exception.isLoginTransactionNotFound(), is(equalTo(false)));
}

@Test
public void testIsInvalidOTP() throws Exception {
GuardianException exception = new GuardianException(createErrorMap("invalid_otp"));

assertThat(exception.getErrorCode(), is(equalTo("invalid_otp")));

assertThat(exception.isInvalidOTP(), is(equalTo(true)));
assertThat(exception.isInvalidToken(), is(equalTo(false)));
assertThat(exception.isEnrollmentNotFound(), is(equalTo(false)));
assertThat(exception.isEnrollmentTransactionNotFound(), is(equalTo(false)));
assertThat(exception.isLoginTransactionNotFound(), is(equalTo(false)));
}

@Test
public void testIsInvalidToken() throws Exception {
GuardianException exception = new GuardianException(createErrorMap("invalid_token"));

assertThat(exception.getErrorCode(), is(equalTo("invalid_token")));

assertThat(exception.isInvalidOTP(), is(equalTo(false)));
assertThat(exception.isInvalidToken(), is(equalTo(true)));
assertThat(exception.isEnrollmentNotFound(), is(equalTo(false)));
assertThat(exception.isEnrollmentTransactionNotFound(), is(equalTo(false)));
assertThat(exception.isLoginTransactionNotFound(), is(equalTo(false)));
}

@Test
public void testIsEnrollmentNotFound() throws Exception {
public void testIsEnrollmentNotFoundDeprecatedCode() throws Exception {
GuardianException exception = new GuardianException(createErrorMap("device_account_not_found"));

assertThat(exception.getErrorCode(), is(equalTo("device_account_not_found")));

assertThat(exception.isInvalidOTP(), is(equalTo(false)));
assertThat(exception.isInvalidToken(), is(equalTo(false)));
assertThat(exception.isEnrollmentNotFound(), is(equalTo(true)));
assertThat(exception.isEnrollmentTransactionNotFound(), is(equalTo(false)));
assertThat(exception.isLoginTransactionNotFound(), is(equalTo(false)));
}

@Test
public void testIsEnrollmentNotFound() throws Exception {
GuardianException exception = new GuardianException(createErrorMap("enrollment_not_found"));

assertThat(exception.getErrorCode(), is(equalTo("enrollment_not_found")));

assertThat(exception.isInvalidOTP(), is(equalTo(false)));
assertThat(exception.isInvalidToken(), is(equalTo(false)));
assertThat(exception.isEnrollmentNotFound(), is(equalTo(true)));
assertThat(exception.isEnrollmentTransactionNotFound(), is(equalTo(false)));
assertThat(exception.isLoginTransactionNotFound(), is(equalTo(false)));
}

@Test
public void testIsEnrollmentTransactionNotFound() throws Exception {
GuardianException exception = new GuardianException(createErrorMap("enrollment_transaction_not_found"));

assertThat(exception.getErrorCode(), is(equalTo("enrollment_transaction_not_found")));

assertThat(exception.isInvalidOTP(), is(equalTo(false)));
assertThat(exception.isInvalidToken(), is(equalTo(false)));
assertThat(exception.isEnrollmentNotFound(), is(equalTo(false)));
assertThat(exception.isEnrollmentTransactionNotFound(), is(equalTo(true)));
assertThat(exception.isLoginTransactionNotFound(), is(equalTo(false)));
}

@Test
public void testIsLoginTransactionNotFound() throws Exception {
GuardianException exception = new GuardianException(createErrorMap("login_transaction_not_found"));

assertThat(exception.getErrorCode(), is(equalTo("login_transaction_not_found")));

assertThat(exception.isInvalidOTP(), is(equalTo(false)));
assertThat(exception.isInvalidToken(), is(equalTo(false)));
assertThat(exception.isEnrollmentNotFound(), is(equalTo(false)));
assertThat(exception.isEnrollmentTransactionNotFound(), is(equalTo(false)));
assertThat(exception.isLoginTransactionNotFound(), is(equalTo(true)));
}

private Map<String, Object> createErrorMap(String errorCode) {
Expand Down

0 comments on commit 6a808c3

Please sign in to comment.