Last active
April 5, 2020 00:23
-
-
Save MAAARKIN/44d8c8541fb9ecb8c02d24727d7a943a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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