openmobilehub / react-native-omh-auth

https://openmobilehub.github.io/react-native-omh-auth/
Apache License 2.0
0 stars 0 forks source link

Enhance Error Handling with Alerts, Global Exception Handling, and Console Logging #58

Closed dzuluaga closed 3 weeks ago

dzuluaga commented 1 month ago

Andrei, I appreciate your work on error handling for the HomeScreen. Here are a few suggestions to enhance it:

  1. Present Alerts for User Feedback:
import { Alert } from 'react-native';

async function someFunction() {
  try {
    // Some code that may throw an error
  } catch (error) {
    Alert.alert('Error', 'An unexpected error occurred. Please try again.');
    throw error; // For global handling
  }
}
  1. Throw Exceptions for Global Handling:
import { ErrorUtils, Alert } from 'react-native';

function handleErrorGlobally(error, isFatal) {
  console.error('Global error handler:', error);

  if (isFatal) {
    Alert.alert('Unexpected error', 'An unexpected error occurred. Please restart the app.');
  } else {
    Alert.alert('Error', error.message);
  }
}

ErrorUtils.setGlobalHandler(handleErrorGlobally);
  1. Log Errors Globally:
import { ErrorUtils } from 'react-native';

function handleErrorGlobally(error, isFatal) {
  console.error('Global error handler:', error);

  if (isFatal) {
    Alert.alert('Unexpected error', 'An unexpected error occurred. Please restart the app.');
  } else {
    Alert.alert('Error', error.message);
  }
}

ErrorUtils.setGlobalHandler(handleErrorGlobally);

Implementing these will improve our error handling. Thanks for considering these changes!

andrei-zgirvaci commented 1 month ago

Hey @dzuluaga, thanks for bringing this to our attention! However, at the moment we don't really see a point in implementing a global error handler for the sample app.

We are catching most of the errors that we expect to be thrown from the OMH library. And in the rare instances where we don't, React Native Logger does a much better job at logging un-catched exceptions and showing them to the developer in a properly formatted way.

dzuluaga commented 3 weeks ago

Sounds good. Let's keep it monitored.