1

I'm trying to manage errors with a try catch in my application. For example in this case if there's no connection , to catch the error with my application going without codepush to interfere . Simply stop codepush if there are errors and go on with the application's functionality.

here is my code:

import React, {useEffect, useState} from 'react';
import {NavigationContainer} from '@react-navigation/native';
import codePush from 'react-native-code-push';
import {LoaderBox, ProgressBox} from '@components';
import {Navigator} from '@navigator';

export default function App() {
  const [syncMessage, setSyncMessage] = useState(true);
  const [syncProgress, setSyncProgress] = useState(false);
  const [progress, setProgress] = useState(0);

  const codePushStatusDidChange = (status: any) => {
    try {
      switch (status) {
        case codePush.SyncStatus.CHECKING_FOR_UPDATE:
          setSyncMessage(true);
          break;

        case codePush.SyncStatus.DOWNLOADING_PACKAGE:
          console.log('Downloading package.');
          setSyncMessage(false);
          setSyncProgress(true);
          break;
        case codePush.SyncStatus.INSTALLING_UPDATE:
          console.log('Installing update.');
          setSyncProgress(true);
          setSyncMessage(false);
          break;
        case codePush.SyncStatus.UP_TO_DATE:
          console.log('Up-to-date.');
          setSyncMessage(false);
          break;
        case codePush.SyncStatus.UPDATE_INSTALLED:
          console.log('Update installed.');
          setSyncProgress(false);
          break;
      }
    } catch (err) {}
  };

  const codePushDownloadDidProgress = (progression: {
    receivedBytes: any;
    totalBytes: any;
  }) => {
    let progressReceived = Math.round(
      (progression.receivedBytes / progression.totalBytes) * 100,
    );
    setProgress(progressReceived);
  };

  useEffect(() => {
    codePush.sync(
      {installMode: codePush.InstallMode.IMMEDIATE},
      codePushStatusDidChange,
      codePushDownloadDidProgress,
    );
  }, []);

  return (
    <NavigationContainer>
      {syncMessage && <LoaderBox />}
      {!syncMessage && !syncProgress && <Navigator />}
      {syncProgress && <ProgressBox prog={progress} />}
    </NavigationContainer>
  );
}

thank u in advance for the help

1 Answers1

0

wrap the function like this:

const codePushStatusDidChange = (status: any) => {
        try {
          switch (status) {
            case codePush.SyncStatus.CHECKING_FOR_UPDATE: {
              setSyncMessage(true);
              break;
            }
            case codePush.SyncStatus.DOWNLOADING_PACKAGE:
              console.log('Downloading package.');
              setSyncMessage(false);
              setSyncProgress(true);
              break;
            case codePush.SyncStatus.INSTALLING_UPDATE:
              console.log('Installing update.');
              setSyncProgress(true);
              setSyncMessage(false);
              break;
            case codePush.SyncStatus.UP_TO_DATE:
              console.log('Up-to-date.');
              setSyncMessage(false);
              break;
            case codePush.SyncStatus.UPDATE_INSTALLED:
              console.log('Update installed.');
              setSyncProgress(false);
              break;
          }
        } catch (err) {}
        setSyncMessage(false);
      };