Thanks to visit codestin.com
Credit goes to github.com

Skip to content

lufia/try

Repository files navigation

try

An experimental error handling library.

GoDev Actions Status

Supported Architectures

  • amd64
  • arm64

Usage

This is a simple example.

import (
	"net/url"
	"os"

	"github.com/lufia/try"
)

func Run(file string) (string, error) {
	cp, err := try.Handle()
	if err != nil {
		return "", err
	}
	s := try.Check1(os.ReadFile(file))(cp)
	u := try.Check1(url.Parse(string(s)))(cp)
	return u.Path, nil
}

try.Handle creates a fallback point, called "checkpoint", then return nil error at the first time.

After that, above code calls os.ReadFile and url.Parse with try.Check1. If either these functions returns an error, try.Check1 rewind the program to the checkpoint, then try.Handle will return the error.

I strongly recommend that Check and Handle should call on the same stack.

Example

Error handling in Go sometimes gets flustrated. For instance:

func GetAlerts(w http.ResponseWriter, r *http.Request) {
	if err := r.ParseForm(); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	orgID, err := strconv.Atoi(r.Form.Get("orgId"))
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	alerts, err := repository.FetchAlerts(orgID)
	if err != nil {
		http.Error(w, err.Error(), http.InternalServerError)
		return
	}
	body, err := json.Marshal(alerts)
	if err != nil {
		http.Error(w, err.Error(), http.InternalServerError)
		return
	}
	...
}

The example above can rewrite more simple with try.

func GetAlerts(w http.ResponseWriter, r *http.Request) {
	cp400, err := try.Handle()
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	cp500, err := try.Handle()
	if err != nil {
		http.Error(w, err.Error(), http.InternalServerError)
		return
	}

	try.Raise(r.ParseForm())(cp400)
	orgID := try.Check1(strconv.Atoi(r.Form.Get("orgId")))(cp400)
	alerts := try.Check1(repository.FetchAlerts(orgID))(cp500)
	body := try.Check1(json.Marshal(alerts))(cp500)
	...
}

About

An experimental error handling library.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •