Custom Failure Handler for withFailureHandler - Go

2019-06-09 13:52发布

问题:

I'm trying to get more information for when google.script.run fails. I know that I can get a basic error handling callback function to give me the error message with something like this:

google.script.run.withFailureHandler(handleError).getMe();

Where handleError gets an argument passed in for the error message.

function handleError(error) {
  console.log(error);
}

However, if I wanted to created a custom error handler that provided the location of where the exception was being thrown, I could use a custom function on withFailureHandler, like this:

google.script.run.withFailureHandler(function () {
  showError(error, 'getMe'); 
}).getMe();

With this method I'm stumped with one issue. How do I capture the error message to pass to my showError() error handler?

回答1:

Simply add the error parameter to your anonymous function.

google.script.run.withFailureHandler(function (error) {
  showError(error, 'getMe'); 
}).getMe();


回答2:

An errorHandler receives the error event from an exception that's thrown on the server. To pass a custom message, have your server side code do something like this:

...
if (errDetected) {
  throw new error("Custom message")
}
...