2

I have a question about catching user error in async and wait.

Let say I have a route that only fetches for a single user.

routes.js

routes.get('/getuserbyid/:id', (req, res) => {

    const id = req.params.id;

    accountController.getById(id)
        .then((result) => {
            res.json({
                confirmation: 'success',
                result: result
            });
        })
        .catch((error) => {
            res.json({
                confirmation: 'failure',
                error: error
            });
        });
});

I have a controller that fetches the request. accountController.js

export const getById = async (id) => {

        try {
            const user = await users.findOne({ where: {
                    id: id
                }});

            if (user === null) {
                return 'User does not exist';
            }
            return user;
        } catch (error) {
            return error;
        }
}

So no matter what happens either I get a null or a single record. It is still a success. In promises, I can reject null, so it will show up in the catch block in the route. Now that with async and await. How can I achieve the same making the null in the error block?

export const getById = (id) => {

    return new Promise((resolve, reject) => {

        users.findOne({ where: {
            id: id
        }})
            .then((result) => {

                if (result === null) {
                    reject('User does not exit');
                }
                resolve(result);
            })
            .catch((error) => {
                reject(error);
            });
    });

}
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Pak Chu
  • 253
  • 1
  • 5
  • 13
  • 3
    You would `throw null;` – Aluan Haddad Feb 08 '18 at 20:30
  • I can see a couple problems here. First, the strict test `(user === null)` is likely missing some error cases, for example `undefined`. Secondly, the try/catch is unnecessary and can go away. If `user` is anything besides a valid user object, simply throw an error right then and there. It will be handled by your `.catch()` callback above. – greim Feb 08 '18 at 20:41

3 Answers3

7

First, avoid the Promise antipattern. You have a function that returns a Promise, no need to wrap that in a new Promise().

Then your funciton could look like this:

export const getById = (id) => {
  return users.findOne({ where: { id } })
    .then((user) => {
      if (user === null)
        throw 'User does not exit';

      return user;
    });
}

and the async/await version of this is

export const getById = async (id) => {
  const user = await users.findOne({ where: { id } });

  if(user === null)
    throw 'User does not exist';

  return user;
}
Thomas
  • 11,958
  • 1
  • 14
  • 23
1

An async function always returns a Promise.

Using the throw construct within an async function rejects the Promise that is returned.

Consider

function getValue() {
  return Promise.reject(null);
}

getValue().catch(e => {
  console.log('An raised by `getValue` was caught by a using the `.catch` method');
  console.log(e);
});


(async function main() {
  try {
    await getValue();
  } catch (e) {
    console.log('An raised by `getValue` was caught by a catch block');
    console.log(e);
  }
}());

async function getValue() {
  throw null;
}

getValue().catch(e => {
  console.log('An raised by `getValue` was caught by a using the `.catch` method');
  console.log(e);
}); 

(async function main() {
  try {
    await getValue();
  } catch (e) {
    console.log('An raised by `getValue` was caught by a catch block');
    console.log(e);
  }
}());

A try block within an async method handles both synchronous and asynchronous errors.

Consider

function throws() {
  throw Error('synchronous failure');
}


async function rejects() {
  throw Error('asynchronous failure');
}

(async function main() {
  try {
    throws();
  } catch (e) {
    console.log('asynchronously handled', e.message);

  }

  try {
    await rejects();
  } catch (e) {
    console.log('asynchronously handled', e.message);
  }
}());

A critical point to keep in mind is that asynchronous errors will not be caught if you forget to await the promise that rejects with them. This is the analog of forgetting to return inside of .then or a .catch callback

Aluan Haddad
  • 29,886
  • 8
  • 72
  • 84
0

You could just throw and exception if your value turns up empty.

export const getById = async (id) => {
        const user = await users.findOne({ where: {
                id: id
            }});

        if (!user) {
            throw Error('User does not exist..');
        }
        return user;        
}
Espen
  • 2,456
  • 1
  • 16
  • 25
  • This won't work, since you're in the middle of a try/catch and the thrown error will be caught and then returned from the async function, which isn't how promises are supposed to work. Just remove the try/catch wrapper completely and it would be much better. – greim Feb 08 '18 at 20:45