2

I created a custom error type to wrap errors around for easier debugging in Golang. It works when there are errors to print, but now it is causing a panic.

Demo

type Error struct {
    ErrString string
}

func (e *Error) Error() string {
    return e.ErrString
}

func Wrap(err error, str string) *Error {
    if err == nil {
        return nil
    }
    e := &Error{
        ErrString: str + err.Error(),
    }
    return e
}

When I call a function an it doesn't return an error, I should still be able to wrap the error.

The expected behavior is that if the error is nil, it should simply ignore it, unfortunately it does the opposite.

func foo() error {
    err := bar()
    return Wrap(err, "bar called")
}

func bar() error {
    return nil
}

func main() {
    err := foo()
    if err != nil {
        fmt.Printf("Found error %v\n",err)
        return
    }
    fmt.Println("No Errors")
}

I expect it to print No errors. Instead it prints Found error <nil> even though the error is nil.

Ibu
  • 42,752
  • 13
  • 76
  • 103

4 Answers4

6
if err != nil

Is comparing a the err variable to a nil error , but its actually a nil *Error

Changing the code to

err:=foo()
var  nilerror *Error = nil
if err != nilerror {
    fmt.Printf("Found error %v\n",err)
    return
}
fmt.Println("No Errors")

Yields the predicted result.

pigeonhands
  • 3,066
  • 15
  • 26
  • 1
    Now it makes sense. my Wrap function was returning a `*Error` nil, not an `error` nil. – Ibu Dec 22 '18 at 03:31
  • 1
    is it possible to change the custom error so that it will work with (if err != nil), instead of having to check against a declared nil pointer? – mehlichmeyer Sep 11 '20 at 10:40
2

type error

The error built-in interface type is the conventional interface for representing an error condition, with the nil value representing no error.

type error interface {
    Error() string 
}

The value of err of interface type error is not nil. It is the value nil of type *main.Error. In fact, err != nil && err.(*Error) == nil is true


For example,

package main

import (
    "fmt"
)

func error1() {
    err := foo()
    fmt.Printf("%T %v %v %v\n", err, err, err == nil, err.(*Error) == nil)
    if err != nil {
        fmt.Printf("Found error %v\n", err)
        return
    }
    fmt.Println("No Errors")
}

func error2() {
    err := foo()
    fmt.Printf("%T %v %v %v\n", err, err, err == nil, err.(*Error) == nil)
    if err != nil && err.(*Error) != nil {
        fmt.Printf("Found error %v\n", err)
        return
    }
    fmt.Println("No Errors")
}

type Error struct {
    ErrString string
}

func (e *Error) Error() string {
    return e.ErrString
}

func Wrap(err error, str string) *Error {
    if err == nil {
        return nil
    }
    e := &Error{
        ErrString: str + err.Error(),
    }
    return e
}

func foo() error {
    err := bar()
    return Wrap(err, "bar called")
}

func bar() error {
    return nil
}

func main() {
    error1()
    fmt.Println()
    error2()
}

Playground: https://play.golang.org/p/nwNRa2sNwj0

Output:

*main.Error <nil> false true
Found error <nil>

*main.Error <nil> false true
No Errors
peterSO
  • 158,998
  • 31
  • 281
  • 276
  • 1
    Then how could I rewrite the Wrap function to take the type into consideration? – Ibu Dec 22 '18 at 03:22
  • 1
    It took a while but now I understand. Basically for my case, Wrap will return error not my custom `Error`. – Ibu Dec 22 '18 at 03:28
  • 2
    @Ibu: No. `foo`, not `Wrap`, will return type `error`, not your custom type `*Error`. `error` is an interface type. – peterSO Dec 22 '18 at 04:09
2

Since your Error type implements the error interface, the easiest solution, is to return an error in Wrap():

func Wrap(err error, str string) error {
    if err == nil {
        return nil
    }
    e := &Error{
        ErrString: str + err.Error(),
    }
    return e
}
Joris
  • 862
  • 1
  • 8
  • 17
0

Change foo() return value to *Error

func foo() error {
    err := bar()
    return Wrap(err, "bar called")
}

Or:

func Wrap(err error, str string) error{
    if err == nil {
        return nil
    }
    e := &Error{
        ErrString: str + err.Error(),
    }
    return e
}
xblymmx
  • 39
  • 2
  • 3