1

In web app development I would like a consistent way to catch and report error conditions. For example, a database update routine may detect a variety of error conditions and ideally I would like the application to capture them and report gracefully. The code below din't work because retdiag is undefined when error is thrown...

function saveData(app,e) {
var db ;
var retdiag = ""; 
var lock = LockService.getPublicLock();
lock.waitLock(30000);
try {
// e.parameters has all the data fields from form
// re obtain the data to be updated
db = databaseLib.getDb();
var result = db.query({table: 'serviceUser',"idx":e.parameter.id});
if (result.getSize() !== 1) {
throw ("DB error - service user " + e.parameter.id);
}
//should be  one & only one
retdiag = 'Save Data Finished Ok'; 
}
catch (ex) {

retdiag= ex.message  // undefined!
}
finally {
lock.releaseLock();
return retdiag;
}
}

Is there a good or best practice for this is GAS?

1 Answer 1

2

To have a full error object, with message and stacktrace you have to build one, and not just throw a string. e.g. throw new Error("DB error ...");

Now, a more "consistent" way I usually implement is to wrap all my client-side calls into a function that will treat any errors for me. e.g.

function wrapper_(f,args) {
  try {
    return f.apply(this,args);
  } catch(err) {
    ;//log error here or send an email to yourself, etc
    throw err.message || err; //re-throw the message, so the client-side knows an error happend
  }
}

//real client side called functions get wrapped like this (just examples)
function fileSelected(file,type) { return wrapper_(fileSelected_,[file,type]); }
function loadSettings(id) { return wrapper_(loadSettings_,[id]); }

function fileSelected_(file,type) {
  ; //do your thing
}

function loadSettings_(id) {
  ; //just examples
  throw new Error("DB error ...");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Simply writing "err.message || err" makes the job :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.