blunder

command module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 14, 2023 License: MIT Imports: 4 Imported by: 0

README

Blunder

GitHub Go Report Card

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
     go get github.com/DenChenn/blunder
    
  2. Import into your project
    printf '// +build tools\npackage tools\nimport (_ "github.com/DenChenn/blunder")' | gofmt > tools.go
    
    go mod tidy
    
  3. Initialize Blunder
     go run github.com/DenChenn/blunder init <your_dir_path>
    
  4. Define your error in blunder.yaml according to example ❤️
  5. Generate all errors
     go run github.com/DenChenn/blunder gen
    
    or generate with gpt-based auto-completion
     export OPENAI_API_TOKEN=<your_openai_api_token>
     go run github.com/DenChenn/blunder gen --complete=true 
    

Usage

Suppose your errors in blunder.yaml are defined like this:

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:

<your_dir_path>/errors/
generated/
  alayer/ 
    errors.go
  blayer/
    errors.go
blunder.yaml

Which can be import into your code like this:

if err != nil {
  if errors.Is(err, &alayer.Err1) {
    //...
  }
}

Or you can wrap your error like this:

if errors.Is(err, &pgx.ErrNoRows) {
  return &alayer.Err1.Wrap(err) 
}

Type assertion

All generated errors implement blunder.OrdinaryError interface, which contains static methods.

ordinaryError, ok := err.(blunder.OrdinaryError)
if ok {
	fmt.Println(ordinaryError.GetMessage())
}

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
internal
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL