Home > OS >  Return a 500 error from doPost in Google App Script
Return a 500 error from doPost in Google App Script

Time:11-16

I have doPost(e) function in an App Script that is called by Zapier. If an exception occurs in my code, a 200 response is sent back to Zapier, but I'd like it to return a 500 error, so that Zapier knows it has failed.

function doPost(e) {
  if (e) {
    try {
      switch (e.parameter.action) {
        case 'newOrder':
          processNewOrder(e.parameter.orderId, e.parameter.orderType);
          break;
        case 'cancelledOrder':
          processCancelledOrder(e.parameter.orderId, e.parameter.orderType);
          break;
      }

      return ContentService.createTextOutput('Done');
    }
    catch (err) {
      throw err;
    }
  }
}

How can I return an exception to Zapier in that catch?

CodePudding user response:

Replace:

    }
    catch (err) {
      throw err;
    }
  }

With:

    }
    catch (err) {
      return ContentService.createTextOutput(err.message);
    }
  }

CodePudding user response:

Answer:

Currently, you cannot force the script to return response codes other than 200.

Issue Tracker:

This has been reported previously in Issue Tracker:

I'd suggest you to star the referenced issue, in order to keep track of it and to help prioritizing it.

Workarounds:

Therefore, you'll have to come up with other ways to return information regarding the outcome of the response, like the one suggested by Amit Agarwal. But the response code will always be 200.

Related question:

  • Related