I am trying to create a custom error in Go such that I can create new instances of that error with a custom message, and then handle that specific type of error. However, when I try to do this, then my overridden errors.Is() is not being executed. Here is my code:
package main
import (
"fmt"
"errors"
)
type Error struct {
Message string
Code int
}
func (e *Error) Error() string {
return e.Message
}
func (e Error) Is(target Error) bool {
// This is never printed - this method never excutes for some reason
fmt.Println("compared!")
return e.Code == target.Code
}
var NotFoundError *Error = &Error{Code: 404, Message: "The page was not found"}
func NewError(errorType *Error, message string) error {
rc := *errorType
rc.Message = message
return &rc
}
func FetchImage() error {
return NewError(NotFoundError, "That image is gone")
}
func main() {
err := FetchImage()
// Returns false
fmt.Println(errors.Is(err, NotFoundError))
}
Run Code Online (Sandbox Code Playgroud)
In this case, the call to errors.Is() returns false. However, even though I have supplied my own Is() function, that function is not being called at all. That is, the string "compared!" is never printed.
Why isn't my Is() function working as desired?
type Error struct {
Message string
Code int
}
func (e *Error) Error() string {
return e.Message
}
func (e *Error) Is(tgt error) bool {
// This is never printed - this method never excutes for some reason
fmt.Println("compared!")
target, ok := tgt.(*Error)
if !ok{
return false
}
return e.Code == target.Code
}
Run Code Online (Sandbox Code Playgroud)
您的 Error 结构未Is正确实现方法,参数不应error为Error。
请参阅实际操作:
https://play.golang.org/p/vRQndE9ZRuH
| 归档时间: |
|
| 查看次数: |
6588 次 |
| 最近记录: |