generic throw giving Expected an object to be thrown lint error
throw Object.assign( new Error(myMessage), { code: 402 });
Throw a regular error and extend it with custom fields.
You could also write a reusable error class for that:
class CodeError extends Error { constructor(message, code) { super(message); this.code = code; } } throw new CodeError(myMessage, 404);
That way, you can distinguish the errors easily on catching:
} catch(error) { if(error instanceof CodeError) { console.log(error.code); } else { //... } }
Another simple workaround is store on variable and throw.
const errorMessage = { code : 403, message : myMessage };throw errorMessage;