Giter Site home page Giter Site logo

Proposal: errors.Pop() about errors HOT 5 OPEN

pkg avatar pkg commented on August 26, 2024
Proposal: errors.Pop()

from errors.

Comments (5)

davecheney avatar davecheney commented on August 26, 2024 4

You can write errors.Pop yourself

if err, ok := err.(interface{Cause() error}); ok {
      cause := err.Cause()
      ///
}

from errors.

davecheney avatar davecheney commented on August 26, 2024

Thank you for this suggestion. I understand what you want to implement, but I do not understand why. Can you show me how a caller of Pop would use the result?

from errors.

tux-eithel avatar tux-eithel commented on August 26, 2024

Hi!
I'm also agree with @bobbytables.
In my case a Pop() function is useful to isolate my library errors form others library.
Sometimes I wrap errors inside my function (which returns error), other times no.

var (
    ErrNetwork = errors.New("network erorr")
    ErrDB      = errors.New("database erorr")
)

func main() {

    err := funcWithError()

    switch errors.Pop(err) {
    case ErrNetwork:
        // do stuff
    case ErrDB:
        // do stuff
    default:
        // do other stuff
    }

...
}

from errors.

davecheney avatar davecheney commented on August 26, 2024

@tux-eithel thanks for your comment. I don't think your sample code works because the errors returned from this package cannot be compared for equality. This is by design.

from errors.

cris-hart avatar cris-hart commented on August 26, 2024

@davecheney My example it too much simplified. The Pop() may returns an errors and the switch could be done on after call Error() function.

var (
    ErrNetwork = errors.New("network erorr")
    ErrDB      = errors.New("database erorr")
)

func main() {

    err := funcWithError()

    pop := errors.Pop(err)

    switch pop.Error() {
    case ErrNetwork:
        // do stuff
    case ErrDB:
        // do stuff
    default:
        // some error from other libraries
    }

...
}

from errors.

Related Issues (20)

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.