No description
Find a file
Theta-Dev 34a01c6279
All checks were successful
continuous-integration/drone/push Build is passing
Pass through try-compatible errors
2021-10-17 10:59:33 +02:00
gotry_generate Improved code generator 2021-10-16 22:56:51 +02:00
try Pass through try-compatible errors 2021-10-17 10:59:33 +02:00
.drone.yml faster ci pipeline 2021-10-14 10:31:05 +02:00
.editorconfig Initial commit 2021-10-01 17:17:34 +02:00
.golangci.yaml Fix gofmt issue 2021-10-14 10:27:49 +02:00
.pre-commit-config.yaml Add pre-commit and drone CI 2021-10-14 08:07:33 +02:00
embed.go Setup golangci 2021-10-10 19:52:10 +02:00
go.mod Fixed module path 2021-10-01 17:26:22 +02:00
LICENSE Initial commit 2021-10-01 17:17:34 +02:00
README.md Add link to readme 2021-10-01 18:46:01 +02:00

gotry

This package is a fork of lainio's err2 package with an improved code generator.

Add custom try package to your project

go install code.thetadev.de/ThetaDev/gotry/gotry_generate

gotry_generate -init

Add type definitions

📂 try/types.csv

<Handler method name>;<Type>;<Import#1>;<Import#2>;...

# Example:
DB;*gorm.DB;gorm.io/gorm

# Add +s to the method name to generate 2 handlers for single values and slices
String+s;string

Generate

go generate ./try

Handle errors

Instead of the idiomatic go way of handling errors, which involves if-conditions for every possible error, ...

func myFunction() (err error) {
  b, err := ioutil.ReadAll(r)
  if err != nil {
    return err
  }
}

you can wrap the error-throwing function in a try wrapper.

func myFunction() (err error) {
  defer err2.Return(&err)

  b := try.Bytes(ioutil.ReadAll(r))

  return
}