In this example, createSchedule
makes an api request, if the api request fails I'd anticipate to log the message in my catch block since I am not handling the error in createSchedule
. Here is an abbreviated version of the code:
Call site:
Schedule.onCustomEvent('Schedule:created', async function () {
const schedule = this;
try {
return createSchedule(schedule);
} catch (e) {
console.log(`Schedule Error: Did not create schedule for ${schedule.name} : ${schedule._id}`);
console.error(e);
}
});
Definition:
const createSchedule = async (schedule) => {
// headers defined outside of func
// url defined outside of func
const { data } = await axios( {
method: 'post',
url,
headers,
data: {
schedule: {
name: schedule.name,
timezone: schedule.getTimezone() || 'US/Eastern'
},
enable_sis_reactivation: true,
}
})
return data;
}
Within createSchedule
I do not have an error handler. At the call-site, I have an error handler, however the logging in the handler isn't logged.
I tested this with providing bad credentials in the header so the post request would return a 401, this wasn't logged in my error handler at the call-site. In addition, I removed the url
variable defined above the createSchedule
function and got a reference error, however the catch block logs were not logged.
CodePudding user response:
try and catch only catch syntax errors what you need to do is to create an event listener and listen for the error and place the code to it. document.getElementById( the id).addEventListener("error",function(){
console.log(Schedule Error: Did not create schedule for ${schedule.name} : ${schedule._id}
);
});
CodePudding user response:
I think you are returning the whatever reponse you get, try below code
Schedule.onCustomEvent('Schedule:created', async function () {
const schedule = this;
try {
let create = await createSchedule(schedule);
return create
} catch (e) {
console.log(`Schedule Error: Did not create schedule for ${schedule.name} : ${schedule._id}`);
console.error(e);
}
});
const createSchedule = async (schedule) => {
// headers defined outside of func
// url defined outside of func
return new Promise((resolve, reject) => {
try {
const {
data
} = await axios({
method: 'post',
url,
headers,
data: {
schedule: {
name: schedule.name,
timezone: schedule.getTimezone() || 'US/Eastern'
},
enable_sis_reactivation: true,
}
})
return resolve(data);
}
cache(err) {
return reject(err)
}
})
}