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
}
errors.Unwrap
is for peeling off layers of an error that has been wrapped.
// 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"
You can recursively unwrap to get to the inner most error:
currentErr := err3
for errors.Unwrap(currentErr) != nil {
currentErr = errors.Unwrap(currentErr)
}
fmt.Println(currentErr) // "error 1"
For an error that contains both low-level and human-readable error, you can implement a custom error.
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,
}
}
In your called function:
func Create(book *model.Book) error {
...
if err != nil {
return NewError(err, "failed to create book")
}
...
}
In your caller:
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
}
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With