I would like to build a stack trace that includes a low level db error with a second error which is human readable.
Is the new errors.Unwrap() function in golang 1.13 built for this purpose? Not sure I understand how to use it. Looking for an example on how to do this.
// 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
}