gopl-zh.github.com/ch7/ch7-11.md

80 lines
3.5 KiB
Markdown
Raw Permalink Normal View History

2016-02-15 03:06:34 +00:00
## 7.11. 基于类型断言区别错误类型
2016-01-21 02:22:10 +00:00
思考在os包中文件操作返回的错误集合。I/O可以因为任何数量的原因失败但是有三种经常的错误必须进行不同的处理文件已经存在对于创建操作找不到文件对于读取操作和权限拒绝。os包中提供了三个帮助函数来对给定的错误值表示的失败进行分类
2016-01-21 02:22:10 +00:00
2016-01-18 12:31:15 +00:00
```go
package os
2015-12-09 07:45:11 +00:00
2016-01-18 12:31:15 +00:00
func IsExist(err error) bool
func IsNotExist(err error) bool
func IsPermission(err error) bool
```
2016-01-21 02:22:10 +00:00
2016-02-15 03:06:34 +00:00
对这些判断的一个缺乏经验的实现可能会去检查错误消息是否包含了特定的子字符串,
2016-01-21 02:22:10 +00:00
2016-01-18 12:31:15 +00:00
```go
func IsNotExist(err error) bool {
2016-01-21 02:22:10 +00:00
// NOTE: not robust!
return strings.Contains(err.Error(), "file does not exist")
2016-01-18 12:31:15 +00:00
}
```
2016-01-21 02:22:10 +00:00
但是处理I/O错误的逻辑可能一个和另一个平台非常的不同所以这种方案并不健壮并且对相同的失败可能会报出各种不同的错误消息。在测试的过程中通过检查错误消息的子字符串来保证特定的函数以期望的方式失败是非常有用的但对于线上的代码是不够的。
2016-01-18 12:31:15 +00:00
一个更可靠的方式是使用一个专门的类型来描述结构化的错误。os包中定义了一个PathError类型来描述在文件路径操作中涉及到的失败像Open或者Delete操作并且定义了一个叫LinkError的变体来描述涉及到两个文件路径的操作像Symlink和Rename。这下面是os.PathError
2016-01-21 02:22:10 +00:00
2016-01-18 12:31:15 +00:00
```go
package os
// PathError records an error and the operation and file path that caused it.
type PathError struct {
2016-01-21 02:22:10 +00:00
Op string
Path string
Err error
2016-01-18 12:31:15 +00:00
}
func (e *PathError) Error() string {
2016-01-21 02:22:10 +00:00
return e.Op + " " + e.Path + ": " + e.Err.Error()
2016-01-18 12:31:15 +00:00
}
```
2016-01-21 02:22:10 +00:00
大多数调用方都不知道PathError并且通过调用错误本身的Error方法来统一处理所有的错误。尽管PathError的Error方法简单地把这些字段连接起来生成错误消息PathError的结构保护了内部的错误组件。调用方需要使用类型断言来检测错误的具体类型以便将一种失败和另一种区分开具体的类型可以比字符串提供更多的细节。
2016-01-21 02:22:10 +00:00
2016-01-18 12:31:15 +00:00
```go
_, err := os.Open("/no/such/file")
fmt.Println(err) // "open /no/such/file: No such file or directory"
fmt.Printf("%#v\n", err)
// Output:
// &os.PathError{Op:"open", Path:"/no/such/file", Err:0x2}
```
2016-01-21 02:22:10 +00:00
2018-06-09 16:27:25 +00:00
这就是三个帮助函数是怎么工作的。例如下面展示的IsNotExist它会报出是否一个错误和syscall.ENOENT§7.8或者和有名的错误os.ErrNotExist相等可以在§5.4.2中找到io.EOF或者是一个`*PathError`它内部的错误是syscall.ENOENT和os.ErrNotExist其中之一。
2016-01-21 02:22:10 +00:00
2016-01-18 12:31:15 +00:00
```go
import (
2016-01-21 02:22:10 +00:00
"errors"
"syscall"
2016-01-18 12:31:15 +00:00
)
var ErrNotExist = errors.New("file does not exist")
// IsNotExist returns a boolean indicating whether the error is known to
// report that a file or directory does not exist. It is satisfied by
// ErrNotExist as well as some syscall errors.
func IsNotExist(err error) bool {
2016-01-21 02:22:10 +00:00
if pe, ok := err.(*PathError); ok {
err = pe.Err
}
return err == syscall.ENOENT || err == ErrNotExist
2016-01-18 12:31:15 +00:00
}
```
2016-01-21 02:22:10 +00:00
2016-02-15 03:06:34 +00:00
下面这里是它的实际使用:
2016-01-21 02:22:10 +00:00
2016-01-18 12:31:15 +00:00
```go
_, err := os.Open("/no/such/file")
fmt.Println(os.IsNotExist(err)) // "true"
```
2016-01-21 02:22:10 +00:00
2016-02-15 03:06:34 +00:00
如果错误消息结合成一个更大的字符串当然PathError的结构就不再为人所知例如通过一个对fmt.Errorf函数的调用。区别错误通常必须在失败操作后错误传回调用者前进行。