Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added error handling in Spreadsheet component #3324

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Tonyhrule
Copy link

Spreadsheet Component Crash on Google Sheets

General items:

If your code changes how something works on the device (i.e., it affects the companion):

  • I branched from ucr
  • My pull request has ucr as the base

Further, if you've changed the blocks language or another user-facing designer/blocks API (added a SimpleProperty, etc.):

  • I have updated the corresponding version number in appinventor/components/src/.../common/YaVersion.java
  • I have updated the corresponding upgrader in appinventor/appengine/src/.../client/youngandroid/YoungAndroidFormUpgrader.java (components only)
  • I have updated the corresponding entries in appinventor/blocklyeditor/src/versioning.js

For all other changes:

  • I branched from master
  • My pull request has master as the base

What does this PR accomplish?

This PR attempts to fix a bug in the App Inventor Spreadsheets component where pushing data to Google Sheets would crash the entire app. The PR adds error handling in all write operations.

Changes Made

  1. Added exception handling for:

    • GoogleJsonResponseException (API errors)
    • IOException (network issues)
    • GeneralSecurityException (authentication problems)
    • General exceptions (unexpected errors)
  2. Error messages with categorization:

    • "API Error: ..." for Google Sheets API issues
    • "IO Error: ..." for network-related problems
    • "Security Error: ..." for authentication issues
    • "Unknown Error: ..." for unexpected exceptions
  3. ErrorOccurred handler:

    • Added try-catch protection in the error handler itself
    • Added error logging for handler failures
    • Prevents cascading crashes during error reporting

@AppInventorWorkerBee
Copy link
Collaborator

Can one of the admins verify this patch?

Comment on lines 354 to 372
try {
Log.d(LOG_TAG, errorMessage);
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
if (!EventDispatcher.dispatchEvent(thisInstance, "ErrorOccurred", errorMessage)) {
// Dispatch to screen if the event handler does not exist.
form.dispatchErrorOccurredEvent(Spreadsheet.this, "ErrorOccurred",
ErrorMessages.ERROR_SPREADSHEET_ERROR, errorMessage);
}
} catch (Exception e) {
Log.e(LOG_TAG, "Error in ErrorOccurred event dispatch", e);
}
}
}
});
});
} catch (Exception e) {
Log.e(LOG_TAG, "Fatal error in ErrorOccurred handler", e);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exception(s) are you trying to protect against here? runOnUiThread doesn't document any thrown exceptions (runtime or otherwise) so there doesn't seem to be any value in the outer try-catch. Do you have a specific project file that would trigger an exception here?

@@ -2091,4 +2127,4 @@ private static Object sanitizeObject(Object o) {
return o.toString();
}
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please restore the newline at the end of the file.

@@ -1491,9 +1500,18 @@ public void run() {
}
});
}
catch (Exception e) {
catch (GoogleJsonResponseException e) {
e.printStackTrace();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As part of this change, I would recommend we move to using the standard Android Log class rather than using e.printStackTrace()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Tonyhrule This block of changed code is still using e.printStackTrace().

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, now I have replaced all instances of e.printStackTrace() with the Android Log class.

@Tonyhrule
Copy link
Author

Thank you for the comments! I have removed the unnecessary outer try-catch block around runOnUiThread, as it does not document any thrown exceptions. I added the missing newline at the end of the file and replaced all printStackTrace() calls with Android's Log.e().

@ewpatton ewpatton added this to the nb201 milestone Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants