fix

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2024 License: MIT Imports: 1 Imported by: 0

README

Go Fix Error Hook

A new way of error handling.

Before throwing an error, you can first attempt to fix it, and let any other part of the program try and do something about it. This means if an error is caused by something externally, a module (for example) could automatically fix the error.

Installation

go get github.com/tkdeng/go-fix-error-hook

Usage


// in this example, we will use an "out of memory" error
var Error_OOM error = errors.New("out of memory")

// example for: fix.Try
func decompress(str string) (string, error) {
  // do stuff...
  str, err := "some large str...", Error_OOM

  // call before checking for nil error
  fix.Try(&err, func() error {
    //note: this method will first run through the fix.Hook list,
    // and clear the cache (as shown in the `fix.Hook` example).

    // retry stuff...
    str, err = "some large string", nil

    if err != nil {
      return err // fix failed (try next hook if same error)
    }
    return nil // nil = fixed error
  })

  if err != nil {
    return "", err
  }
  return str, nil
}

// example for: fix.Hook
var Cache = map[string]string{}

func init(){
  fix.Hook(Error_OOM, func() bool {
    // this method will be called, when fix.Try is called.
    // note: if a previous hook in the list fixes the problem first,
    // and the error has changed after running the retry callback (in fix.Try),
    // this method will be skipped.

    if _, ok := Cache["old data"]; !ok { // if "old data" not in cache, return false
      return false // fix changed nothing (skip to next hook)
    }

    delete(Cache, "old data") // remove "old data" from cache
    return true // fix did something (run the retry callback in fix.Try)
  })
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Hook

func Hook(err error, cb func(err error) bool)

Hook a callback to be called as a potential fix to an error.

@cb: return true if the fix was successful, return false, if it failed.

func HookAny added in v1.2.0

func HookAny(err error, cb func(err error) bool)

HookAny is like Hook, but will use the error.Is method.

@cb: return true if the fix was successful, return false, if it failed.

func Try

func Try(err *error, retry func(err error) error)

Try to fix an error if a hook can fix it.

@err will be updated with the new error, or with nil if fixed successfully.

note: if @err is already nil, this method will be ignored.

func TryOnce added in v1.0.3

func TryOnce(err *error, retry func(err error) error)

TryOnce will try to fix only one error.

If a different error comes up, this method will stop trying, instead of trying to fix the next error.

@err will be updated with the new error, or with nil if fixed successfully.

note: if @err is already nil, this method will be ignored.

Types

This section is empty.

Jump to

Keyboard shortcuts

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