Skip to content

Enhance safe() overloads#2

Merged
fprl merged 1 commit intodocsfrom
main
Dec 23, 2025
Merged

Enhance safe() overloads#2
fprl merged 1 commit intodocsfrom
main

Conversation

@fprl
Copy link
Copy Markdown
Owner

@fprl fprl commented Dec 23, 2025

No description provided.

Copilot AI review requested due to automatic review settings December 23, 2025 11:43
@fprl fprl merged commit 4848c1a into docs Dec 23, 2025
6 checks passed
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR enhances the safe() method to accept both Promise objects and functions that return values or promises, making it more ergonomic for error handling in both synchronous and asynchronous contexts.

Key Changes

  • Added function overload to safe() that accepts () => T | Promise<T> in addition to the existing Promise<T> signature
  • Updated implementations to detect function inputs and wrap them in Promises before error handling
  • Added test cases for synchronous function throws

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
packages/errata/src/errata.ts Updated safe type definition to support both function and promise inputs; modified implementation to handle function inputs by wrapping them in a Promise
packages/errata/src/client.ts Updated safe type definition to support both function and promise inputs; modified implementation to handle function inputs; minor comment update
packages/errata/test/errata.test.ts Added test case to verify synchronous throws from function inputs are properly captured and converted to ErrataError
packages/errata/test/client.test.ts Added test case to verify synchronous throws from function inputs are properly captured and converted to ErrataClientError

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

/** Promise helper returning a tuple without try/catch at call sites. */
const safe = async <T>(
promise: Promise<T>,
/** Promise/helper returning a tuple without try/catch at call sites. */
Copy link

Copilot AI Dec 23, 2025

Choose a reason for hiding this comment

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

The comment has a grammatical issue. "Promise/helper" should be "Promise helper or function" to properly describe that this function accepts both promises and functions.

Suggested change
/** Promise/helper returning a tuple without try/catch at call sites. */
/** Promise helper or function returning a tuple without try/catch at call sites. */

Copilot uses AI. Check for mistakes.
Comment on lines +213 to +221
it('handles synchronous throw from function input', async () => {
const [value, err] = await errors.safe(() => {
throw new Error('boom')
})

expect(value).toBeNull()
expect(err).toBeInstanceOf(errors.ErrataError)
expect(err?.code).toBe('errata.unknown_error')
})
Copy link

Copilot AI Dec 23, 2025

Choose a reason for hiding this comment

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

The test coverage for the new function input overload is incomplete. While synchronous throws are tested, there's no test for the case where a function returns a Promise (e.g., an async function or a function that returns Promise.resolve/reject). Consider adding a test case like:

it('handles async function that returns promise', async () => {
  const [value, err] = await errors.safe(async () => {
    throw new Error('async boom')
  })
  expect(value).toBeNull()
  expect(err?.code).toBe('errata.unknown_error')
})

Also consider testing successful async function returns.

Copilot uses AI. Check for mistakes.
Comment on lines +332 to +339
it('captures synchronous throws from function input', async () => {
const [data, err] = await client.safe(() => {
throw new Error('boom')
})

expect(data).toBeNull()
expect(err?.code).toBe('errata.unknown_error')
})
Copy link

Copilot AI Dec 23, 2025

Choose a reason for hiding this comment

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

The test coverage for the new function input overload is incomplete. While synchronous throws are tested, there's no test for the case where a function returns a Promise (e.g., an async function or a function that returns Promise.resolve/reject). Consider adding a test case like:

it('handles async function that returns promise', async () => {
  const [data, err] = await client.safe(async () => {
    throw new Error('async boom')
  })
  expect(data).toBeNull()
  expect(err?.code).toBe('errata.unknown_error')
})

Also consider testing successful async function returns.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants