diff --git a/app/config.go b/app/config.go index 7087b7f..532d327 100644 --- a/app/config.go +++ b/app/config.go @@ -1,11 +1,14 @@ package app import ( + "context" + "errors" md "farga/app/middleware" "farga/app/templating" "farga/db/types" "farga/lib" hk "farga/lib/hackatime" + "fmt" "net/http" @@ -28,6 +31,7 @@ type DeleteSSHKeyForm struct { func (a *App) UserConfigHandler(w http.ResponseWriter, r *http.Request) { user := r.Context().Value(lib.UserKey).(*types.User) config, err := a.DB.GetUserConfig(user.ID) + query := r.URL.Query() var hkUser *hk.User if err != nil { @@ -35,10 +39,15 @@ func (a *App) UserConfigHandler(w http.ResponseWriter, r *http.Request) { return } - if user.HackatimeToken != "" { + if user.HackatimeToken != "" && query["err"][0] != "WarnOffline" { // TODO: use a cookie instead. hkUser, err = hk.GetUserInfo(user.HackatimeToken) - if err != nil { // TODO: check if offline, right now it ugly timeouts. - http.Error(w, err.Error(), 500) + if err != nil { + if errors.Is(err, context.DeadlineExceeded) { + fmt.Println("timeouted") + http.Redirect(w, r, "?err=WarnOffline", http.StatusSeeOther) + } else { + http.Error(w, err.Error(), 500) + } return } } diff --git a/lib/hackatime/fetch.go b/lib/hackatime/fetch.go index 49733c8..36fab8b 100644 --- a/lib/hackatime/fetch.go +++ b/lib/hackatime/fetch.go @@ -23,6 +23,7 @@ type User struct { Emails []string `json:"emails"` } +// 5 second timeout fetch for API requests, authenticates itself with a Bearer. func fetch(url string, bearer string) ([]byte, error) { req, err := http.NewRequest("GET", url, nil) if err != nil { diff --git a/lib/messages.go b/lib/messages.go index d17e14c..d2cc048 100644 --- a/lib/messages.go +++ b/lib/messages.go @@ -21,6 +21,8 @@ var Errors = map[string]error{ "UserNotFound": errors.New("User not found"), "InvalidSession": errors.New("Session expired or invalid"), + "WarnOffline": errors.New("It looks like you're offline. Some features might not work properly."), // TODO: maybe warnings? + "GitInvalidBranchName": errors.New("Invalid branch name."), "GitBranchTooLong": errors.New("Branch too long!"), "GitBranchTooShort": errors.New("Branch too short!"),