diff --git a/ajax.go b/ajax.go index a0130d1..cbe716f 100644 --- a/ajax.go +++ b/ajax.go @@ -24,10 +24,7 @@ func (a ajaxResponse) Bytes() []byte { func ajaxGetHandler(res http.ResponseWriter, r *http.Request, session *sessions.Session, ctx *pongo2.Context) (*string, error) { res.Header().Set("Content-Type", "application/json") - user, err := checkLogin(r, session) - if err != nil { - return nil, err // TODO: Handle in-app? - } + user, _ := checkLogin(r, session) if user == nil || !storage.IsPresent(user.UserFile) { res.Write(ajaxResponse{Error: true}.Bytes()) @@ -36,13 +33,12 @@ func ajaxGetHandler(res http.ResponseWriter, r *http.Request, session *sessions. userFileRaw, err := storage.Read(user.UserFile) if err != nil { - return nil, err // TODO: Handle in-app? + fmt.Printf("ERR: Unable to read user file: %s\n", err) + res.Write(ajaxResponse{Error: true}.Bytes()) + return nil, nil } - userFile, err := readDataObject(userFileRaw) - if err != nil { - return nil, err // TODO: Handle in-app? - } + userFile, _ := readDataObject(userFileRaw) res.Write(ajaxResponse{Version: userFile.MetaData.Version, Data: userFile.Data}.Bytes()) return nil, nil @@ -50,10 +46,7 @@ func ajaxGetHandler(res http.ResponseWriter, r *http.Request, session *sessions. func ajaxPostHandler(res http.ResponseWriter, r *http.Request, session *sessions.Session, ctx *pongo2.Context) (*string, error) { res.Header().Set("Content-Type", "application/json") - user, err := checkLogin(r, session) - if err != nil { - return nil, err // TODO: Handle in-app? - } + user, _ := checkLogin(r, session) if user == nil { res.Write(ajaxResponse{Error: true, Type: "login"}.Bytes()) @@ -67,13 +60,12 @@ func ajaxPostHandler(res http.ResponseWriter, r *http.Request, session *sessions userFileRaw, err := storage.Read(user.UserFile) if err != nil { - return nil, err // TODO: Handle in-app? + fmt.Printf("ERR: Unable to read user file: %s\n", err) + res.Write(ajaxResponse{Error: true, Type: "storage_error"}.Bytes()) + return nil, nil } - userFile, err := readDataObject(userFileRaw) - if err != nil { - return nil, err // TODO: Handle in-app? - } + userFile, _ := readDataObject(userFileRaw) var ( version = r.FormValue("version") @@ -92,19 +84,20 @@ func ajaxPostHandler(res http.ResponseWriter, r *http.Request, session *sessions } if err := storage.Backup(user.UserFile); err != nil { - return nil, err // TODO: Handle in-app? + fmt.Printf("ERR: Unable to backup user file: %s\n", err) + res.Write(ajaxResponse{Error: true, Type: "storage_error"}.Bytes()) + return nil, nil } userFile.MetaData.Version = checksum userFile.Data = data - d, err := userFile.GetData() - if err != nil { - return nil, err // TODO: Handle in-app? - } + d, _ := userFile.GetData() if err := storage.Write(user.UserFile, d); err != nil { - return nil, err // TODO: Handle in-app? + fmt.Printf("ERR: Unable to write user file: %s\n", err) + res.Write(ajaxResponse{Error: true, Type: "storage_error"}.Bytes()) + return nil, nil } res.Write(ajaxResponse{Version: userFile.MetaData.Version, Data: userFile.Data}.Bytes()) diff --git a/login.go b/login.go index 494396b..513e4f4 100644 --- a/login.go +++ b/login.go @@ -24,13 +24,12 @@ func loginHandler(res http.ResponseWriter, r *http.Request, session *sessions.Se userFileRaw, err := storage.Read(createUserFilename(username)) if err != nil { - return nil, err // TODO: Handle in-app? + fmt.Printf("ERR: Unable to read user file: %s\n", err) + (*ctx)["error"] = true + return stringPointer("login.html"), nil } - userFile, err := readDataObject(userFileRaw) - if err != nil { - return nil, err // TODO: Handle in-app? - } + userFile, _ := readDataObject(userFileRaw) if userFile.MetaData.Password != password { (*ctx)["error"] = true @@ -74,7 +73,7 @@ func checkLogin(r *http.Request, session *sessions.Session) (*authorizedAccount, vars := mux.Vars(r) idx, err := strconv.ParseInt(vars["userIndex"], 10, 64) if err != nil { - return nil, err // TODO: Handle in-app? + return nil, err } auth, ok := session.Values["authorizedAccounts"].(authorizedAccounts) diff --git a/overview.go b/overview.go index 2b729b9..d92f378 100644 --- a/overview.go +++ b/overview.go @@ -8,10 +8,7 @@ import ( ) func overviewHandler(res http.ResponseWriter, r *http.Request, session *sessions.Session, ctx *pongo2.Context) (*string, error) { - user, err := checkLogin(r, session) - if err != nil { - return nil, err // TODO: Handle in-app? - } + user, _ := checkLogin(r, session) if user == nil || !storage.IsPresent(user.UserFile) { http.Redirect(res, r, "../../login", http.StatusFound) diff --git a/register.go b/register.go index fbea71e..edcd198 100644 --- a/register.go +++ b/register.go @@ -28,15 +28,14 @@ func registerHandler(res http.ResponseWriter, r *http.Request, session *sessions d := dataObject{} d.MetaData.Password = hashedPassword - data, err := d.GetData() - if err != nil { - return nil, err // TODO: Handle in-app? - } + data, _ := d.GetData() - if err := storage.Write(createUserFilename(username), data); err == nil { - (*ctx)["created"] = true + if err := storage.Write(createUserFilename(username), data); err != nil { + fmt.Printf("ERR: Unable to write user file: %s\n", err) + (*ctx)["error"] = true return stringPointer("register.html"), nil } - return nil, err // TODO: Handle in-app? + (*ctx)["created"] = true + return stringPointer("register.html"), nil } diff --git a/templates/register.html b/templates/register.html index efb6a6d..a9e8ca9 100644 --- a/templates/register.html +++ b/templates/register.html @@ -18,6 +18,14 @@ {% endif %} +{% if error %} +
+
+

Ooops…

+

Something went wrong. Please try again.

+
+
+{% endif %}

Please register