Skip to content

Conversation

mafredri
Copy link
Member

@mafredri mafredri commented Dec 8, 2023

The failure in https://github.com/coder/coder/actions/runs/7143038511/job/19453662466?pr=11102#step:10:430 was not very helpful and had me scratching my head for too long.

image

@mafredri mafredri force-pushed the mafredri/test-e2e-improve-defaults-error branch from 53704f8 to 74c375d Compare December 8, 2023 16:01
Agent.encode(agentResource);
} catch (e) {
throw new Error(
"agentResource encode failed, missing defaults? " + e,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One gotcha with JS errors: their default stringification method only grabs the error's name and message properties – the entire stack gets lost (partially because stack technically is a non-standard property, even though it's basically added everywhere)

I would go for something like this:

throw new Error(
  `agentResource encode failed, missing defaults?\n${e}\n${e.stack ?? ""}`
);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking to avoid the double stack trace (hence discarding the original one since we still create one at new Error), but I agree that the original stack can also be useful, I'll make the change 👍🏻

Copy link
Member

@mtojek mtojek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I trust Michael in terms of TypeScript 👍 .

Copy link
Member

@Parkreiner Parkreiner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. Looks good!

Agent.encode(agentResource);
} catch (e) {
throw new Error(
`agentResource encode failed, missing defaults?\n${e}\n${e.stack}`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, wait – forgot there's one other issue. Since JS/TS allow you to throw any kind of value at all, TS doesn't assume that a thrown value is actually an error, and just defaults to type unknown

You'd need a type guard to prove to the compiler that the value is an error before you try accessing error-specific properties from it

try {
  // Throw-able code
} catch (e) {
  let errorMessage  = `agentResource encode failed, missing defaults?\n${e}`;
  if (e instanceof Error) {
    errorMessage += `\n${e.stack}`;
  }
   
  throw new Error(errorMessage);
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obviously, because JS, the ${e.name}: ${e.message} is sometimes included in the stack trace (Node, etc.) and other times not (browser). Hopefully we now have a satisfying solution with the last commit. 😄

@github-actions github-actions bot added the stale This issue is like stale bread. label Dec 19, 2023
@github-actions github-actions bot closed this Dec 23, 2023
@mafredri mafredri reopened this Dec 23, 2023
@mafredri mafredri enabled auto-merge (squash) December 23, 2023 11:48
@mafredri mafredri merged commit be3889a into main Dec 23, 2023
@mafredri mafredri deleted the mafredri/test-e2e-improve-defaults-error branch December 23, 2023 11:52
@github-actions github-actions bot locked and limited conversation to collaborators Dec 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
stale This issue is like stale bread.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants