Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Add await to createTimeSeries so that onMetricUploadError can take ef… #978

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ywangjl
Copy link

@ywangjl ywangjl commented May 10, 2022

…fect

Currently the error thrown by createTimeSeries cannot be captured by onMetricUploadError, because the code is not run under the same context as the try catch block without "await".

Please see the following code illustration, toggle between "delayWithError(ms);" and "await delayWithError(ms);" to see the difference. Without await, the error is not caught and handled.

////////////////////////////////////////////////////
async function delay(ms: number): Promise {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}

async function delayWithError(ms: number) {
await delay(ms);
throw("some error");
}

async function test(ms: number) {
console.log(start first delay, waiting for ${ms}ms...);
await delay(ms);
console.log("Done first delay");
console.log(start second delay with error, waiting for ${ms}ms...);
// await delayWithError(ms);
delayWithError(ms);
}

async function main() {
const ms = 1000;
try{
await test(ms);
}catch(err){
console.log("caugh error:" + err);
}
}

main();
/////////////////////////////////////////////////////

…fect

Currently the error thrown by createTimeSeries cannot be captured by onMetricUploadError, because the code is not run under the same context as the try catch block without "await". 

Please see the following code illustration, toggle between "delayWithError(ms);" and "await  delayWithError(ms);" to see the difference. Without await, the error is not caught and handled.

////////////////////////////////////////////////////
async function delay(ms: number): Promise<void> {
  return new Promise<void>(resolve => {
    setTimeout(resolve, ms);
  });
}

async function delayWithError(ms: number) {
   await delay(ms);
   throw("some error");
}

async function test(ms: number) {
    console.log(`start first delay, waiting for ${ms}ms...`);
    await delay(ms);
    console.log("Done first delay");
    console.log(`start second delay with error, waiting for ${ms}ms...`);
    // await delayWithError(ms);
    delayWithError(ms);
}

async function main() {
  const ms = 1000;
  try{
      await test(ms);
  }catch(err){
      console.log("caugh error:" + err);
  }
}

main();
/////////////////////////////////////////////////////
@google-cla
Copy link

google-cla bot commented May 10, 2022

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

For more information, open the CLA check for this pull request.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant