Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose generic error codes for CredentialsManagerError #1076

Merged
merged 4 commits into from
Feb 18, 2025

Conversation

kailash-b
Copy link
Contributor

@kailash-b kailash-b commented Feb 14, 2025

Changes

Added

We now expose platform agnostic generic error codes that represent different exception scenarios common in both android and iOS.
We have considered only the CredentialsManagerError for now.
Keeping the backward compatibility in mind, a new field with the generic error code has been exposed via the BaseError class.

The users can leverage the field as below :

try {
  const credentials = await auth0.credentialsManager.getCredentials();
} catch (error) {
  if(error.generic_error_code == "NO_CREDENTIALS"){
    // handle scenario
  }
}

Note : The old behaviour still remains untouched.

References

Testing

  • This change adds unit test coverage
  • This change has been tested on the latest version of the platform/language or why not

Checklist

@kailash-b kailash-b requested a review from a team as a code owner February 14, 2025 04:19
@@ -1,12 +1,14 @@
class BaseError extends Error {
constructor(name: string, message: string) {
generic_error_code: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use field name like

  • identifier
  • reason
  • type

Or something that will be generic and can fit in a single word.

The purpose of the field will be extensively documented so it would be a one time learning that it is a platform agnostic error code.

import { User } from '../types';
import { deepEqual } from '../utils/deepEqual';
import { AuthState } from './auth0-context';

type Action =
| { type: 'LOGIN_COMPLETE'; user: User }
| { type: 'LOGOUT_COMPLETE' }
| { type: 'ERROR'; error: Error }
| { type: 'ERROR'; error: BaseError }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What value do we provide to generic_error_code if for authentication errors.

Copy link
Contributor Author

@kailash-b kailash-b Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is left empty.
The field is currently populated only in case of a CredentialManagerException.
We can re-use this field when we collate the Authentication Errors as well (tracked as part of SDK-5700).

@@ -30,6 +30,43 @@

public class A0Auth0Module extends ReactContextBaseJavaModule implements ActivityEventListener {

private final Map<CredentialsManagerException, String> ERROR_CODE_MAP = new HashMap<>() {{
put(CredentialsManagerException.Companion.getINVALID_CREDENTIALS(), "INVALID_CREDENTIALS");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is Enum.name field provided by Enums in Kotlin, can we use that to simplify the code? I haven't fired up my IDE to test this but looks like there is an opportunity to clean this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Enums are internal in the underlying android SDK right now and we cannot access them.
We are leveraging the Public fields exposed via the Companion class.
However, we have a plan to migrate this .java code to .kt. Once that is done, we can leverage kotlin semantics and clean this snippet (tracked as part of SDK-5699)

@kailash-b kailash-b merged commit 42fef5e into master Feb 18, 2025
4 checks passed
@kailash-b kailash-b deleted the feature/SDK-5643 branch February 18, 2025 06:46
@kailash-b kailash-b mentioned this pull request Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants