Jack
Jack

Reputation: 10613

Trigger google.script.run.withFailureHandler from Code.gs

Calling a 'server-side' function in Google App Script can be achieved with:

google.script.run.withFailureHandler(CallbackFailure)
      .withSuccessHandler(CallbackSuccess).doSomething();

In Code.gs, how can I ensure CallbackFailure is called? I attempted:

function doSomething() {
   throw new Error( "Trigger CallbackFailure" );
}

However I receive an error because of an unhandled exception. I had hoped throwing an Error would call CallbackFailure - but it was just guess work and doesn't work.

Upvotes: 1

Views: 1075

Answers (1)

m5khan
m5khan

Reputation: 2717

I do not see any problem in your code, it should work fine.

I did in my index.html

google.script.run.withFailureHandler(function(e){console.log("failure handler",e)})
    .withSuccessHandler(function(e){console.log("success handler",e)}).doSomething();

and in my code.gs

function doSomething() {
   throw new Error( "Trigger CallbackFailure" );
}

and my browser console logged:

failure handler Error {name: "", message: "Error: Trigger CallbackFailure"}

withFailureHandler callback function is called if the server-side function throws an exception as stated in the documentation. This worked fine.

Maybe there could be some other issue in your script, you can share your container bound script if you like.

Upvotes: 1

Related Questions