Skip to content

Latest commit

 

History

History
129 lines (99 loc) · 5.01 KB

try-catch.md

File metadata and controls

129 lines (99 loc) · 5.01 KB

simple try

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  let result = "";
  try {
    result = "succeeded";
    throw new Error("fail");
  } catch {
    result = "failed";
  }
  return result;
});

reference error

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  let result = "";
  try {
    result = "succeeded";

    //asl.createError will create an node error with Error and Cause properties
    throw asl.runtime.createError("Test Error", "Failed on purpose");
  } catch (err) {
    result = `failed ${(err as asl.AslError).Error} (${
      (err as asl.AslError).Cause
    })`;
  }
  return result;
});

simple multiple statements

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  try {
    const arr = [1];
    const withinTry = arr.map((x) => "succeeded");
    return withinTry[0];
  } catch {
    return "it failed";
  }
});

try around pass state

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  try {
    return "this cannot fail";
  } catch {
    return "this never happens";
  }
});

try finally

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  try {
    await Promise.all([() => "succeeded"]);
  } finally {
    return "finally";
  }
});

try catch finally

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  let result = "";
  try {
    result = "try";
  } catch {
    result = "catch";
  } finally {
    result = "finally";
  }
  return result;
});

try catch fail state

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  try {
    return asl.fail({
      error: "InternalFailure",
      cause: "bad luck",
    });
  } catch (e) {
    const aslError = e as asl.AslError;
    if ("Error" in aslError && "Cause" in aslError) {
      return `${aslError.Error} (${aslError.Cause})`;
    }
  }
  return "this should not happen";
});