Unhandled promise rejection - key path incomplete - javascript

I keep getting an error from GCP regarding this, I am using datastore & deploying on GAE. Anyone have any ideas why I am getting this error using javascript promises?
I am using a google action to open on google home, ask for an activation keyphrase if the device has not been registered to an apartment number in datastore already. If it is not registered, it asks for a keyphrase that will associate the unique device id with an apartment number. If the unique id has an apartment associated with it, then is asks what it can help with.
I am not sure why it is saying the key path is incomplete. Also I am new to promises! So any help is greatly appreciated
UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 99): Error: Key path element must not be incomplete: [Activation: ]
With this code?
datastore.get(datastore.key([ACTIVATION, device_id]))
.then(results => {
let activation = null
if (results[0] ) {
activation = results[0]
}
return Promise.resolve(activation)
})
.then(activation => {
console.log(activation)
let actionMap = new Map();
actionMap.set('input.welcome', assistant => {
console.log('input.welcome')
if (!activation) {
assistant.ask("Hello! May I have your key phrase?")
}
else {assistant.ask("Welcome back, what can I do for you today?")
}
})
actionMap.set('input.unknown', assistant => {
console.log('input.unknown')
if (!activation) {
assistant.ask("Please provide your activation code")
} else
{
let speech = "OK"
if (request.body &&
request.body.result &&
request.body.result.fulfillment &&
request.body.result.fulfillment.messages &&
request.body.result.fulfillment.messages[0] &&
request.body.result.fulfillment.messages[0].speech) {
speech = request.body.result.fulfillment.messages[0].speech
}
sendSMSFromUnit(activation.number, request.body.result.resolvedQuery)
assistant.tell("Got it. ")
}
})
actionMap.set('input.keyphrase', assistant => {
let activationCode = TitleCase([
assistant.getArgument('Token1'),
assistant.getArgument('Token2'),
assistant.getArgument('Token3')
].join(" "))
console.log('activationCode: ' + activationCode)
if (activation && activation.keyphrase == activationCode) {
assistant.tell('This device is activated.')
return
}
datastore.get(datastore.key([APARTMENT, activationCode]))
.then(results => {
console.log(results)
if (!results[0]) {
assistant.ask('Activation unsuccessful. Can you provide your activation code again?')
return
}
let apartment = results[0]
datastore.insert({
key: datastore.key([ACTIVATION, device_id]),
data: {
name: apartment.name,
number: apartment.number,
keyphrase: activationCode,
device_id: device_id
}
}).then(() => {
assistant.ask('Thanks! ')
})
})
})

The whole pattern of a promise is
Promise((resolve, reject) => {
// ...
});
Now how to use it
promiseFunc(...)
.then((x) => {
// It get executed well
})
.catch((x) => {
// An error happened
});
In your code you are missing the .catch part. So if an error get thrown into your promise function you won't catch it and result of a node exception. That's why you have the following warning : Unhandled promise rejection

You are getting that error message because you are not catering for when
the promise rejects rather than resolves.
In your code where you call '.then', that is when the promise has resolved. But you have no action for when the promise is rejected. Take the following example;
// psuedo promise function which resolves if the data is good and rejects if the data is bad
function myPromiseFunction() {
return new Promise((resolve,reject) => {
// do something like make a http call here...
// if the response is good
return resolve(response)
// if the response is not so good
return reject(error)
});
}
// using the promise function
myPromiseFunction()
.then((response) => {
console.log(response);
}, (error) => { // <---- you are missing this part
console.log(error);
});
or you can write it this way
myPromiseFunction()
.then((response) => {
console.log(response);
})
.catch((error) => { // <---- you are missing this part
console.log(error);
})

Related

Catching promise errors inside another promise's callback

The code below runs as expected. If the charge function is invoked, the function fetches the relevant ticket object from firestore and then returns it back to the client.
If the ticket doesn't exist, the function throws a HttpsError with an error message that will be parsed by the client.
exports.charge = functions.https.onCall(data => {
return admin.firestore().collection('tickets').doc(data.ticketId.toString()).get()
.then((snapshot) => {
return { ticket: snapshot.data() }
})
.catch((err) => {
throw new functions.https.HttpsError(
'not-found', // code
'The ticket wasn\'t found in the database'
);
});
});
The problem comes after this. I now need to charge the user using Stripe, which is another asynchronous process that will return a Promise. The charge requires the pricing info obtained by the first async method, so this needs to be called after snapshot is retrieved.
exports.charge = functions.https.onCall(data => {
return admin.firestore().collection('tickets').doc(data.ticketId.toString()).get()
.then((snapshot) => {
return stripe.charges.create(charge) // have removed this variable as irrelevant for question
.then(() => {
return { success: true };
})
.catch(() => {
throw new functions.https.HttpsError(
'aborted', // code
'The charge failed'
);
})
})
.catch(() => {
throw new functions.https.HttpsError(
'not-found', // code
'The ticket wasn\'t found in the database'
);
});
});
My problem is with catching errors in the new charge request. It seems that if the charge fails, it successfully calls the first 'aborted' catch, but then it is passed to parent catch, and the error is overridden and the app sees the 'ticket not found' error.
How can I stop this from happening? I need to catch both errors separately and throw a HttpsError for each one.
Generally, such problems are can be handled with adding status node and then chaining with a final then block. You can try something like following
exports.charge = functions.https.onCall(data => {
return admin.firestore().collection('tickets').doc(data.ticketId.toString()).get()
.then((snapshot) => {
return stripe.charges.create(charge)
.then(() => {
return { success: true };
})
.catch(() => {
return {
status : 'error',
error : new functions.https.HttpsError(
'aborted', // code
'The charge failed',
{ message: 'There was a problem trying to charge your card. You have NOT been charged.' }
)};
})
})
.catch(() => {
return {
status : 'error',
error : new functions.https.HttpsError(
'not-found', // code
'The ticket wasn\'t found in the database',
{ message: 'There was a problem finding that ticket in our database. Please contact support if this problem persists. You have NOT been charged.' }
)};
}).then((response) => {
if(response.status === 'error') throw response.error;
else return response;
});
});
Don't nest a then inside another then for multiple items of work:
work1
.then((work1_results) => {
return work2.then((work2_results) => {
// this is bad
})
})
Instead, perform all your work as a chained sequence:
work1
.then((work1_results) => {
return work2
})
.then((work2_results) => {
// handle the results of work2 here
})
You can store intermediate results in higher-scoped variables if you need to accumulate data between your callbacks.

Using promises with Node JS express

I am newbie to promises, I am trying to understand how do they work.
Here my questions at first:
When request is handled in a route function, does it wait for all promises, I mean that when I am using promise or callback it is a new scope and execution continues further.
If I keep a req/res objects for example in timer and then respond to user, what will user see ? A request will just be blocked until I explicitly send response ?
So I have encountered the following problems.
Here is my route.
router.post('book', authHandler.provideMiddleware(), (req, res) => {
bookManager.createBook(req.body, {
onSuccess: function (data) {
respondSuccess(res,HttpStatus.OK, {'data': data});
},
onError: function (message) {
respondError(res, HttpStatus.HTTP_STATUS.BAD_REQUEST, {'error': message});
}
});
});
Inside bookmanager I have the following
createBook(data, hook) {
let book = createBookFromRequest(data);
let verifyData = new Promise((resolve, reject) => {
let valid = checkBookData(book);
if(valid) {
resolve(book);
}
else {
reject("Invalid data");
}
});
let createBook = new Promise((resolve, reject) => {
book.save((err, result) => {
if (!err) {
reject("Error while saving");
}
else {
resolve(result);
}
});
});
verifyData
.then(() => {
return createBook;
})
.then((data) => {
hook.onSuccess(data);
})
.catch((error) => {
hook.onError(error);
});
}
My idea is to chain multiple functions and if any error occurred, call hook.onError method, otherwise call on success.
I have several problems here.
When error is thrown, my book is still created.
I have the following error
node:8753) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 6): Error: Can't set headers after they are sent.
I want to use the approach like Rx (Reactive Extensions).
Could someone explain what is wrong and how do promises really work in this case ?
1. When request is handled in a route function, does it wait for all promises, I mean that when I am using promise or callback it is a new scope and execution continues further.
It waits for you to send a response via res. You don't have to do that in response to the event, it's absolutely normal for it to be later, after an asynchronous process (like a promise resolution) completes.
2. If I keep a req/res objects for example in timer and then respond to user, what will user see ? A request will just be blocked until I explicitly send response ?
Yes.
I have several problems here.
1. When error is thrown, my book is still created.
You're always starting the process of creating the book, regardless of whether the data was verified as correct. new Promise starts the work.
2. I have the following error
node:8753) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 6): Error: Can't set headers after they are sent.
You're creating a promise and storing it in createBook, and never handling rejections of that promise if verifyData rejects. So you get an unhandled promise rejection.
You can get rid of the entire new Promise around saving the book, and just put it in the verifyData chain; see comments:
createBook(data, hook) {
let book = createBookFromRequest(data);
let verifyData = new Promise((resolve, reject) => {
let valid = checkBookData(book);
if (valid) {
resolve(book);
}
else {
reject("Invalid data");
}
});
verifyData
.then(() => book.save()) // The chain takes on the state of the promise
// returned by `book.save`
.then(data => {
hook.onSuccess(data);
})
.catch((error) => {
hook.onError(error);
});
}
In that, I'm assuming createBookFromRequest and checkBookData are both synchronous processes, as that's how you're using them.
And actually, given that's the case, I don't see any need for the promise you're creating to verify the data. So it could be simpler:
createBook(data, hook) {
let book = createBookFromRequest(data);
if (checkBookData(book)) {
book.save()
.then(_ => hook.onSuccess(data))
.catch(error => hook.onError(error));
} else {
hook.onError("Invalid data");
}
}

Unhandled promise rejection in Node.js

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.
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}`));
});
});
}
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}`;
// ...
});
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');
});

Returning one object that's built with nested promises

I'm struggling to wrap my head around a nested promise layout where one one object is returned at the end of it. My current code is as follows:
router
router.get(`/${config.version}/event/:id?`, function (req, res, next) {
var event = new Event(req, res, next);
event.getInfo(req.params.id).then((info) => {
res.send(info);
});
});
function
getInfo(id) {
db.main('events').where('id', id).select()
.then((result) => {
if(result.length > 0) {
var event = result[0];
//regular functions
event.status = this.getStatus(id);
event.content = this.getContent(id);
event.price = this.getPrice(id);
//promise functions
var users = this.getUsers(id);
var hosts = this.getHosts(id);
Promise.all([users, hosts]).then(values => {
event.users = values[0];
event.hosts = values[1];
//return whole event object to router
return event;
})
.catch((err) => {
return {
result: 'error',
error: err
};
});
} else {
return {
result: 'error',
error: "Event does not exist"
};
}
}).catch((e) => {
return {
result: 'error',
error: "Could not retrieve event info"
};
});
}
As you can see, the router initiates a call to get info about an event. The function then does a database call and gets some event data. Thereafter I need to get the users and hosts of the event from a different table, append that info to the event object as well and then return the whole object to the router to be sent to the client.
When I do this I get an error because I'm not returning a promise from the getInfo function, but I'm not sure how or which promise I'm supposed to return.
I'd appreciate some help with this. Thanks
using .then means that you are returning a promise.
function getInfo(id) {
return new Promise(function(resolve, reject) {
resolve('yay!');
})
}
getInfo().then(function(result) { //result = yay! });
to make your code work, simply replace all the returns with resolves, the errors with rejects, and wrap the whole thing with a return new Promise as i did.
getInfo(id) {
return new Promise(function(resolve, reject) {
db.main('events').where('id', id).select()
.then((result) => {
if (result.length > 0) {
var event = result[0];
//regular functions
event.status = this.getStatus(id);
event.content = this.getContent(id);
event.price = this.getPrice(id);
//promise functions
var users = this.getUsers(id);
var hosts = this.getHosts(id);
Promise.all([users, hosts]).then(values => {
event.users = values[0];
event.hosts = values[1];
//return whole event object to router
resolve(event);
})
.catch((err) => {
reject({
result: 'error',
error: err
});
});
} else {
reject({
result: 'error',
error: "Event does not exist"
});
}
}).catch((e) => {
reject({
result: 'error',
error: "Could not retrieve event info"
});
});
});
}
Just wrap your async code in Promise like this:
getInfo(id) {
return new Promise(function(resolve, reject) {
db.main('events').where('id', id).select()
.then((result) => {
//...
resolve(/* result */)
// OR
reject(/* Error */)
})
}
Note: Use resolve and reject instead return
It's a combination of several things, but the main one is that you are never returning anything from getInfo, so your router handler is calling .then on undefined.
Do not call .catch (without throwing inside it) on Promises you intend to return for a caller to consume. This makes it not possible to use .catch, because you recovered the Promise chain into a resolved one.
Whatever you return inside a .then will be merged into the promise chain, so it's not actually a "Promise that resolves with a Promise". Your whole code could be replaced with:
getInfo (id) {
return db.main('events').where('id', id).select()
.then(result => {
if (result.length == 0) {
// you can also just throw your error object thing,
// but standard Error are generally the convention
throw new Error('Event does not exist')
}
const [event] = result
event.status = this.getStatus(id)
event.content = this.getContent(id)
event.price = this.getPrice(id)
return Promise.all([this.getUsers(id), this.getHosts(id)])
.then(([users, hosts]) => {
event.users = users
event.hosts = hosts
// this is the only value that
// this.getInfo(id).then(value => {/* ... */}) will see
return event
}
})
}

ES6 promises, chaining promises only if previous promises were rejected, while keeping rejection reasons

I'm building an authentication API that tries to authenticate against different authentication methods until one succeeds (like Spring Security).
Each authentication method returns a promise, fulfilled if the authentication did succeed, rejected if the authentication did fail.
To try to authenticate a user, I need call the authentication methods sequentially:
If the first authentication method succeeds the user is authenticated and I
don't want the other authentication methods to be called.
If the first authentication method fails, I need to try the second authentication method. And so on...
If none of the authentication methods succeed, I need to reject the authentication and maybe to
know the reasons every authentication did fail.
If an authentication succeeds I need to get back a value (authentication object).
This process is almost what promises libraries call .any (or .some) except that I don't want to execute every promises (hence try every authentication methods, which induces unnecessary workload). I want to try the next authentication method only if the previous failed.
Question 1: Is there a function available in a Promise/A+ compliant library that already does that ?
Question 2: I've been thinking of the following way (cf code below), is there any better way ?
// Defining some promise factories
var promiseFactory1 = {
buildPromise: function() {
return new Promise((resolve, reject) => {
console.log('trying p1...');
// resolve('p1 success');
reject('p1 failure');
})
}
};
var promiseFactory2 = {
buildPromise: function() {
return new Promise((resolve, reject) => {
console.log('trying p2...');
// resolve('p2 success');
reject('p2 failure');
})
}
};
var promiseFactory3 = {
buildPromise: function() {
return new Promise((resolve, reject) => {
console.log('trying p3...');
resolve('p3 success');
// reject('p3 failure');
})
}
};
// Building the promise
let promisesFactory = [promiseFactory1, promiseFactory2, promiseFactory3];
let rejections = [];
var reducedPromise = promisesFactory.reduce(function(promise, nextFactory) {
if (promise === null) return nextFactory.buildPromise();
return promise.catch(err => {
rejections.push(err);
return nextFactory.buildPromise();
});
}, null);
reducedPromise
.catch(err => {
rejections.push(err); // catching the last rejection
})
.then(success => {
if(rejections.length == promisesFactory.length) {
console.log(rejections);
// TODO return Promise.reject(new SomeCustomError());
} else {
console.log(success);
// TODO return Promise.resolve(success);
}
});
I believe you can do it like this (where auth1, auth2, and auth3 are the auth functions returning promises):
auth1(/*args*/)
.catch(failed => auth2(/*args*/))
.catch(failed => auth3(/*args*/))
.then(result => {
console.log("Success:" + result);
return result;
})
.catch(failed => {
console.log("Failure:" + failed);
});
Because we don't have then callbacks, the resolution value propagates to the final then (just like having .then(result => result)); but we return new promises from the catch callbacks, triggering the next authentication method.
The then at the end gets the resolution value of the first succeeding auth method; the final catch above just gets the rejection reason of the last failing auth method (auth3).
Live on Babel's REPL
If you need all the failing reasons, you could keep them in an array:
let failures = [];
auth1(/*args*/)
.catch(failed => {failures.push(failed); return auth2(/*args*/);})
.catch(failed => {failures.push(failed); return auth3(/*args*/);})
.then(result => {
console.log("Success: " + result + " (failed: " + failures.join(", ") + ")");
return result;
})
.catch(failed => {
failures.push(failed);
console.log("Failures: " + failures.join(", "));
});
Live Copy
If your auth methods are themselves in an array, you can do the above in a loop:
let auths = [auth1.bind(null, /*args*/), auth2.bind(null, /*args*/), auth3.bind(null, /*args*/)];
return auths.reduce((p, auth) => p.catch(failed => auth()), Promise.reject())
.then(result => {
console.log("Success: " + result);
return result;
})
.catch(failed => {
console.log("Failed: " + failed);
});
Live Copy

Categories

Resources