Skip to content

Commit

Permalink
docs: Include error handling part of the Asgardeo Android SDK to the …
Browse files Browse the repository at this point in the history
…documentation.
  • Loading branch information
Achintha Isuru committed Aug 16, 2024
1 parent 9023aec commit 30b86cc
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ internal class FlowManagerImpl private constructor() : FlowManager {
*/
override fun manageStateOfAuthorizeFlow(responseObject: JsonNode): AuthenticationFlow {
return when (responseObject.get("flowStatus").asText()) {
/**
* TODO: Add exact error message returned from Asgardeo to the FlowManagerException
* should be embed into the exception message.
*
* https://github.com/asgardeo/mobile-ui-sdks/issues/44
*/
FlowStatus.FAIL_INCOMPLETE.flowStatus -> {
throw FlowManagerException(
FlowManagerException.AUTHENTICATION_NOT_COMPLETED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import io.asgardeo.android.core_auth_direct.models.prompt_type.PromptTypes
import kotlinx.coroutines.CompletableDeferred
import java.lang.ref.WeakReference


/**
* Implementation of [RedirectAuthenticationHandlerManager]
* This manager is responsible for handling the redirect authentication process
Expand Down
8 changes: 6 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Mobile UI SDKs for Asgardeo - Documentation Site

Documentation site for the Mobile UI SDKs for Asgardeo.
Documentation site for the Mobile UI SDKs for Asgardeo. This site was developed using [VitePress](https://vitepress.dev/) which is a Vue-powered static site generator and a spiritual successor to VuePress, built on top of Vite.

## Prerequisite

Expand Down Expand Up @@ -70,4 +70,8 @@ npm run docs:dev

```
http://localhost:5173/mobile-ui-sdks/
```
```

## Other Information

- For more information on how to improve the documentation, see the [VitePress documentation](https://vitepress.dev/).
8 changes: 6 additions & 2 deletions docs/website/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,22 @@ export default defineConfig({
{ text: "Get User Details", link: "/android/user-details.html" },
{ text: "Get Token Information", link: "/android/token-information.html" },
{ text: "Logout", link: "/android/logout.html" },
{ text: "Client Attestation", link: "/android/client-attestation.html" },
{ text: "Client Attestation", link: "/android/client-attestation.html" }
]
},
{ text: "Use Authenticators", link: "/android/use-authenticators" },
{ text: "Error Handling", link: "/android/error-handling.html" },
{ text: "Best Practices", link: "/android/best-practices.html" },
{ text: "APIs", link: "/android/apis" }
]
}
],

socialLinks: [
{ icon: "discord", link: "https://discord.com/invite/wso2" },
{ icon: "github", link: "https://github.com/asgardeo/mobile-ui-sdks" }
{ icon: "x", link: "https://x.com/asgardeo" },
{ icon: "youtube", link: "https://www.youtube.com/user/WSO2TechFlicks" },
{ icon: "github", link: "https://github.com/asgardeo/mobile-ui-sdks" },
]
}
})
33 changes: 33 additions & 0 deletions docs/website/android/best-practices.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!--
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
-->

# Best Practices when using the Asgardeo Android SDK

This guide provides best practices to follow when using the Asgardeo Android SDK. Following these best practices will help you to integrate the SDK seamlessly into your Android application.

## Use the latest version of the SDK

Always use the latest version of the Asgardeo Android SDK to ensure that you have access to the latest features and bug fixes. You can find the latest version of the SDK in the [Maven Repository](https://central.sonatype.com/artifact/io.asgardeo/asgardeo-android).

## MVVM architecture

When integrating the Asgardeo Android SDK into your application, it is recommended to use the [Model-View-ViewModel (MVVM) architecture](https://www.geeksforgeeks.org/mvvm-model-view-viewmodel-architecture-pattern-in-android/). This architecture helps to separate the UI logic from the business logic and provides a clean and maintainable codebase.

## Use `Dispatchers.IO`

Suspended functions in the SDK are designed to be optimized for `Dispatchers.IO` context.
131 changes: 131 additions & 0 deletions docs/website/android/error-handling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<!--
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
-->

# Error Handling in the Asgardeo Android SDK

Error handling is an important aspect of any application. It is crucial to handle errors gracefully and provide meaningful error messages to the user. This guide provides details on error handling in the Asgardeo Android SDK.

As mentioned in the [Start the Authentication Process](./start.md) guide, the `AuthenticationState.Error` state is triggered when an error occurs during the authentication process. This state provides an error message that can be displayed to the user.

### How to Capture Errors in the Authentication Process
The above mentioned error state is triggered when an error occurs during the authentication process, and throws a `FlowManagerException`. You can capture this error and handle it as shown below:

```kotlin
..
is AuthenticationState.Error -> {
when (it.throwable::class.java.simpleName) {
"FlowManagerException" -> {
// Handle the error
}
}
}
..
```

You can handle this error by displaying a meaningful error message to the user like a toast message or a snackbar.

```kotlin
..
is AuthenticationState.Error -> {
when (it.throwable::class.java.simpleName) {
"FlowManagerException" -> {
Toast.makeText(context, "An error occurred during the authentication process", Toast.LENGTH_SHORT).show()
}
}
}
..
```

Also if you want to show an error message in the TextFields, you have to navigate to the login component and show the error message.

> [!NOTE]
> There is an issue in the SDK that the exact error message provided from Asgardeo is not correctly passed to the `FlowManagerException`. This is a known issue that is tracked in the [GitHub repository](https://github.com/asgardeo/mobile-ui-sdks/issues/44).
### Authenticator Not Found Error

If the used authenticator is not found in current authentication step, the `AuthenticationState.Error` state is triggered with a `AuthenticatorProviderException` that contains the error message `Authenticator not found`. You can capture this error and handle it as shown below:

```kotlin
..
is AuthenticationState.Error -> {
when (it.throwable::class.java.simpleName) {
"AuthenticatorProviderException" -> {
// Handle the error
}
}
}
..
```

But the best approach is to avoid this error by checking the availability of the authenticator before using it. This can be done by dynamically checking the availability of the authenticator as shown in the [Start the Authentication Process](./start.md) guide.

### Authenticator Specific Errors

#### Redirect Based Authentication Errors

If the authenticator is a redirect-based authenticator, the `AuthenticationState.Error` state is triggered with a `RedirectAuthenticationException` that contains the error messages like `Redirect URI not found`. You can capture this error and handle it as shown below:

```kotlin
..
is AuthenticationState.Error -> {
when (it.throwable::class.java.simpleName) {
"RedirectAuthenticationException" -> {
// Handle the error
}
}
}
..
```

You can see all the error messages that can be thrown by the redirect-based authenticator in the <a href="/mobile-ui-sdks/android/api/core-auth-direct/io.asgardeo.android.core_auth_direct.models.exceptions/-redirect-authentication-exception/-companion/index.html" target="_blank">RedirectAuthenticationException</a> from here.

#### Google Authentication Error (Legacy & New)

If the authenticator is Google, the `AuthenticationState.Error` state is triggered with a `GoogleNativeAuthenticationException` that contains the error messages like `Google Web Client ID is not set`. You can capture this error and handle it as shown below:

```kotlin
..
is AuthenticationState.Error -> {
when (it.throwable::class.java.simpleName) {
"GoogleNativeAuthenticationException" -> {
// Handle the error
}
}
}
..
```

You can see all the error messages that can be thrown by the Google authenticator in the <a href="/mobile-ui-sdks/android/api/core-auth-direct/io.asgardeo.android.core_auth_direct.models.exceptions/-google-native-authentication-exception/-companion/index.html" target="_blank">GoogleNativeAuthenticationException</a> from here.

#### Passkey Authentication Error

If the authenticator is Passkey, the `AuthenticationState.Error` state is triggered with a `PasskeyAuthenticationException` that contains the error messages like `Passkey Authentication is not supported`. You can capture this error and handle it as shown below:

```kotlin
..
is AuthenticationState.Error -> {
when (it.throwable::class.java.simpleName) {
"PasskeyAuthenticationException" -> {
// Handle the error
}
}
}
..
```

You can see all the error messages that can be thrown by the Passkey authenticator in the <a href="/mobile-ui-sdks/android/api/core-auth-direct/io.asgardeo.android.core_auth_direct.models.exceptions/-passkey-authentication-exception/-companion/index.html" target="_blank">PasskeyAuthenticationException</a> from here.
10 changes: 5 additions & 5 deletions docs/website/android/start.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ After that, you can call `authenticationProvider.isLoggedInStateFlow`, this will
Then call the `authenticationProvider.initializeAuthentication` to initialize the authentication process when the state is **AuthenticationState.Initial**.

> [!IMPORTANT]
> All the suspend functions should be called inside a coroutine scope. Suspended functions in the SDK are designed to be optimezed for `Dispatchers.IO` context.
> All the suspend functions should be called inside a coroutine scope. Suspended functions in the SDK are designed to be optimized for `Dispatchers.IO` context.
```kotlin
@Composable
Expand Down Expand Up @@ -90,21 +90,21 @@ private fun HandleAuthenticationState(authenticationStateFlow: SharedFlow<Authen
authenticationProvider.initializeAuthentication(context) // [!code highlight]
}
is AuthenticationState.Unauthenticated -> {
/**
/**
* Gets called when /authorize and /authn responds with an “INCOMPLETE” state.
* This means authentication flow is still not completed and a particular step is getting
* challenged for authentication.
*/
LoginForm(it.authenticationFlow)
}
is AuthenticationState.Error -> {
/**
* Gets called when /authorize and /authn responds with an “FAILED_INCOMPLETE” state
/**
* Gets called when /authorize and /authn responds with an “FAIL_INCOMPLETE” state
* which responds at an error of a particular authentication step
*/
}
is AuthenticationState.Authenticated -> {
/**
/**
* Gets called when /authn responds with an “SUCCESS” state. This means
* authentication flow is completed
*/
Expand Down

0 comments on commit 30b86cc

Please sign in to comment.