Generic Error Handling in SwiftUI
Book

Exploring MusicKit and Apple Music API

Unlock the full power of MusicKit & Apple Music APIs in your apps with the best guide! Use code musickit-blog for a limited-time 35% discount!

Error handling is something that I usually put out to fix at last and print the error in the catch statement. As I grow as a developer, I cannot do this anymore in my apps, and I try to incorporate the process of error handling while writing the methods and writing tests for them.

One requirement I recently faced was generalizing error handling so that I can handle the error-related code in one place and directly use it everywhere else.

I started with three ideas in mind:

  • A error protocol out of which I can create different error structures
  • A base view model that takes care of handling the error.
  • A view modifier responsible for the error alert shown to the user.

So let’s explore some error handling!

Error Protocol

Conclusion

These views are more of me being lazy, but I like them when working on a project with hundreds of them.

If you have a better approach, please tag @rudrankriyam on Twitter! I love constructive feedback and appreciate constructive criticism.

Thanks for reading, and I hope you’re enjoying it!

Book

Exploring MusicKit and Apple Music API

Unlock the full power of MusicKit & Apple Music APIs in your apps with the best guide! Use code musickit-blog for a limited-time 35% discount!

Written by

Rudrank Riyam

Hi, my name is Rudrank. I create apps for Apple Platforms while listening to music all day and night. Author of "Exploring MusicKit". Apple WWDC 2019 scholarship winner.