Skip to content

Instantly share code, notes, and snippets.

@codelynx
Created July 18, 2022 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codelynx/2eded4d7380e572cc45ce4d3b84e5a57 to your computer and use it in GitHub Desktop.
Save codelynx/2eded4d7380e572cc45ce4d3b84e5a57 to your computer and use it in GitHub Desktop.
General purpose to make a concrete Error instance in Swift
//
// ZError.swift
// ZKit
//
// Created by Kaz Yoshikawa on 7/18/22.
//
// Usage:
// General usage for throwing Error in Swift language.
//
// Example:
// guard let number = number else { throw ZError("number should not be nil.") }
import Foundation
struct ZError: Error, CustomStringConvertible {
let message: String
let fileID: String
let function: String
let line: Int
init(_ message: String, fileID: String = #fileID, function: String = #function, line: Int = #line) {
self.message = message
self.fileID = fileID
self.function = function
self.line = line
}
var description: String {
return "\(message): \(fileID), \(function)(\(line)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment