try

package
v0.8.4 Latest Latest
Warning

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

Go to latest
Published: May 1, 2022 License: MIT Imports: 2 Imported by: 180

Documentation

Overview

Package try is a package for try.ToX functions that implement the error checking. try.ToX functions check 'if err != nil' and if it throws the err to the error handlers, which are implemented by the err2 package. More information about err2 and try packager roles can be seen in the FileCopy example:

...
r := try.To1(os.Open(src))
defer r.Close()

w := try.To1(os.Create(dst))
defer err2.Handle(&err, func() {
     os.Remove(dst)
})
defer w.Close()
try.To1(io.Copy(w, r))
return nil
...

All of the try package functions are as fast as the simple 'if err != nil {' statement, thanks to the compiler inlining and optimization.

Note that try.ToX function names end to a number (x) because:

"No variadic type parameters. There is no support for variadic type parameters,
which would permit writing a single generic function that takes different
numbers of both type parameters and regular parameters." - Go Generics

The leading number at the end of the To2 tells that To2 takes two different non-error arguments, and the third one must be an error value.

Looking at the FileCopy example again, you see that all the functions are directed to try.To1 are returning (type1, error) tuples. All of these tuples are the correct input to try.To1. However, if you have a function that returns (type1, type2, error), you must use try.To2 function to check the error. Currently the try.To3 takes (3 + 1) return values which is the greatest amount. If more is needed, let us know.

Example (CopyFile)
package main

import (
	"fmt"
	"io"
	"os"

	"github.com/lainio/err2"
	"github.com/lainio/err2/try"
)

func main() {
	copyFile := func(src, dst string) (err error) {
		defer err2.Returnf(&err, "copy %s %s", src, dst)

		// These try package helpers are as fast as Check() calls which is as
		// fast as `if err != nil {}`

		r := try.To1(os.Open(src))
		defer r.Close()

		w := try.To1(os.Create(dst))
		defer err2.Handle(&err, func() {
			os.Remove(dst)
		})
		defer w.Close()
		try.To1(io.Copy(w, r))
		return nil
	}

	err := copyFile("/notfound/path/file.go", "/notfound/path/file.bak")
	if err != nil {
		fmt.Println(err)
	}
}
Output:

copy /notfound/path/file.go /notfound/path/file.bak: open /notfound/path/file.go: no such file or directory

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Is

func Is(err, filter error) bool

Is-function performs a filtered error check for the given argument. It's the same as To-function, but it checks if the error matches the filter before throwing an error. The false return value tells that there are no errors and the true value that the error is the filter.

Example (ErrorHappens)
package main

import (
	"fmt"
	"io"

	"github.com/lainio/err2"
	"github.com/lainio/err2/try"
)

var errForTesting = fmt.Errorf("error for %s", "testing")

func main() {
	copyStream := func(src string) (s string, err error) {
		defer err2.Returnf(&err, "copy stream %s", src)

		err = errForTesting
		try.Is(err, io.EOF)
		return src, nil
	}

	str, err := copyStream("testing string")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(str)
}
Output:

copy stream testing string: error for testing
Example (ErrorHappensNot)
package main

import (
	"fmt"

	"github.com/lainio/err2"
	"github.com/lainio/err2/try"
)

var errForTesting = fmt.Errorf("error for %s", "testing")

func main() {
	copyStream := func(src string) (s string, err error) {
		defer err2.Returnf(&err, "copy stream %s", src)

		err = fmt.Errorf("something: %w", errForTesting)
		if try.Is(err, errForTesting) {
			return "wrapping works", nil
		}

		return src, nil
	}

	str, err := copyStream("testing string")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(str)
}
Output:

wrapping works

func IsEOF

func IsEOF(err error) bool

IsEOF-function performs a filtered error check for the given argument. It's the same as To-function, but it checks if the error matches the 'io.EOF' before throwing an error. The false return value tells that there are no errors and the true value that the error is the 'io.EOF'.

func IsEOF1

func IsEOF1[T any](v T, err error) (bool, T)

IsEOF1-function performs a filtered error check for the given argument. It's the same as To-function, but it checks if the error matches the 'io.EOF' before throwing an error. The false return value tells that there are no errors and the true value that the error is the 'io.EOF'.

Example
package main

import (
	"bytes"
	"fmt"

	"github.com/lainio/err2"
	"github.com/lainio/err2/try"
)

func main() {
	copyStream := func(src string) (s string, err error) {
		defer err2.Returnf(&err, "copy stream %s", src)

		in := bytes.NewBufferString(src)
		tmp := make([]byte, 4)
		var out bytes.Buffer
		for eof, n := try.IsEOF1(in.Read(tmp)); !eof; eof, n = try.IsEOF1(in.Read(tmp)) {
			out.Write(tmp[:n])
		}

		return out.String(), nil
	}

	str, err := copyStream("testing string")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(str)
}
Output:

testing string

func IsEOF2

func IsEOF2[T, U any](v1 T, v2 U, err error) (bool, T, U)

IsEOF2-function performs a filtered error check for the given argument. It's the same as To-function, but it checks if the error matches the 'io.EOF' before throwing an error. The false return value tells that there are no errors and the true value that the error is the 'io.EOF'.

func To

func To(err error)

To is a helper function to call functions which returns (error) and check the error value. If an error occurs, it panics the error where err2 handlers can catch it if needed.

func To1

func To1[T any](v T, err error) T

To1 is a helper function to call functions which returns (any, error) and check the error value. If an error occurs, it panics the error where err2 handlers can catch it if needed.

func To2

func To2[T, U any](v1 T, v2 U, err error) (T, U)

To2 is a helper function to call functions which returns (any, any, error) and check the error value. If an error occurs, it panics the error where err2 handlers can catch it if needed.

func To3

func To3[T, U, V any](v1 T, v2 U, v3 V, err error) (T, U, V)

To3 is a helper function to call functions which returns (any, any, any, error) and check the error value. If an error occurs, it panics the error where err2 handlers can catch it if needed.

Types

This section is empty.

Jump to

Keyboard shortcuts

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