From ca8805a6bc6c2ce8276f26799e36c784c1a53eb2 Mon Sep 17 00:00:00 2001 From: RaviAnand Mohabir Date: Thu, 26 Oct 2023 23:59:17 +0200 Subject: [PATCH] feat: :sparkles: improve error handling --- auth/routes.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/auth/routes.go b/auth/routes.go index 4c8e8f5..23ec083 100644 --- a/auth/routes.go +++ b/auth/routes.go @@ -2,6 +2,7 @@ package auth import ( "context" + "fmt" "net/http" "gitea.ravianand.me/Dan6erbond/listy/core" @@ -23,6 +24,7 @@ func Redirect(app *core.App) func(w http.ResponseWriter, r *http.Request) { session.Values["state"] = state if err := session.Save(r, w); err != nil { + fmt.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -46,16 +48,30 @@ func Callback(app *core.App) func(w http.ResponseWriter, r *http.Request) { // use the same state string here that you used to generate the URL token, err := app.SpotifyAuth.Token(r.Context(), state.(string), r) + if err != nil { + fmt.Println(err) http.Error(w, "Couldn't get token", http.StatusNotFound) return } spotifyClient := spotify.New(app.SpotifyAuth.Client(r.Context(), token)) - user, _ := spotifyClient.CurrentUser(ctx) + user, err := spotifyClient.CurrentUser(ctx) + + if err != nil { + fmt.Println(err) + http.Error(w, "Couldn't get current user", http.StatusNotFound) + return + } - users.SaveUserToken(ctx, app, user.ID, token) + _, err = users.SaveUserToken(ctx, app, user.ID, token) + + if err != nil { + fmt.Println(err) + http.Error(w, "Couldn't save token", http.StatusNotFound) + return + } app.Render.JSON(w, 200, user) }