# Precise error typing in Swift

**URL:** <https://forums.swift.org/t/precise-error-typing-in-swift/52045>\
**Category:** Discussion\
**Tags:** error-handling\
**Created:** [September 14, 2021, 9:24am UTC](https://forums.swift.org/t/precise-error-typing-in-swift/52045 "2021-09-14T09:24:56Z")\
**Posts on this page:** 20\
**Page:** 4

<div class="post-metadata">

**Author:** ![Jeehut](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeehut/32/21743_2.png) [@Jeehut](https://forums.swift.org/u/Jeehut)\
**Post date:** [April 8, 2022, 7:29pm UTC](https://forums.swift.org/t/precise-error-typing-in-swift/52045/63 "2022-04-08T19:29:52Z")

</div>

I understand the framework argument. But this isn't code for a framework, it's already at the end of the chain: It's code inside an app project. I don't need to make my type frozen or add an unknown case, because when I change the enum (= add new cases), I can also change all callers of the enum (= the app code). And for this task, I'd actually be happy if the compiler failed to build the call site when I add a new case, because then I know exactly where I need to make changes.

---

<div class="post-metadata">

**Author:** ![Michael\_Ilseman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/michael_ilseman/32/44640_2.png) [@Michael\_Ilseman](https://forums.swift.org/u/Michael_Ilseman)\
**Post date:** [April 8, 2022, 7:31pm UTC](https://forums.swift.org/t/precise-error-typing-in-swift/52045/64 "2022-04-08T19:31:29Z")

</div>

Why is it public? That's what threw me. If you have no users other than yourself, then there are fewer problems. I think the argument for non-public typed throws is very strong, it's when you actually have users that things get complicated.

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [April 8, 2022, 7:32pm UTC](https://forums.swift.org/t/precise-error-typing-in-swift/52045/65 "2022-04-08T19:32:52Z")

</div>

As long as the language offers an easy, automatic way to erase the error, I really don't see the problem here. As a framework author I'd rather offer precise errors the user can erase with a standard mechanism than erased errors the user has to recover by knowing about my error types.

IMO, it's simple enough to have `throws` and `throws ErrorType`, where any throw of a typed error from within a `throws` function will automatically be erased to `Error`.

---

<div class="post-metadata">

**Author:** ![Jeehut](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeehut/32/21743_2.png) [@Jeehut](https://forums.swift.org/u/Jeehut)\
**Post date:** [April 8, 2022, 7:33pm UTC](https://forums.swift.org/t/precise-error-typing-in-swift/52045/66 "2022-04-08T19:33:22Z")

</div>

It's public because I'm modularizing my app. See here: [Episode #171: Modularization: Part 1](https://www.pointfree.co/episodes/ep171-modularization-part-1)

---

<div class="post-metadata">

**Author:** ![Saklad5](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Saklad5](https://forums.swift.org/u/Saklad5)\
**Post date:** [April 8, 2022, 7:34pm UTC](https://forums.swift.org/t/precise-error-typing-in-swift/52045/67 "2022-04-08T19:34:35Z")

</div>

> [@Jeehut](#):
>
> It's public because I'm modularizing my app.

This is the main reason I want precise errors: they make reasoning about specialized micropackages far easier.

---

<div class="post-metadata">

**Author:** ![davepaul0](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/davepaul0/32/22766_2.png) [@davepaul0](https://forums.swift.org/u/davepaul0)\
**Post date:** [April 9, 2022, 2:45pm UTC](https://forums.swift.org/t/precise-error-typing-in-swift/52045/68 "2022-04-09T14:45:59Z")

</div>

+1000 on this - I work heavily on both local and shared Swift packages. I'd love to be able to provide async APIs with strong error type guarantees - currently I have to use Combine or `Result` types for this.

Will the consuming clients erase the errors downstream in their error handling pipelines? Maybe. But at least I can give them an API error contract that's enforced by the compiler.

---

<div class="post-metadata">

**Author:** ![sveinhal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sveinhal/32/3992_2.png) [@sveinhal](https://forums.swift.org/u/sveinhal)\
**Post date:** [April 23, 2022, 10:36am UTC](https://forums.swift.org/t/precise-error-typing-in-swift/52045/69 "2022-04-23T10:36:18Z")

</div>

> [@Michael\_Ilseman](#):
>
> Do you intend for this enum to be `@frozen` or not? If it's not frozen, then users will still need a fall-through for unknown cases. If you want to add `@frozen` but keep the ability to throw new kinds of errors, you could add a `case unknown(Error)` , which lands users in the exact same boat but with an extra level of error wrapping. If you wanted to leave the enum exactly as-is, it's still the case that thrown foundation errors and type decoding errors are wrapped up in an extra layer instead of just throwing them directly.

Even if the throwing function is in a public library, and even if the typed error enum isn't `@frozen`, it's still adds value for framework users, non? It gives me a great starting point to discover common, high-level errors that may require special handling or user interactions.

I'd much rather have the compiler tell me that the common errors are file-not-exists, file-is-dir and that I also have to deal with `@unknown` errors, rather than having no help at all.

---

<div class="post-metadata">

**Author:** ![Jeehut](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeehut/32/21743_2.png) [@Jeehut](https://forums.swift.org/u/Jeehut)\
**Post date:** [June 26, 2022, 6:24pm UTC](https://forums.swift.org/t/precise-error-typing-in-swift/52045/70 "2022-06-26T18:24:59Z")

</div>

I just wrote a pitch kinda related to this thread (it's about `Result`, actually), see here:

> [@If/guard-let-catch for conveniently accessing both Result type cases](https://forums.swift.org/t/if-guard-let-catch-for-conveniently-accessing-both-result-type-cases/58492):
>
> Motivation A few weeks ago, I had [asked on Twitter](https://twitter.com/jeehut/status/1524056421494669312) if developers prefer error handling in Swift by using throws vs. returning Result. Of the 102 votes, 45% voted for "I prefer Result", which is 40% more than the 32% who voted for "I prefer throws" ([source](https://twitter.com/jeehut/status/1524056425076514817)). While this small survey isn't representative by any means, it still proves that there's clearly high interest in using Result for error handling (and not only for closure return types), even if that means we have to build nested error types…

---

<div class="post-metadata">

**Author:** ![JuneBash](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/junebash/32/19323_2.png) [@JuneBash](https://forums.swift.org/u/JuneBash)\
**Post date:** [July 14, 2022, 9:01pm UTC](https://forums.swift.org/t/precise-error-typing-in-swift/52045/71 "2022-07-14T21:01:52Z")

</div>

Are there still plans or work in progress for this? This, the `@rethrows` attribute, and the lack of a primary associated type for `AsyncSequence` means that certain APIs are currently impossible or unwieldy. Take for example a redux-like system with the following protocol.

```swift
protocol Reducer<State, Action> {
  associatedtype State 
  associatedtype Action

  func reduce(state: inout State, action: Action) -> any AsyncSequence<Action, Never>
}

```

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [July 15, 2022, 4:38pm UTC](https://forums.swift.org/t/precise-error-typing-in-swift/52045/72 "2022-07-15T16:38:17Z")

</div>

There hasn't been any concrete action here, no; the team has been wrapped up in other work.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [July 15, 2022, 5:35pm UTC](https://forums.swift.org/t/precise-error-typing-in-swift/52045/73 "2022-07-15T17:35:10Z")

</div>

One thing that caught my attention recently was the `distributed actor` implication for error handling.

A non throwing `distributed func foo()` on a distributed actor becomes throwing. Does it mean that a distributed operation cannot ever have a concrete error type and must erase any error (if any) to `Error`?

Follow up questions. Now that we have `any` and `some`:

- do we now throw `any Error`?
- could we have opaque errors and throw `some Error` with typed error feature?

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [July 15, 2022, 5:43pm UTC](https://forums.swift.org/t/precise-error-typing-in-swift/52045/74 "2022-07-15T17:43:37Z")

</div>

In principle, I think the _ad hoc_ checking for distributed code could merge the error types from the actor method and the actor's chosen transport protocol, so if they agreed on a non-`Error` error type, you wouldn't have to erase all the way to `Error`.

That said, a network service seems like exactly the sort of situation for which I have argued before that trying to lock down the error type is usually counter-productive, and you are better off just using `Error`.

---

<div class="post-metadata">

**Author:** ![ktoso](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ktoso/32/44526_2.png) [@ktoso](https://forums.swift.org/u/ktoso)\
**Post date:** [July 16, 2022, 4:51am UTC](https://forums.swift.org/t/precise-error-typing-in-swift/52045/75 "2022-07-16T04:51:20Z")

</div>

Locking down error types across network boundaries is a mess imho and counter productive. but if we had to be more precise that’s why the distributed module includes a “DistributedActorSystemError” protocol to which errors thrown by the actor system (transport) should conform… in theory then actual errors then a would be “SomeErrorTheFunctionThrows | DistributedActorSystemError” but I know we’re missing union types and there did not seem to be much love for them in the team, at least last time I mentioned them.

In concrete runtimes (the cluster) we even require allow-listing error types before we try to encode them, as to not accidentally leak information to remote callers.

Having that said, the less tied up remote peers are in exact impl details of another the better; and especially errors are not that helpful imho.

---

<div class="post-metadata">

**Author:** ![minuscorp](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/minuscorp/32/8404_2.png) [@minuscorp](https://forums.swift.org/u/minuscorp)\
**Post date:** [July 17, 2022, 4:41pm UTC](https://forums.swift.org/t/precise-error-typing-in-swift/52045/76 "2022-07-17T16:41:05Z")

</div>

This recalls the discussion and proposal made [here](https://forums.swift.org/t/typed-throws/39660) time ago. With the introduction of Concurrency, consumers keep losing information about error handling and it's inevitable in a way or another for Swift to provide a visible error handling interface as it does with Result and Combine.

---

<div class="post-metadata">

**Author:** ![ksluder](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@ksluder](https://forums.swift.org/u/ksluder)\
**Post date:** [July 17, 2022, 9:45pm UTC](https://forums.swift.org/t/precise-error-typing-in-swift/52045/77 "2022-07-17T21:45:00Z")

</div>

It’s not clear why you think it’s “inevitable”?

---

<div class="post-metadata">

**Author:** ![minuscorp](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/minuscorp/32/8404_2.png) [@minuscorp](https://forums.swift.org/u/minuscorp)\
**Post date:** [July 17, 2022, 10:21pm UTC](https://forums.swift.org/t/precise-error-typing-in-swift/52045/78 "2022-07-17T22:21:16Z")

</div>

It's my own opinion but the current throw system is totally erasing sufficiently error information wether his _rivals_ are specifically typing it. There are technical reasons to go for Concurrency, but error management is not over of them vs its competitors that, surprisingly, were added when the language got certain maturity. Why should I have to choose between typed or not typed error handling depending on the framework or API that I'm consuming? There are many explanations of this in the linked pitch so I think I shouldn't extend much further about my opinion.

---

<div class="post-metadata">

**Author:** ![ksluder](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@ksluder](https://forums.swift.org/u/ksluder)\
**Post date:** [July 17, 2022, 11:37pm UTC](https://forums.swift.org/t/precise-error-typing-in-swift/52045/79 "2022-07-17T23:37:51Z")

</div>

There’s a difference between losing error information and not exposing it in the type system. Swift wants you to handle things like enum cases exhaustively. It would be a serious ABI stability challenge if APIs were locked to a fixed set of error types forever.

When you control both sides of the ABI (like the interface between your model and view code), it’s not as big of a deal.

---

<div class="post-metadata">

**Author:** ![davedelong](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/davedelong/32/38708_2.png) [@davedelong](https://forums.swift.org/u/davedelong)\
**Post date:** [July 18, 2022, 12:19am UTC](https://forums.swift.org/t/precise-error-typing-in-swift/52045/80 "2022-07-18T00:19:37Z")

</div>

> [@ksluder](#):
>
> It would be a serious ABI stability challenge if APIs were locked to a fixed set of error types forever.

How would it be any different from locking a return value to a fixed type?

```swift
func getFoo() throws -> Foo

```

is homomorphic to

```swift
func getFoo() -> Result<Foo, Error>

```

Specifying a precise error type as the `Result` parameter would present the same sort of ABI stability challenges as the Success type. Am I missing something?

---

<div class="post-metadata">

**Author:** ![ksluder](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@ksluder](https://forums.swift.org/u/ksluder)\
**Post date:** [July 18, 2022, 12:21am UTC](https://forums.swift.org/t/precise-error-typing-in-swift/52045/81 "2022-07-18T00:21:12Z")

</div>

> [@davedelong](#):
>
> How would it be any different from locking a return value to a fixed type?

It’s not. Return types are part of the ABI. Opaque return types give implementations the ability to change their concrete return type as they evolve. Encouraging people to use more specific error types is a motion in the opposite direction. It increases fragility, which is tolerable only when you tightly control both sides of the interface.

---

<div class="post-metadata">

**Author:** ![davedelong](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/davedelong/32/38708_2.png) [@davedelong](https://forums.swift.org/u/davedelong)\
**Post date:** [July 18, 2022, 12:28am UTC](https://forums.swift.org/t/precise-error-typing-in-swift/52045/82 "2022-07-18T00:28:10Z")

</div>

> [@ksluder](#):
>
> Encouraging people to use more specific error types is a motion in the opposite direction.

I don't see it as moving in the opposite direction, so much as filling out the type-safety story of Swift. It has always struck me as odd that one of the main motivations for the creation of Swift was Objective-C's lack of type safety (_`id` has entered the chat_). Yet in Swift, we've somehow ended up in the place where we want descriptive types everywhere ... except with our errors?

Adding precise error typing would not stop anyone doing `func getFoo() throws`; we would still deal with `any Error` values just as we currently do. But for the API authors who _can_ make guarantees about the kinds of errors we produce... why should we preclude that?

[Previous page](https://forums.swift.org/t/precise-error-typing-in-swift/52045.md?page=3)

[Next page](https://forums.swift.org/t/precise-error-typing-in-swift/52045.md?page=5)
