Skip to content

[React Native] MSAL throws JsonSyntaxException: Expected BEGIN_OBJECT but was STRING from Android layer #2394

@phamngocthachlt6c

Description

@phamngocthachlt6c

Description

We encountered an exception coming from MSAL Android:
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

This seems to happen when MSAL tries to read token cache from SharedPreferences.

Environment

  • MSAL Android version: 2.2.+
  • MSAL React Native: 4.0.4
  • Platform: React Native (bridge to MSAL Android)
  • Android version: multiple
  • Device: multiple
  • Network condition: may happen when device loses connection or the app is killed

Steps to Reproduce

(We cannot consistently reproduce, but possible scenarios:)

  1. App killed while MSAL is saving tokens
  2. Internet disconnected during token refresh
  3. Concurrent access to MSAL cache

Stack Trace

Error: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
at promiseMethodWrapper (address at index.android.bundle:1:195468)
at ?anon_0_ (address at index.android.bundle:1:2661568)
at next (native)
at asyncGeneratorStep (address at index.android.bundle:1:614795)
at next (address at index.android.bundle:1:615053)
at anonymous (address at index.android.bundle:1:615005)
at tryCallTwo (address at InternalBytecode.js:1:1222)
at doResolve (address at InternalBytecode.js:1:2541)
at Promise (address at InternalBytecode.js:1:1318)
at anonymous (address at index.android.bundle:1:614926)
at apply (native)
at acquireTokenSilent (address at index.android.bundle:1:2661511)
at ?anon_0
(address at index.android.bundle:1:2658787)
at next (native)
at asyncGeneratorStep (address at index.android.bundle:1:614795)
at _next (address at index.android.bundle:1:615053)
at tryCallOne (address at InternalBytecode.js:1:1180)
at anonymous (address at InternalBytecode.js:1:1874)
at apply (native)
at anonymous (address at index.android.bundle:1:319608)
at _callTimer (address at index.android.bundle:1:318556)
at _callReactNativeMicrotasksPass (address at index.android.bundle:1:318701)
at callReactNativeMicrotasks (address at index.android.bundle:1:320701)
at __callReactNativeMicrotasks (address at index.android.bundle:1:181351)
at anonymous (address at index.android.bundle:1:180433)
at __guard (address at index.android.bundle:1:181189)
at flushedQueue (address at index.android.bundle:1:180344)
at invokeCallbackAndReturnFlushedQueue (address at index.android.bundle:1:180287)

Expected Behavior

MSAL should handle corrupted or empty cache gracefully and return a recoverable error instead of crashing.

Additional context

We are logging this through Application Insights from the React Native layer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    User-ErrorIT - Github Issue where user is incorrectly using MSAL, leading to unexpected behaviorneeds-more-info

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions