nzrsky / DieKit

💀 Comprehensive error printer for Swift

Home Page:https://nzrsky.github.io/DieKit/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

💀 Error Printer for Swift

CI codecov

Supports macOS, Ubuntu & Windows Swift 5.9 Latest release

DieKit, a Swift package designed to streamline and enhance error handling in your Swift applications. It makes exception handling more robust and insightful.

Key Features

  • Enhanced Error Reporting: It catches, prints, and rethrows errors for comprehensive debugging.
  • Wide Error Support: DieKit extends support to various Swift and Core Foundation error types, providing a versatile tool for a broad range of applications.
  • Works everythere, including Vapor 💧

Installation

Add DieKit to your Swift project by including it in your Package.swift dependencies:

dependencies: [
    .package(url: "https://github.com/nzrsky/DieKit.git", from: "0.1.12")
]

Usage

Print ANSI color (if supported) errors to stderr:

print(error: "message")
// or
print(error: CustomError())

Fatal error with proper message as a fallback:

import DieKit

// Not really informative sometimes
let x = env["SECRET_KEY"]!
// main.swift:28: Fatal error: Unexpectedly found nil while unwrapping an Optional value

// ❌ This code doesn't compile
let x = env["SECRET_KEY"] ?? fatalError()

// 🙈 Compiles but looks ugly
let x = env["SECRET_KEY"] ?? { fatalError("<Your message>") }()

// ✅ Almost perfect. You also can print stacktrace using `, trace: true`
let x = env["SECRET_KEY"] ?? die("Specify SECRET_KEY")
// Specify SECRET_KEY in env
// main.swift:28: Fatal error

Print error to stderr and proceed handling:

// Usage of printOnException
do {
    let result = try printOnException { try throwingOperation() }
    // prints error message to stderr according error's type
} catch error {
    // handle error
}

// Usage of dieOnException
let result = dieOnException { try throwingOperation() }
// prints error message to stderr and die

Posix errors:

throw NSError(posixError: EFAULT)

// or
some_system_code_which_writes_errno() 
let error = NSError.posixLatest /* error from errno with proper description */

Print formatted errors:

// Default behaviour
print(CGError.illegalArgument)
// The operation couldn’t be completed. (__C.CGError 1003)

// With DieKit
print(CGError.illegalArgument)
// error: The operation couldn’t be completed. (CGError: Illegal Argument)

Contact

Follow and contact me on Twitter. If you find an issue, just open a ticket on it. Pull requests are warmly welcome as well.

Backers & Sponsors

Open-source projects cannot live long without your help. If you find Kingfisher is useful, please consider supporting this project by becoming a sponsor. Your user icon or company logo shows up on my blog with a link to your home page.

Become a sponsor through GitHub Sponsors. ❤️

Contributing

Contributions make the open-source community an amazing place to learn, inspire, and create. Any contributions you make to DieKit are greatly appreciated.

License

Distributed under the MIT License. See LICENSE for more information.

About

💀 Comprehensive error printer for Swift

https://nzrsky.github.io/DieKit/

License:MIT License


Languages

Language:Swift 97.5%Language:Shell 2.2%Language:Ruby 0.2%Language:Makefile 0.1%