How Do I Authorize An App Script In A Web Interface?
Solution 1:
You will receive a CORS error if any function in your call throws an exception, since you have no try...catch
handler. When a deployed webapp fails to return a valid response (i.e. either an HtmlOutput
or TextOutput
), Google's error page does not include any CORS headers.
To prevent these "masking" CORS errors, wrap your web app entry points with try...catch
so that the actual error can be reported.
Even without wrapping your webapp's entry points with try...catch
, it is very likely that you are able to access the underlying error by viewing your project's Stackdriver logs / Stackdriver Error Reporting.
This is a simple example, demonstrating some event object validation to ensure that the request is from somewhere you care about & is well-formed, and then calls a function that may throw an exception for invalid / unauthorized operations. In either case, it returns an opaque user-facing response ("Sorry!") and for errors will generate a Stackdriver log. It cannot catch the exception that occurs for an execution time quota breach, since your script is simply killed.
functiondoGet(e) {
if (!e || !validate_(e)) {
returnfail_();
}
try {
returnwalkDriveContent(...);
}
catch (err) {
console.error({message: "Drive walk failed: " + err.message, stack: err.stack, error: err});
returnfail_();
}
}
functionfail_() {
returnContentService.createTextOutput("Sorry!");
}
functionvalidate_(eventObj) {
/** your code here that ensures this is a valid event object you can work with */// return true;// or// return false;
}
functionwalkDriveContent(folderName) {
/**
* Your code that may throw exceptions here, but otherwise
* returns TextOutput or HtmlOutput
*/
}
Post a Comment for "How Do I Authorize An App Script In A Web Interface?"