This Week I Learned #29

Assumed audience: people interested in learning and metacognitive tools.

Here are some of the things I learned this week, in the order I’ve noted them down.

# Localized Errors in Swift

  • In a typical RTFM case, I just learned that localizedDescription was not defined in LocalizedError like I’ve been incorrectly assuming.

  • To make up for that, I am now more familiar with the properties that come with the LocalizedError protocol and how to use each to improve error handling in both server and client apps.

# Sync and Async Error Handling

  • Speaking of errors, dealing with synchronous errors using do/catch alongside asynchronous ones in SwiftNIO requires taking some special API design considerations. It’s easy to fall into the trap of writing code that can both throw and return futures, thus requiring two forms of incompatible error handling. One possible, and rather straightforward, solution is to turn synchronous throws into failed futures that can be easily used in an async pipeline.

# Mongo & Swift

  • I gave the official Mongo driver for Swift a try and I am quite pleased with it. On the API-front, I have some quibbles with how it handles decoding errors for Decodable types, but I can live with those for now.
  • I have mixed feelings about Mongo itself, but I appreciate not having to worry about schemas and migrations for every server app I am deploying. For certain micro-projects, the benefits of NoSQL far outweigh the drawbacks.
Have feedback or questions? Drop me a line via the contact form, Twitter, or Mastodon.
Found any typos? Edit the post here.