我想建立一个堆栈跟踪,其中包括一个低级别的数据库错误与第二个错误,这是人类可读的。
golang 1.13中的新errors.Unwrap()函数是为此目的而构建的吗?我不知道该怎么使用它。寻找一个如何做到这一点的例子。
// model/book.go
package model
type Book struct {
Id uint32 `json:"id" db:"id"`
Title string `json:"title" db:"title"`
Author string `json:"author" db:"author"`
Price float32 `json:"price" db:"price"`
}
func (b *Book) Tablename() string {
return "books"
}
// main.go
package main
func main() {
bk := model.Book{
Title: "oliver twist",
Author: "charles dickens",
Price: 10.99,
}
err:= Create(&bk)
if err !=nil {
// how to use Unwrap?
}
}
func Create(book *model.Book) error {
insertSQL := "INSERT INTO ...."
// code to insert
if err != nil {
return err
}
book.Id = uint32(lastID)
return nil
}发布于 2019-11-16 12:41:45
errors.Unwrap用于剥离已包装的错误层。
// error 1 wrapped by 2 wrapped by 3
err1 := errors.New("error 1")
err2 := fmt.Errorf("error 2: %w", err1)
err3 := fmt.Errorf("error 3: %w", err2)
fmt.Println(err1) // "error 1"
fmt.Println(err2) // "error 2: error 1"
fmt.Println(err3) // "error 3: error 2: error 1"
// unwrap peels a layer off
fmt.Println(errors.Unwrap(err3)) // "error 2: error 1"
fmt.Println(errors.Unwrap(errors.Unwrap(err3))) // "error 1"您可以递归地展开以获取最内部的错误:
currentErr := err3
for errors.Unwrap(currentErr) != nil {
currentErr = errors.Unwrap(currentErr)
}
fmt.Println(currentErr) // "error 1"对于同时包含低级错误和人类可读错误的错误,您可以实现自定义错误。
type Error struct {
LowLevel error
HumanReadable string
}
func (e Error) Error() string {
return e.HumanReadable
}
func (e Error) Unwrap() error {
return e.LowLevel
}
// helper
func NewError(inner error, outer string) *Error {
return &Error{
LowLevel: inner,
HumanReadable: outer,
}
}在您调用的函数中:
func Create(book *model.Book) error {
...
if err != nil {
return NewError(err, "failed to create book")
}
...
}在您的调用方中:
func main() {
...
err := Create(&bk)
if err != nil {
log.Print(errors.Unwrap(err)) // internally log low-level error
fmt.Print(err) // present human-readable error to user
}
...
}https://stackoverflow.com/questions/57945730
复制相似问题