Giter VIP home page Giter VIP logo

gerr's Introduction

Gerr

An error handling library that makes code simpler
catches errors explicitly
Goodbye err != nil

Install

go get -u github.com/huangjc7/gerr@latest

Use

  1. If your program is executed once and then terminated, use the following method:
func main() {
	// When shouldWait is true, waitGroup will be used to facilitate the scenario where the goroutine has not completed execution when the function exits.
	// When shouldWait is false, waitGroup will not be used to continue receiving errors from the error channel.
	// Note: When shouldWait is false, there is no need to call the Close method

	g := gerr.New(true)

	// Start goroutine to receive error messages
	g.Receive()

	//Register an error callback function to perform business logic processing when catching errors.
	fmtProcessFunc := func(err error) {
		fmt.Println("Error", err)
		//Business processing logic
	}

	logProcessFunc := func(err error) {
		log.Printf("Error:[%s]\n", err)
		return
	}

	//New test error
	err1 := errors.New("this is an error 1")
	err2 := errors.New("this is an error 2")

	// Use LogProcessFunc to handle errors
	g.CatchErr(fmtProcessFunc, err1)
	g.CatchErr(logProcessFunc, err2)

	g.Close()

}
  1. If your program runs continuously, use the following method:
func main() {
	// When shouldWait is true, waitGroup will be used to facilitate the scenario where the goroutine has not completed execution when the function exits.
	// When shouldWait is false, waitGroup will not be used to continue receiving errors from the error channel.
	// Note: When shouldWait is false, there is no need to call the Close method

	g := gerr.New(false)

	// Start goroutine to receive error messages
	g.Receive()

	//Register an error callback function to perform business logic processing when catching errors.
	fmtProcessFunc := func(err error) {
		fmt.Println("Error", err)
		//Business processing logic
	}

	logProcessFunc := func(err error) {
		log.Printf("Error:[%s]\n", err)
		return
	}

	//New test error
	err1 := errors.New("this is an error 1")
	err2 := errors.New("this is an error 2")

	for {
		// Use LogProcessFunc to handle errors
		g.CatchErr(fmtProcessFunc, err1)
		g.CatchErr(logProcessFunc, err2)
	}
}
  1. Recommended example
    Very simple to use, no if != nil in the code
// BusinessLogic struct contains business logic
type BusinessLogic struct {
    errorHandler *gerr.Error
}

// Callback function for error handling logic
func (b *BusinessLogic) FmtProcessFunc(err error) {
    fmt.Println("Error in business logic:", err)
}

// Callback function for error handling logic
func (b *BusinessLogic) LogProcessFunc(err error) {
    log.Printf("Error in business logic: [%s]\n", err)
}

// NewBusinessLogic creates a BusinessLogic instance
func NewBusinessLogic(errorHandler *gerr.Error) *BusinessLogic {
    return &BusinessLogic{
        errorHandler: errorHandler,
    }
}

// PerformOperation performs an operation, which may produce an error
func (b *BusinessLogic) PerformOperation() {
    // Here we simulate some operations that may produce errors
    err := errors.New("operation error")
    // Catch errors and if there are any, use the specified callback function for handling
    b.errorHandler.CatchErr(b.LogProcessFunc, err)
    b.errorHandler.CatchErr(b.FmtProcessFunc, err)
}

func main() {
    g := gerr.New(false)

    // Start a goroutine to receive error messages
    g.Receive()

    // Create a BusinessLogic instance
    bl := NewBusinessLogic(g)

    // Simulate the execution of business logic
    for {
        bl.PerformOperation()
    }
}

gerr's People

Contributors

huangjc7 avatar

Stargazers

 avatar shanchui avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.