a-marenkov / gsheets

A Dart library for working with Google Sheets API.
Other
79 stars 31 forks source link

Question on error handling or caching responses for data validation #21

Closed useit4me closed 4 years ago

useit4me commented 4 years ago

Im writing a log to my sheet, a set of users task related data, here is my function,

static logAllTaskData(TaskLogData taskLogData) async { final taskLogToRow = [ userName, _taskName, _taskDetail, _taskStart, _taskStop, _taskDuration ]; var sheet = ss.worksheetByTitle("Logs"); sheet ??= await ss.addWorksheet("Logs"); var row = sheet.insertRow(2); await sheet.values.insertRow(2, taskLogToRow); } it works fine but once in a while i'm getting blanks and data is not inserted but the row is created, nothing is printed in the debug log it doesn't show any error. i'm wondering how to catch that so that we can handle it and make sure data is not lost is strictly written to sheet.

also how to do proper error handling and how to catch the responses while using the gsheets plugin when i do try catch nothing came up. Please help!

a-marenkov commented 4 years ago

Hi @useit4me

Sorry, for the delay with reply.

It looks like you've forgotten to add await before sheet.insertRow.

final row = await sheet.insertRow(2);
await sheet.values.insertRow(2, taskLogToRow);

Speaking of handling errors - just wrap in try - catch. If operation fails, the exception will be thrown.

To ensure that operation is succeded check out return type of the methods (they either return data or bool).

Hope it helps. Please let me know if you still have the issue.

Thanks!

a-marenkov commented 4 years ago

Hi @useit4me! Did it solve the issue?

useit4me commented 4 years ago

Apologies for the delay in reply. It did actually! what I did additionally, was to add a random key to check if its there.

var getTaskLogRow =  await sheet.values.rowByKey(_uniqID);
    if (getTaskLogRow == null) {
      print("Failed first...retrying...");
    await sheet.insertRow(2);
    await sheet.values.insertRow(2, taskLogToRow);
    } 
    print("Reading data from sheets to see if correctly is written");
    print(await sheet.values.rowByKey(_uniqID));
    } catch (Exception) {
      print("Exception occurred!!");
      print(Exception);
    } 

It is working fine now, I will continue to test and see if anything comes up. Thanks for the help and concern, really appreciate this, let me know in any way I can repay you!