How to handle exceptions in a freshdesk serverless app

Hi everyone, I’m new to freshdesk development and I’m having trouble in how to handle exceptions in a serverless app.

I’ve done the following:

$"https://myExternalAPI",  options)
        .then(function (data) {
           // some logic
          function (error) {
            console.error(error.message + error.stack);

But when I go to the serverless logs, the following error appears:

Basically it says that $…).then(…).catch is not a function. So, I want to know if .catch works or not in a serverless app or I need to do something else.


1 Like

Hi @Matheus_Souza_Silva

When you call $request, on the .then(), you need to pass callback functions:

    var options = {...}
    $"url", options).then(
      function (data) {
      function (error) {

Thanks, I thought using .catch was supported.


@Matheus_Souza_Silva Even I wonder why .catch is not supported when the $request or $db methods always return a promise.

1 Like

It is because according to $request, $db a successful response from the external server($request) and Db service respectively is still a success. Only if the services are down, the catch is reached. That’s the reason both response and error object are resolved


Adding on to @shravan.balasubraman, the confusion on the catch block might also arise due to the promise polyfills used in the APIs.
For example, if we observe the Request API behavior,
(Server-Side) It would result in an error if an ES6 promise-styled catch block is used.

However, a fail block would work very similarly to a catch block due to the polyfill.

(Frontend) If we observe its behavior in the frontend, it would be very much in line with ES6 promises.

I realize that this might’ve resulted in a mismatch and have noted it as a feedback :slight_smile: ! A good thing is that there is a common pattern that works across all regardless of the polyfills. This is what @samuelpares has recommended in his solution too.
ES6 Promise behavior (without an explicit catch block. error handled in the second fn. arg)

Although this is a nuance in the way promises operate in the context of the developer platform, it could definitely remove a lot of confusion down the lane.