Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/denchenn/blunder
A modern golang error handling package powered by GPT-3.
https://github.com/denchenn/blunder
codegen error-handling gin golang gpt-3 grpc template
Last synced: about 1 month ago
JSON representation
A modern golang error handling package powered by GPT-3.
- Host: GitHub
- URL: https://github.com/denchenn/blunder
- Owner: DenChenn
- License: mit
- Created: 2023-05-18T19:48:40.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-09-14T11:35:21.000Z (over 1 year ago)
- Last Synced: 2024-11-02T07:14:06.444Z (about 2 months ago)
- Topics: codegen, error-handling, gin, golang, gpt-3, grpc, template
- Language: Go
- Homepage:
- Size: 43.9 KB
- Stars: 15
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Blunder
![GitHub](https://img.shields.io/github/license/DenChenn/blunder)
![Go Report Card](https://goreportcard.com/badge/github.com/DenChenn/blunder)## What is Blunder?
Blunder is a simple, gpt-based and easy-to-use error handling package for golang.
It generate typed errors and manage them in centralized way, which reduce the dependency between packages and make error handling more convenient.## Getting Started
1. Install package
```bash
go get github.com/DenChenn/blunder
```
2. Import into your project
```bash
printf '// +build tools\npackage tools\nimport (_ "github.com/DenChenn/blunder")' | gofmt > tools.gogo mod tidy
```
3. Initialize Blunder
```bash
go run github.com/DenChenn/blunder init
```
3. Define your error in `blunder.yaml` according to example ❤️
4. Generate all errors
```bash
go run github.com/DenChenn/blunder gen
```
or generate with gpt-based auto-completion
```bash
export OPENAI_API_TOKEN=
go run github.com/DenChenn/blunder gen --complete=true
```## Usage
Suppose your errors in `blunder.yaml` are defined like this:
```yaml
details:
- package: alayer
errors:
- code: Err1
#...
- code: Err2
#...
- code: Err3
#...
- package: blayer
errors:
- code: Err1
#...
- code: Err2
#...
- code: Err3
#...
```Your error will be generated in given path like this:
```
/errors/
generated/
alayer/
errors.go
blayer/
errors.go
blunder.yaml
```Which can be import into your code like this:
```go
if err != nil {
if errors.Is(err, &alayer.Err1) {
//...
}
}
```Or you can wrap your error like this:
```go
if errors.Is(err, &pgx.ErrNoRows) {
return &alayer.Err1.Wrap(err)
}
```## Type assertion
All generated errors implement `blunder.OrdinaryError` interface, which contains static methods.```go
ordinaryError, ok := err.(blunder.OrdinaryError)
if ok {
fmt.Println(ordinaryError.GetMessage())
}
```