pkg / errors

Simple error handling primitives
https://godoc.org/github.com/pkg/errors
BSD 2-Clause "Simplified" License
8.18k stars 691 forks source link

undefined errors.Wrap #225

Closed bygui86 closed 4 years ago

bygui86 commented 4 years ago

I was looking into new errors package functionalities and how to handle errors on defer, but I'm not able to build my very simple test.

Here the code:

package main

import (
    "errors"
    "fmt"
    "os"
)

func main() {
    goodErr := good()
    if goodErr != nil {
        fmt.Println("good:", goodErr.Error())
    }

    betterErr := better()
    if betterErr != nil {
        fmt.Println("better:", betterErr.Error())
    }

    bestErr := best()
    if bestErr != nil {
        fmt.Println("best:", bestErr.Error())
    }
}

func good() error {
    f, err := os.Open("./defer-error/book.txt")
    if err != nil {
        return err
    }
    defer func() {
        err := f.Close()
        if err != nil {
            // do something like log
        }
    }()

    // some other code

    return nil
}

func better() (err error) {
    f, err := os.Open("./defer-error/book.txt")
    if err != nil {
        return err
    }
    defer func() {
        ferr := f.Close()
        if ferr != nil {
            err = ferr
        }
    }()

    // some other code

    return nil
}

func best() (err error) {
    f, err := os.Open("./defer-error/book.txt")
    if err != nil {
        return err
    }
    defer func() {
        ferr := f.Close()
        if ferr != nil {
            err = errors.Wrap(err, ferr.Error())
        }
    }()

    // some other code

    return nil
}

Here the error:

zsh$ go build ./...
# github.com/bygui86/go-defer-panic-recover/defer-error
defer-error/main.go:63:10: undefined: errors.Wrap

I really don't understand why it fails. Even using JetBrains Goland, it encounters the very same error. What am I doing wrong?

Go version: 1.14 OS: macOS Mojave 10.14.6 (18G3020) Shell: zsh

davecheney commented 4 years ago

Please check your imports, you’ve imported the standard library errors package, not this project.

On 16 Mar 2020, at 04:55, Matteo Baiguini notifications@github.com wrote:

 I was looking into new errors package functionalities and how to handle errors on defer, but I'm not able to build my very simple test.

Here the code:

package main

import ( "errors" "fmt" "os" )

func main() { goodErr := good() if goodErr != nil { fmt.Println("good:", goodErr.Error()) }

betterErr := better() if betterErr != nil { fmt.Println("better:", betterErr.Error()) }

bestErr := best() if bestErr != nil { fmt.Println("best:", bestErr.Error()) } }

func good() error { f, err := os.Open("./defer-error/book.txt") if err != nil { return err } defer func() { err := f.Close() if err != nil { // do something like log } }()

// some other code

return nil }

func better() (err error) { f, err := os.Open("./defer-error/book.txt") if err != nil { return err } defer func() { ferr := f.Close() if ferr != nil { err = ferr } }()

// some other code

return nil }

func best() (err error) { f, err := os.Open("./defer-error/book.txt") if err != nil { return err } defer func() { ferr := f.Close() if ferr != nil { err = errors.Wrap(err, ferr.Error()) } }()

// some other code

return nil } Here the error:

zsh$ go build ./...

github.com/bygui86/go-defer-panic-recover/defer-error

defer-error/main.go:63:10: undefined: errors.Wrap I really don't understand why it fails. Even using JetBrains Goland, it encounters the very same error.

Go version: 1.14 OS: macOS Mojave 10.14.6 (18G3020) Shell: zsh

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

bygui86 commented 4 years ago

Oh I'm really sorry! I changed it many times, but every time Goland saves it changes the import back to standard one... Thanks a lot!