Skip to content

Instantly share code, notes, and snippets.

Forked from nicklockwood/error-propagation.swift
Last active April 27, 2017 14:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Typed error use-case example
// I know you meant it to be exaggerated, but I still think this is the better approach in practice:
enum ClassError: Error {
case propertyNotFound(name: String)
case typeMismatch(propertyName: String)
class Foo {
var properties: [(String, Any)] = []
private func validateValue(_ value: Any, for property: String) throws { // Note that I pass the name just for the error; that's ok IMO
// ... some logic
throw ClassError.typeMismatch(propertyName: property)
private func validateProperty(name: String, value: Any) throws {
if true {
throw ClassError.propertyNotFound(name: name)
try validateValue(value, for: name) // Don't need to wrap
func validate(name: String) throws { // should only throw a ClassError
for (name, value) in {
try validateProperty(name: name, value: value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment