Created
July 18, 2022 19:22
-
-
Save codelynx/2eded4d7380e572cc45ce4d3b84e5a57 to your computer and use it in GitHub Desktop.
General purpose to make a concrete Error instance in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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