https://github.com/nzrsky/diekit
💀 Comprehensive error printer for Swift
https://github.com/nzrsky/diekit
apple errno error ios macos nserror swift swiftui tvos visionos watchos
Last synced: about 1 year ago
JSON representation
💀 Comprehensive error printer for Swift
- Host: GitHub
- URL: https://github.com/nzrsky/diekit
- Owner: nzrsky
- License: mit
- Created: 2023-10-08T10:19:01.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-24T02:34:50.000Z (almost 2 years ago)
- Last Synced: 2025-01-16T13:22:28.384Z (over 1 year ago)
- Topics: apple, errno, error, ios, macos, nserror, swift, swiftui, tvos, visionos, watchos
- Language: Swift
- Homepage: https://nzrsky.github.io/DieKit/
- Size: 95.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: CODEOWNERS
- Security: SECURITY.md
Awesome Lists containing this project
README
# 💀 Error Printer for Swift
[](https://github.com/nzrsky/DieKit/actions/workflows/build-test.yml?query=branch%3Amain+)
[](https://codecov.io/gh/nzrsky/DieKit)
[]()
[]()
[]()
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:
```swift
dependencies: [
.package(url: "https://github.com/nzrsky/DieKit.git", from: "0.1.12")
]
```
## Usage
Print ANSI color (if supported) errors to stderr:
```swift
print(error: "message")
// or
print(error: CustomError())
```
Fatal error with proper message as a fallback:
```swift
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("") }()
// ✅ 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:
```swift
// 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:
```swift
throw NSError(posixError: EFAULT)
// or
some_system_code_which_writes_errno()
let error = NSError.posixLatest /* error from errno with proper description */
```
Print formatted errors:
```swift
// 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](http://twitter.com/nzrsky). If you find an issue,
just [open a ticket](https://github.com/nzrsky/DieKit/issues/new) 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](https://github.com/sponsors/nzrsky). :heart:
## 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.