Skip to content

Add an errorMessage(e: unknown) helper and retire 101 'as Error' casts #771

Description

@InfinityBowman

77 catch blocks in non-test source do (err as Error).message (101 counting tests). If a non-Error is thrown, the message is undefined and the user sees an empty error.

// packages/web/src/stores/authStore.ts -- 10 occurrences in this file alone
catch (err) { set({ authError: (err as Error).message }); }

useUnknownInCatchVariables is already on via strict, so err is correctly unknown -- the cast is throwing that away.

Fix

One helper in @corates/shared:

export function errorMessage(e: unknown): string {
  return e instanceof Error ? e.message : String(e);
}

Name it to avoid getErrorMessage(code: string), which already exists in packages/shared/src/errors/helpers.ts and maps an error code to its default message. Then convert the call sites. Note that several files already do this correctly inline (err instanceof Error ? err.message : String(err) appears in the guards), so the helper also de-duplicates that.

Done when

  • Helper exists in @corates/shared
  • No as Error in non-test source (currently 77)

Effort: 2-3 hours, mechanical.

Carried over from packages/docs/audits/typescript-audit-2026-04.md item 9, which measured ~100 in April counting tests. It is 101 now on the same basis, 77 in non-test source.


Part of #778 (TypeScript correctness target state and tracker).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions