错误的具体类型

huangapple go评论72阅读模式
英文:

Error concrete type

问题

我有一个方法CreateProduct(&Product) error,它返回一个实现了error接口的值。它可以是gorm数据库错误或者我自己定义的错误类型。

有了返回的值,我如何知道错误的类型是什么?

err = api.ProductManager.CreateProduct(product)
if err != nil {
    // TODO: 如何区分这是一个验证错误?
    response.WriteHeader(422)
    response.WriteJson(err)
    return
}
英文:

I have a method CreateProduct(&Product) error that returns a value implementing error interface. It can be a gorm database error or my own error type.

Having the returned value, how can I know which type is the error?

err = api.ProductManager.CreateProduct(product)
if err != nil {
	// TODO: how to distinguish that it is a validation error?
	response.WriteHeader(422)
	response.WriteJson(err)
	return
}

答案1

得分: 3

你可以进行类型断言,并在返回的错误是预期类型时执行相应的操作:

if nerr, ok := err.(yourError); ok {
  // 做一些操作
}

你还可以使用类型切换进行多个测试:

switch t := err.(type) {
case yourError:
    // t 是 yourError 类型
case otherError:
    // err 是 otherError 类型
case nil:
    // err 是 nil
default:
    // t 是其他类型
}

注意:即使在 nil(当 err == nil)上也可以进行类型断言:

断言的结果是一对具有类型 (T, bool) 的值。

  • 如果断言成立,表达式返回一对值 (x.(T), true)
  • 否则,表达式返回 (Z, false),其中 Z 是类型 T 的零值。

在这里,“Error”的“零值”将是 nil

英文:

You can do a type assertion, and act if the error returned is from the expected type:

if nerr, ok := err.(yourError); ok  {
  // do something
}

You can also do a type switch for multiple test

switch t := err.(type) {
case yourError:
    // t is a yourError
case otherError :
    // err is an otherError
case nil:
    // err was nil
default:
    // t is some other type 
}

Note: a type assertion is possible even on nil (when err == nil):

> the result of the assertion is a pair of values with types (T, bool).

> - If the assertion holds, the expression returns the pair (x.(T), true);

  • otherwise, the expression returns (Z, false) where Z is the zero value for type T

Here, the "zero value" of "Error" would be nil.

答案2

得分: 3

你可以使用type assertions来实现:

if gormError, ok := err.(gorm.RecordNotFound); ok {
   // 处理 RecordNotFound 错误
}

if myError, ok := err.(MyError); ok {
   // 处理 MyError
}

当处理多个错误情况时,使用type switches可能会很有用:

switch actualError := err.(type) {
case gorm.RecordNotFound:
    // 处理 RecordNotFound
case MyError:
    // 处理 MyError
case nil:
    // 没有错误
}
英文:

You can use type assertions to do that:

if gormError, ok := err.(gorm.RecordNotFound); ok {
   // handle RecordNotFound error
}

if myError, ok := err.(MyError); ok {
   // handle MyError
}

When dealing with multiple error cases, it can be useful to use type switches for that:

switch actualError := err.(type) {
case gorm.RecordNotFound:
    // handle RecordNotFound
case MyError:
    // handle MyError
case nil:
    // no error
}

答案3

得分: 0

你可以像处理其他接口一样处理它。使用类型断言或类型切换:

switch err := err.(type) {
case MyValidationError:
    fmt.Printf("validation error")
case MyOtherTypeOfError:
    fmt.Printf("my other type of error")
default:
    fmt.Printf("error of type %T", err)
}

Go Playground: http://play.golang.org/p/5-OQ3hxmZ5

英文:

The same way you would go about with any other interface. Use type assertion or a type switch:

switch err := err.(type) {
case MyValidationError:
    fmt.Printf("validation error")
case MyOtherTypeOfError:
    fmt.Printf("my other type of error")
default:
    fmt.Printf("error of type %T", err)
}

Go Playground: http://play.golang.org/p/5-OQ3hxmZ5

huangapple
  • 本文由 发表于 2014年7月6日 02:51:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/24589566.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定