Prevent “Unhandled promise rejection” error

后端 未结 3 737
不知归路
不知归路 2020-12-11 23:00

In my server app I want to return a \"forbidden\" value when the user has no permissions for the endpoint.

To this end I create a rejected promise for reuse:

3条回答
  •  无人及你
    2020-12-11 23:26

    I would give advice which deviates from the accepted answer. I wouldn't recommend using the return statement to provide an Error - this is ignoring the exact intention of throw!

    I offer that your intuition and the accepted answer are over-engineered. Simply do this instead:

    if (!isCollab) throw new Error('FORBIDDEN');
    

    If you don't want a stack trace forget about all the overhead and simply do:

    if (!isCollab) throw 'FORBIDDEN';
    

提交回复
热议问题