Skip to content

Instantly share code, notes, and snippets.

@MAAARKIN
Last active April 5, 2020 00:23
Show Gist options
  • Save MAAARKIN/44d8c8541fb9ecb8c02d24727d7a943a to your computer and use it in GitHub Desktop.
Save MAAARKIN/44d8c8541fb9ecb8c02d24727d7a943a to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"github.com/maaarkin/errors-handling/internal/service"
"github.com/pkg/errors"
)
type stackTracer interface {
StackTrace() errors.StackTrace
}
func main() {
todoService := service.NewTodoService()
todo, err := todoService.Save("")
if err != nil { //existe error a ser tratado
if st, ok := err.(stackTracer); ok {
log.Fatalf("%+v", st.StackTrace()[0:2])
} else {
//condicional para garantir que caso o error identificado
//não tenha sido corretamente tratado com o pkg/errors
log.Fatal(err) //utilizando o fatal para interromper a execução do main.
}
}
fmt.Println(todo.Title)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment