async function handlePersonalInformation(event) {
event.preventDefault();
const formData = initialInfo(data);
try {
await getPersonalInfo();
try {
await updatePersonalInfo(formData);
alert('Successfully updated!');
} catch (error) {
handleError(error);
}
} catch (error) {
try {
await createPersonalInfo(data);
alert('Your personal infromation has been saved');
} catch (error) {
handleError(error);
}
}
}
I am trying to refactor this function using only one try / catch block.
Unfortunately without success.
The problem with this code is that exception thrown by getPersonalInfo is treated as a normal business case: you always go with 'create...' fork if there's an error. Yet I don't think that's the correct implementation: what if that request failed because of some other reason, different from 404?
The better approach seems to be isolating this function, making it only throw real errors:
function wrappedGetPersonalInfo(data) {
try {
const res = await getPersonalInfo(data);
return res;
} catch (e) { // I wish there were typed catches
if (e instanceof NotFoundError) { // the check should suit your case
return null;
}
throw e;
}
}
... then you'll be able to streamline your code quite a lot:
try {
const res = await wrappedGetPersonalInfo(data);
if (res) {
await updatePersonalInfo(formData);
alert('Successfully updated!');
}
else {
await createPersonalInfo(data);
alert('Your personal infromation has been saved');
}
} catch (error) {
handleError(error);
}
Related
I can't catch an exception thrown by the service worker's message event..
The client uses following code to execute the command on the SW:
import { messageSW } from "workbox-window";
// .. code for Workbox initialization/registration omitted
messageSW(registration?.active, { type: "SYNC" })
.then((results) => {
console.log("done");
})
.catch((e) => {
console.error(e);
});
On the SW (sw.js) side I have the following code:
self.addEventListener("message", async (event) => {
if (requestType === "SYNC") {
event.ports[0].postMessage(await longRunningTask());
}
});
This solution works OK as long as the SW is not throwing any exceptions. Meaning that the client prints the "done" message after the long running process on the SW is executed. If the exception is thrown nothing gets returned, ever.
I have managed to fix the problem by doing the following:
self.addEventListener("message", async (event) => {
if (requestType === "SYNC") {
try {
event.ports[0].postMessage(await longRunningTask());
} catch (error) {
event.ports[0].postMessage(error);
}
}
});
In this case - the result is always returned regardless, "done" is printed, but:
how do I actually produce an exception from the service worker, so the client could catch and handle it?
In general it would be good to hear if what I am doing is an appropriate approach to how asynchronous code on the SW shall be invoked from the client...
Here is my own solution I ended up using:
On service worker side - helper method:
async function replyToSenderAsync(event, task) {
let isCanReply = event.ports && event.ports.length >= 0;
try {
const result = await task();
if (isCanReply) {
event.ports[0].postMessage({ error: null, message: result });
}
} catch (error) {
if (isCanReply) {
event.ports[0].postMessage({ error: error, message: null });
}
}
}
When exception is caught we set the error property. Use as:
self.addEventListener("message", async (event) => {
const requestType = event?.data?.type;
if (requestType === "QUEUE_CLEAR") {
await replyToSenderAsync(event, async () => await clearQueueAsync());
}
});
On client side request wrapper:
function sendMessageToSWAsync(targetSW, messageType, message) {
return new Promise(function (resolve, reject) {
if (
!isServiceWorkerSupported.value ||
!isServiceWorkerRegistered.value ||
!targetSW
) {
reject(new Error("Unable to send the message to a service worker"));
}
try {
messageSW(targetSW, { type: messageType, message: message })
.then((messageResponse) => {
if (!messageResponse) {
reject(new Error("Service worker responsed with empty response"));
} else {
if (messageResponse.error) {
reject(messageResponse.error);
} else {
resolve(messageResponse.message);
}
}
})
.catch((messageError) => {
reject(messageError);
});
} catch (error) {
reject(error);
}
});
}
The magic here is to read the error property and reject the promise if that is the case (hence causing an exception to be thrown). Use as
try {
let response = await sendMessageToSWAsync(registration?.active, "QUEUE_GET_ALL");
}
catch(error) {
}
sendMessageToSWAsync(registration?.active, "QUEUE_GET_ALL")
.then((response) => {})
.catch((error) => {})
async await is very confusing. I have the below code and here I am trying to implement a create user function that first needs to check if the user exists in my MySQL database. I have the initial setting of the user where I call await on my findByUsername function. However, The function finishes after my if(!user) code has been executed.
I want the program to wait for the findByUsername function to finish and then perform the check on whether the user exists or not.
const { username, password, firstname, permission } = req.body;
let user = await User.findByName(req.body.username, (err, data) => {
if (err) {
if (err.kind === "not_found") {
console.log("finished");
return null;
} else {
console.error("error occurred");
}
} else {
console.log("finished too");
return data;
}
});
if (!user) {
console.log("couldnt find user");
res.status(404).send("couldnt find it :(");
} else {
console.log("found user");
res.send("found them");
}
===EDIT===
I am getting another that has also been confusing me where it says that result is not a function inside of my findByName function on my User model.
sql.query(`SELECT * from users WHERE username = '${username}'`, (err, res) => {
if (err) {
console.log("error ", err);
result(err, null);
return;
}
if (res.length) {
console.log("found user: ", res[0]);
result(null, res[0]);
return;
}
console.log("couldnt find");
return { kind: "not_found" }, null;
});
};
We must use await inside an async function.
For example:
const getUser = async (username) => await User.findByName(username);
Then call that function inside a try catch
try {
user = getUser(someUsername)
} catch (error) {
// Handle errors here
}
// Code here will only run once getUser is finished
Since i don't know your UserService i don't know if "User.FindByName" is a async function. but if that is the case you could do it like this:
const { username, password, firstname, permission } = req.body;
try {
let user = await User.findByName(req.body.username);
if (!user) {
console.log("couldnt find user");
res.status(404).send("couldnt find it :(");
} else {
console.log("found user");
res.send("found them");
}
} catch (e) {
res.status(400).send(e);
}
Are you able to change the definition of User.findByName?
Instead of using await and also passing in a callback, why not remove the callback and process it synchronously?
let user = await User.findByName(username);
// Handle response here
this will solve your Problem If you are using sequelize with mysql you can use where
condition in your code so you can use more conditions in it
const { username, password, firstname, permission } = req.body;
try {
let user = await User.findOne({
where: { username: req.body.username },
attributes: { exclude: ['password', 'signup_token'] }
});
if (!user) {
console.log("couldnt find user");
res.status(401).send("couldnt find user with username" +
(req.body.username));
} else {
console.log("User Found");
return res.status(200).send("User Found With Username" +
(req.body.username));
}
} catch (error){
console.log(error);
return res.status(500).send(error);
}
The function finishes after my if(!user) code has been executed.
That is because you are mixing async/await with callbacks which you should not.
// Here you are mixing async/await and callbacks
let user = await User.findByName(req.body.username, (err, data) => { /*....*/ });
// it is the same as
function findByNameCallback(err, data) { /*.....*/ }
let user = await User.findByName(req.body.username, findByNameCallback);
// -- So you either remove the callback from the equation
var user;
try {
user = await User.findByName(req.body.username);
} catch (err) {
// Handle error
}
// Work with the user object...
// ----
// Or remove the async/await stuff
User.findByName(req.body.username, findByNameCallback);
I'm trying to better understand error handling while using async/await, third party libs, and external APIs.
Given the following pseudo-code:
const createConnectionRequest = async (args) => {
try {
const { data } = axios.post(url, args);
return data;
} catch (err) {
throw new Error(err);
}
}
My understanding is the throw would occur a result of the axios.post failing rather than an issue with my request.
If the response from my API was 200 but included an error of some sort, eg.
{
status: 200,
error: 'Invalid fields supplied',
}
Would I throw this error in the try block and expect a parent calling function to catch it?
const createConnectionRequest = async (args) => {
try {
const { data } = axios.post(url, args);
if (data.error) {
throw new Error(data.error);
}
return data;
} catch (err) {
throw new Error(err);
}
}
...
const processor = async () => {
try {
await createConnectionRequest(...);
} catch (err) {
// handle error from API response here
throw new Error(err);
}
}
I use multiple same try/catch test on many object method. So, i would like create try/catch method for refacto my code, but the error is not returned.
For exemple :
#autobind
async forgottenPassword(req, res) {
return this.callService(
res,
async () => await companyService.forgottenPassword(req.body.formData)
);
}
callService(res, func) {
try {
func();
} catch (error) {
res.statusMessage = error.message;
res.status(error.statusCode);
} finally {
res.end();
}
}
My catch is never called :/
Anyone know did I make a mistake ?
Thank you !
You need to make your callService async and use await there too.
#autobind
async forgottenPassword(req, res) {
return this.callService(
res,
async () => await companyService.forgottenPassword(req.body.formData)
);
}
async callService(res, func) {
try {
await func();
} catch (error) {
res.statusMessage = error.message;
res.status(error.statusCode);
} finally {
res.end();
}
}
I'm working on an app for which we're using promises. I'm attempting to figure out what the better pattern is.
Breaking up concerns between thenables and rejecting the promise if an error. Rejection is then handled in a catch block. Or is it better to throw a new type of error and handle in the catch block?
Account.findOneAsync({email: request.payload.email})
.then(function (user) {
if (user) {
return user.compareHash(request.payload.password);
} else {
// Account not found
return Bpromise.reject('AccountNotFound');
}
})
.then(function (validPassword) {
if (validPassword) {
return request.auth.jwt.user.sign();
} else {
// Invalid password
return Bpromise.reject('InvalidPassword');
}
})
.then(function (jwt) {
var response = reply.success();
return response.header('authorization', jwt);
})
.catch(function (e) {
if (e === 'AccountNotFound' || e === 'Invalid Password') {
return reply(Boom.unauthorized('Invalid username/password'));
} else {
// Perhaps log something like unhandled error
return reply(Boom.unauthorized('Invalid username/password'));
}
});
Or nesting promising as such. I feel here that this is just going down the same rabbit hole of "callback hell" though.
Account.findOneAsync({email: request.payload.email})
.then(function (user) {
if (user) {
user.compareHash(request.payload.password)
.then(function (valid) {
if (valid) {
request.server.plugins.jwt.sign()
.then(function (jwt) {
var response = reply.success();
return response.header('authorization', jwt);
});
} else {
// Invalid password
return reply(Boom.unauthorized('Invalid username/password'));
}
});
} else {
// Account not found
return reply(Boom.unauthorized('Invalid username/password'));
}
})
.catch(function (e) {
console.log(e);
});
I think you can get the best of both worlds by throwing and then catching your boom objects.
One thing you're missing in both approaches is that when you're already inside a then handler, the idiomatic thing to do is throw an error rather than creating and returning a rejected promise. You also don't need an else block after a return statement:
Account.findOneAsync({email: request.payload.email})
.then(function (user) {
if (user) {
return user.compareHash(request.payload.password);
}
// Account not found
throw Boom.unauthorized('Invalid username/password');
})
.then(function (validPassword) {
if (validPassword) {
return request.auth.jwt.user.sign();
}
// Invalid password
throw Boom.unauthorized('Invalid username/password');
})
.then(function (jwt) {
var response = reply.success();
return response.header('authorization', jwt);
})
.catch(function (e) {
if (e.isBoom) {
return reply(e);
}
// Perhaps log something like unhandled error
return reply(Boom.unauthorized('Invalid username/password'));
});