3

I'm trying to make a DELETE call and I'm implementing the function below. I understand that in a promise, there needs to be a "resolve" and a "reject" state, but I'm getting an unhandled promise rejection error:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): [object Object]

I don't really like using conditional statements inside a promise because it gets messy, but what I'm trying to do here is to check if the organization is verified, and if it is, a delete operation should not occur and will reject.

function deleteOrg(id) {
    return new Promise((resolve, reject) => {
        // A helper function that returns an 'org' object
        findById(id)
        .then((orgObject) => {
            if (orgObject.verified_at !== null) {
                throw new Error(422, 'Unable to delete organization')
            }

            //Organization is not verified, so proceed to delete
            new Organization().where({'id': id}).destroy()
            .then(() => {
                return resolve() //return 200 upon deletion
            })
            .catch((err) => {
                return reject(new Error(500, 'Unable to delete organization'))
            })
        })
        .catch((err) => {
            const m = `Unable to delete organization: ${err.message}`
            return reject(new Error(500, m))
        })
    })
}

I'm pretty sure I'm handling the rejection inside the if wrong.

nem035
  • 34,790
  • 6
  • 87
  • 99
patrickhuang94
  • 2,085
  • 5
  • 36
  • 58

3 Answers3

0

Creating promises inside promise constructors is a known anti-pattern. Try modularizing your promises into separate functions instead:

function deleteOrg(id) {

  const verifyOrg = (orgObject) => {
    if (orgObject.verified_at !== null) {
      throw new Error(422, 'Unable to delete organization')
    }
  };

  const destroyOrg = () => new Organization().where({
    'id': id
  }).destroy();

  return findById(id)
    .then(verifyOrg)
    .then(destroyOrg);
}

Where you can let the errors propagate through the promise chain and handle them outside:

deleteOrg(id)
  .catch((err) => {
    const m = `Unable to delete organization: ${err.message}`;
    // ...
  });
Community
  • 1
  • 1
nem035
  • 34,790
  • 6
  • 87
  • 99
  • 1
    This looks like a much cleaner way to go about promises. However, I'm still getting the same unhandled promise rejection error. – patrickhuang94 Jan 19 '17 at 22:43
0

as findById and .destroy return Promises, there is no need for the Promsie constructor

Your code is then simplified to

function deleteOrg(id) {
    return findById(id)
    .then((orgObject) => {
        if (orgObject.verified_at !== null) {
            throw new Error(422, 'Unable to delete organization')
        }

        //Organization is not verified, so proceed to delete
        return new Organization().where({'id': id}).destroy()
        .catch((err) => {
            throw (new Error(500, `Unable to delete organization: ${err.message}`));
        });
    });    
}
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
0
original method() => {
             try{ //code to raise the exceptio


            })

;

The best way to handle is to use expect It can be matched with an exception. A sample test

  someMock.mockFunc(() => {
          throw new Error("Something");
        });

   test('MockFunc in error', () => {
     return expect(orginalCall()).rejects.toMatch('Something');
   });
kbvishnu
  • 14,760
  • 19
  • 71
  • 101