How to create custom errors in Swift
Create an enum of type error and list the cases.
The following example can be found along with a simple app for demonstrating SwiftData through the tutorial/swift-data-basics branch on our Open Source Swift Starter Project.
git clone git@github.com:delasign/swift-starter-project.git
Step One: Create the Errors
Create a new swift file and within it, create an Enum, of type Error that includes all the errors that you wish to track.
We recommend that you do this within a folder called "errors" under a "models" folder.
Step Two: Use the Errors
In functions that produce errors, apply the error created in Step One as if you were using any other enum.
These errors can be particularly helpful as failed results within tasks.
Any Questions?
We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.