Skip to content

Result support: allow propagation of CancelledError in catch #568

@emizzle

Description

@emizzle

For when Result support will be more tightly integrated into chronos, it would be nice to have a catch that takes into account, and propagates, CancelledError, eg:

template catchAsync*(body: typed): Result[type(body), ref CatchableError] =
  ## Catch exceptions for `body` containing `await` and store them in the
  ## Result, propagating cancellations.
  ##
  ## ```
  ## let r = catchAsync: await someAsyncFuncThatMayRaise()
  ## ```
  type R = Result[type(body), ref CatchableError]

  try:
    when type(body) is void:
      body
      R.ok()
    else:
      R.ok(body)
  except CancelledError as eCancelled:
    raise eCancelled
  except CatchableError as eResultPrivate:
    R.err(eResultPrivate)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions