Skip to content
/ gee Public

GEE (Go Error Expander) saves you the effort of error handling in Go

License

Notifications You must be signed in to change notification settings

snprajwal/gee

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GEE (Go Error Expander)

Go is stupid simple. Unfortunately, some parts of it are plain stupid. One of them is error handling.

The gee CLI tool simply expands placeholders into error handling, saving the programmer from typing:

...
if err != nil {
    return err
}
...

over and over again, countless times across the codebase.

Getting started

  • Add import fmt in the file if errors need to be wrapped with custom messages
  • Add a var _ error declaration at the top of the function
  • Use _ in place of an error
  • Add a //gee: comment above the line with the error. If a message is present after the colon, GEE wraps the error with that message and returns it. If not, it directly returns the error.

GEE will automatically replace the placeholder _s and inject error handling.

Consider a sample input.go file:

package main

import "fmt"

func foo() error {
    return nil
}

func main() {
    var _ error
    //gee:
    _ = foo()
    //gee:failed to run foo
    _ = foo()
}

Upon running gee input.go, GEE will expand this into:

package main

import "fmt"

func foo() error {
    return nil
}

func main() {
    var err error
    err = foo()
    if err != nil {
        return err
    }
    err = foo()
    if err != nil {
        return fmt.Errorf("failed to run foo: %w", err)
    }
}

Installation

The easiest way is via go install:

go install github.com/snprajwal/gee

Usage

# Providing a directory as the argument runs the CLI on all `.go` files in the directory
gee cli/
# Providing a single `.go` file runs the CLI on just that file
gee main.go

By default, GEE prints the expanded Go code to stdout. To modify the original .go files, run the CLI with the -i or --in-place flag.

# Providing the `-i` or `--in-place` flag modifies the file in-place
gee -i main.go

License

This project is licensed under the MIT License, which is available here.

About

GEE (Go Error Expander) saves you the effort of error handling in Go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published