ctietze,
@ctietze@mastodon.social avatar

Are there any API design best practices about error handling?

So far, I sketched everything with Result<Value,Error> so I get typed errors.

But both stack unwinding and the lack of ceremony of throws is appealing, too. API consumers probably don't need fine grained detail which of the 20 error cases occurred.

mattiem,
@mattiem@mastodon.social avatar

@ctietze I think in practice, there are very few reasons why callers should care about error types. The binary worked/failed is so applicable to so many situations, and I think you have to have a very compelling reason to not use it.

icanzilb,
@icanzilb@mastodon.social avatar

@ctietze your clients know which exact error is being thrown if they care to check, do you imply throws are untyped?

icanzilb,
@icanzilb@mastodon.social avatar

@ctietze that happens at runtime though, for compile time typed error handling you have to wait for the proposal to get implemented in a release toolchain

ctietze,
@ctietze@mastodon.social avatar

@icanzilb In my API I (currently) quite like that I have a way to express this:

protocol Expression {  
 associatedtype Evaluation  
 associatedtype Failure: Error  
 func evaluate(in buffer: Buffer) -> Result<Evaluation, Failure>  
}  

And offer overloads where Failure==Never and stuff. The actual types are very aware of their limitations and capabilities, and wrapping concrete FooError in BarError without having to account for any Error is nice.

icanzilb,
@icanzilb@mastodon.social avatar

@ctietze yes, I get it - type checking this way is handy 👍🏼

a40yostudent,
@a40yostudent@iosdev.space avatar

@ctietze I don’t know if I can well understand your question, and that’s my fault because I’m neither fluent in English nor in Swift, but I hope this will be helpful. https://github.com/apple/swift-evolution/blob/main/proposals/0413-typed-throws.md

ctietze,
@ctietze@mastodon.social avatar

@a40yostudent yeah I'm somewhat hoping for typed throws in the future 👍

pilky,
@pilky@mastodon.social avatar

@ctietze Usually I’d only use Result for an async API, so I’ve mostly been switching to async await so I can use throws instead. The only remaining places would be anywhere I need an NSOperation

ctietze,
@ctietze@mastodon.social avatar

@pilky I'm at a point where I believe I'll expose a throwing API to clients because it's much nicer to work with.

Internally, I'll stick to Result to express non-failability on the type level for certain commands.

Otherwise, I'd need to work with @​rethrows as a protocol annotation to conditionally have a conforming type be throwing or not, and that gets a bit weird IMHO

  • All
  • Subscribed
  • Moderated
  • Favorites
  • swift
  • DreamBathrooms
  • mdbf
  • ethstaker
  • magazineikmin
  • cubers
  • rosin
  • thenastyranch
  • Youngstown
  • osvaldo12
  • slotface
  • khanakhh
  • kavyap
  • InstantRegret
  • Durango
  • JUstTest
  • everett
  • tacticalgear
  • modclub
  • anitta
  • cisconetworking
  • tester
  • ngwrru68w68
  • GTA5RPClips
  • normalnudes
  • megavids
  • Leos
  • provamag3
  • lostlight
  • All magazines