Skip to content

Commit

Permalink
Use createModalNotReadyError() when modal is not ready on react native
Browse files Browse the repository at this point in the history
  • Loading branch information
romin-halltari committed Nov 20, 2023
1 parent 09ec393 commit 343e59c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 23 deletions.
8 changes: 2 additions & 6 deletions packages/@magic-sdk/provider/src/core/view-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import {
JsonRpcRequestPayload,
MagicMessageEvent,
MagicMessageRequest,
RPCErrorCode,
JsonRpcError,
} from '@magic-sdk/types';
import { JsonRpcResponse } from './json-rpc';
import { createPromise } from '../util/promise-tools';
import { getItem, setItem } from '../util/storage';
import { createJwt } from '../util/web-crypto';
import { SDKEnvironment } from './sdk-environment';
import { createModalNotReadyError } from './sdk-exceptions';

interface RemoveEventListenerFunction {
(): void;
Expand Down Expand Up @@ -140,10 +139,7 @@ export abstract class ViewController {
// if the app was initially opened without internet connection. That is
// why we reject the promise without waiting and just let them call it
// again when internet connection is re-established.
const error: JsonRpcError = {
code: RPCErrorCode.InternalError,
message: 'Connection to Magic SDK not ready. Please check your internet connection.',
};
const error = createModalNotReadyError();
reject(error);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@
/* eslint-disable prefer-spread */

import browserEnv from '@ikscodes/browser-env';
import {
MagicIncomingWindowMessage,
MagicOutgoingWindowMessage,
JsonRpcRequestPayload,
RPCErrorCode,
} from '@magic-sdk/types';
import _ from 'lodash';
import { MagicIncomingWindowMessage, MagicOutgoingWindowMessage, JsonRpcRequestPayload } from '@magic-sdk/types';
import { createViewController } from '../../../factories';
import { JsonRpcResponse } from '../../../../src/core/json-rpc';
import * as storage from '../../../../src/util/storage';
import * as webCryptoUtils from '../../../../src/util/web-crypto';
import { SDKEnvironment } from '../../../../src/core/sdk-environment';
import { createModalNotReadyError } from '../../../../src/core/sdk-exceptions';

/**
* Create a dummy request payload.
Expand Down Expand Up @@ -220,10 +215,7 @@ test('does not wait for ready and throws error when platform is react-native', a
try {
await viewController.post(MagicOutgoingWindowMessage.MAGIC_HANDLE_REQUEST, payload);
} catch (e) {
expect(e).toEqual({
code: RPCErrorCode.InternalError,
message: 'Connection to Magic SDK not ready. Please check your internet connection.',
});
expect(e).toEqual(createModalNotReadyError);
}
expect(createJwtStub).not.toHaveBeenCalledWith();
expect(onSpy.mock.calls[0][0]).toEqual(MagicIncomingWindowMessage.MAGIC_HANDLE_RESPONSE);
Expand Down
7 changes: 4 additions & 3 deletions packages/@magic-sdk/react-native-bare/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,16 @@ When attempting to import `Magic`, take note that the React Native metro bundler
For this issue consider using Microsoft's [rnx-kit](https://microsoft.github.io/rnx-kit/docs/guides/bundling) suite of tools that include a plugin for metro that fixes this symlink related error.

### Handling internet connection problems
When an app is opened without internet connection, any request to the Magic SDK will result in a rejection with the following error:
When an app is opened without internet connection, any request to the Magic SDK will result in a rejection with a `MagicSDKError`:

```json
{
"code": -32603,
"message": "Connection to Magic SDK not ready. Please check your internet connection."
"code": "MODAL_NOT_READY",
"rawMessage": "Modal is not ready."
}
```


It is good practice to use [@react-native-community/netinfo](https://www.npmjs.com/package/@react-native-community/netinfo) to track the internet connection state of the device. For your convenience, we've also added a hook that uses this library behind the scenes:


Expand Down
6 changes: 3 additions & 3 deletions packages/@magic-sdk/react-native-expo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ When attempting to import `Magic`, take note that the React Native metro bundler
For this issue consider using Microsoft's [rnx-kit](https://microsoft.github.io/rnx-kit/docs/guides/bundling) suite of tools that include a plugin for metro that fixes this symlink related error.

### Handling internet connection problems
When an app is opened without internet connection, any request to the Magic SDK will result in a rejection with the following error:
When an app is opened without internet connection, any request to the Magic SDK will result in a rejection with a `MagicSDKError`:

```json
{
"code": -32603,
"message": "Connection to Magic SDK not ready. Please check your internet connection."
"code": "MODAL_NOT_READY",
"rawMessage": "Modal is not ready."
}
```

Expand Down

0 comments on commit 343e59c

Please sign in to comment.