How can I make my async function fail gracefully with Express?

Here’s my project link: Glitch :・゚✧

In my project, I’m using a bunch of asynchronous SQL calls, which pass information to the client. Now, when something not found in the SQLite database is found, my db.all functions don’t return errors, they just return undefined results.

So, in my first (promisified) database call (in get.js), the error will come out of here:

const getenID = name => {
  return new Promise((res, rej) => {
    en_db.all(
      "SELECT ID FROM Descriptions WHERE Content=?;",
      [name],
      (err, data) => {
        if (err) rej(err);
        if (!data[0]) {
          return "error"; //error is derived from here
        } else {return res(data[0].ID);}
        
      }
    );
  });
};

Anyway, this is fine. Now if I call, say, echoesapi.glitch.me/api/asdfasdfasdf then the api doesn’t crash and sit around for 30 seconds trying to get back up. However, I want my API to send an error message - right now it’ll load for oblivion.

So in my exported async function, I have this:

  let enID = await getenID(name);
  if (enID == "error") {
    return "error";
  }

However, if I paste echoesapi.glitch.me/api/asdfasdfasdf into my browser, I don’t see “error” I see nothing. If I try fetching it:

Sad - it should send error.

Any help would be really appreciated!!

2 Likes

I am quite certain it is due to your returning a bare string in the failure case. It isn’t a “response” like it is in the successful case.

2 Likes

Thank you! I was forgetting to provide a resolve case. You were right, it was because I was just returning - I needed to resolve.