Better error handling in views

This commit is contained in:
Suraj 2016-02-01 19:15:04 +05:30
parent 534cb71064
commit a64c630767
6 changed files with 60 additions and 24 deletions

View File

@ -2,12 +2,13 @@ package db
import ( import (
"database/sql" "database/sql"
_ "github.com/mattn/go-sqlite3" //we want to use sqlite natively
md "github.com/shurcooL/github_flavored_markdown"
"github.com/thewhitetulip/Tasks/types"
"log" "log"
"strings" "strings"
"time" "time"
_ "github.com/mattn/go-sqlite3" //we want to use sqlite natively
md "github.com/shurcooL/github_flavored_markdown"
"github.com/thewhitetulip/Tasks/types"
) )
var database Database var database Database
@ -22,6 +23,7 @@ func (db Database) begin() (tx *sql.Tx) {
tx, err := db.db.Begin() tx, err := db.db.Begin()
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return nil
} }
return tx return tx
} }
@ -30,6 +32,7 @@ func (db Database) prepare(q string) (stmt *sql.Stmt) {
stmt, err := db.db.Prepare(q) stmt, err := db.db.Prepare(q)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return nil
} }
return stmt return stmt
} }
@ -38,6 +41,7 @@ func (db Database) query(q string, args ...interface{}) (rows *sql.Rows) {
rows, err := db.db.Query(q, args...) rows, err := db.db.Query(q, args...)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return nil
} }
return rows return rows
} }
@ -45,7 +49,7 @@ func (db Database) query(q string, args ...interface{}) (rows *sql.Rows) {
func init() { func init() {
database.db, err = sql.Open("sqlite3", "./tasks.db") database.db, err = sql.Open("sqlite3", "./tasks.db")
if err != nil { if err != nil {
log.Println(err) log.Fatal(err)
} }
} }
@ -56,7 +60,7 @@ func Close() {
//GetTasks retrieves all the tasks depending on the //GetTasks retrieves all the tasks depending on the
//status pending or trashed or completed //status pending or trashed or completed
func GetTasks(status string) types.Context { func GetTasks(status string) (types.Context, error) {
var task []types.Task var task []types.Task
var context types.Context var context types.Context
var TaskID int var TaskID int
@ -89,11 +93,11 @@ func GetTasks(status string) types.Context {
task = append(task, a) task = append(task, a)
} }
context = types.Context{Tasks: task, Navigation: status} context = types.Context{Tasks: task, Navigation: status}
return context return context, nil
} }
//GetTaskByID function gets the tasks from the ID passed to the function, used to populate EditTask //GetTaskByID function gets the tasks from the ID passed to the function, used to populate EditTask
func GetTaskByID(id int) types.Context { func GetTaskByID(id int) (types.Context, error) {
var tasks []types.Task var tasks []types.Task
var task types.Task var task types.Task
@ -110,7 +114,7 @@ func GetTaskByID(id int) types.Context {
} }
tasks = append(tasks, task) tasks = append(tasks, task)
context := types.Context{Tasks: tasks, Navigation: "edit"} context := types.Context{Tasks: tasks, Navigation: "edit"}
return context return context, nil
} }
//TrashTask is used to delete the task //TrashTask is used to delete the task

View File

@ -5,10 +5,11 @@ package main
* License: MIT * License: MIT
**/ **/
import ( import (
"github.com/thewhitetulip/Tasks/config"
"github.com/thewhitetulip/Tasks/views"
"log" "log"
"net/http" "net/http"
"github.com/thewhitetulip/Tasks/config"
"github.com/thewhitetulip/Tasks/views"
) )
func main() { func main() {

View File

@ -3,7 +3,6 @@ package views
import ( import (
"crypto/md5" "crypto/md5"
"fmt" "fmt"
"github.com/thewhitetulip/Tasks/db"
"io" "io"
"log" "log"
"net/http" "net/http"
@ -11,12 +10,13 @@ import (
"strconv" "strconv"
"text/template" "text/template"
"time" "time"
"github.com/thewhitetulip/Tasks/db"
) )
// UploadedFileHandler is used to handle the uploaded file related requests // UploadedFileHandler is used to handle the uploaded file related requests
func UploadedFileHandler(w http.ResponseWriter, r *http.Request) { func UploadedFileHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" { if r.Method == "GET" {
log.Println("into the handler")
token := r.URL.Path[len("/files/"):] token := r.URL.Path[len("/files/"):]
//file, err := db.GetFileName(token) //file, err := db.GetFileName(token)
@ -34,11 +34,15 @@ func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
file, handler, err := r.FormFile("uploadfile") file, handler, err := r.FormFile("uploadfile")
if err != nil { if err != nil {
log.Println(err) log.Println(err)
message = "Error uploading file"
http.Redirect(w, r, "/", http.StatusInternalServerError)
} }
taskPriority, priorityErr := strconv.Atoi(r.FormValue("priority")) taskPriority, priorityErr := strconv.Atoi(r.FormValue("priority"))
if priorityErr != nil { if priorityErr != nil {
log.Print(priorityErr) log.Print(priorityErr)
message = "Bad task priority"
http.Redirect(w, r, "/", http.StatusInternalServerError)
} }
priorityList := []int{1, 2, 3} priorityList := []int{1, 2, 3}
found := false found := false
@ -88,13 +92,16 @@ func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
if taskTruth != nil { if taskTruth != nil {
message = "Error adding task" message = "Error adding task"
log.Println("error adding task to db") log.Println("error adding task to db")
http.Redirect(w, r, "/", http.StatusInternalServerError)
} else { } else {
message = "Task added" message = "Task added"
log.Println("added task to db") log.Println("added task to db")
} }
http.Redirect(w, r, "/", http.StatusFound) http.Redirect(w, r, "/", http.StatusFound)
} else { } else {
log.Fatal("CSRF mismatch") log.Println("CSRF mismatch")
message = "Server Error"
http.Redirect(w, r, "/", http.StatusInternalServerError)
} }
} else { } else {

View File

@ -1,10 +1,11 @@
package views package views
import ( import (
"github.com/thewhitetulip/Tasks/db"
"log" "log"
"net/http" "net/http"
"strconv" "strconv"
"github.com/thewhitetulip/Tasks/db"
) )
//TrashTaskFunc is used to populate the trash tasks //TrashTaskFunc is used to populate the trash tasks
@ -13,6 +14,7 @@ func TrashTaskFunc(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(r.URL.Path[len("/trash/"):]) id, err := strconv.Atoi(r.URL.Path[len("/trash/"):])
if err != nil { if err != nil {
log.Println(err) log.Println(err)
http.Redirect(w, r, "/trash", http.StatusBadRequest)
} else { } else {
err = db.TrashTask(id) err = db.TrashTask(id)
if err != nil { if err != nil {
@ -20,7 +22,7 @@ func TrashTaskFunc(w http.ResponseWriter, r *http.Request) {
} else { } else {
message = "Task trashed" message = "Task trashed"
} }
http.Redirect(w, r, "/", http.StatusFound) http.Redirect(w, r, "/trash", http.StatusFound)
} }
} else { } else {
message = "Method not allowed" message = "Method not allowed"
@ -34,6 +36,7 @@ func RestoreTaskFunc(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(r.URL.Path[len("/restore/"):]) id, err := strconv.Atoi(r.URL.Path[len("/restore/"):])
if err != nil { if err != nil {
log.Println(err) log.Println(err)
http.Redirect(w, r, "/deleted", http.StatusBadRequest)
} else { } else {
err = db.RestoreTask(id) err = db.RestoreTask(id)
if err != nil { if err != nil {
@ -55,8 +58,12 @@ func EditTaskFunc(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(r.URL.Path[len("/edit/"):]) id, err := strconv.Atoi(r.URL.Path[len("/edit/"):])
if err != nil { if err != nil {
log.Println(err) log.Println(err)
http.Redirect(w, r, "/", http.StatusBadRequest)
} else { } else {
task := db.GetTaskByID(id) task, err := db.GetTaskByID(id)
if err != nil {
task.Message = "Error fetching Tasks"
}
editTemplate.Execute(w, task) editTemplate.Execute(w, task)
} }
} else { } else {
@ -70,12 +77,17 @@ func DeleteTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" { if r.Method == "GET" {
id := r.URL.Path[len("/delete/"):] id := r.URL.Path[len("/delete/"):]
if id == "all" { if id == "all" {
db.DeleteAll() err := db.DeleteAll()
if err != nil {
message = "Error deleting tasks"
http.Redirect(w, r, "/", http.StatusInternalServerError)
}
http.Redirect(w, r, "/", http.StatusFound) http.Redirect(w, r, "/", http.StatusFound)
} else { } else {
id, err := strconv.Atoi(id) id, err := strconv.Atoi(id)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
http.Redirect(w, r, "/", http.StatusBadRequest)
} else { } else {
err = db.DeleteTask(id) err = db.DeleteTask(id)
if err != nil { if err != nil {
@ -98,6 +110,7 @@ func RestoreFromCompleteFunc(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(r.URL.Path[len("/incomplete/"):]) id, err := strconv.Atoi(r.URL.Path[len("/incomplete/"):])
if err != nil { if err != nil {
log.Println(err) log.Println(err)
http.Redirect(w, r, "/completed", http.StatusBadRequest)
} else { } else {
err = db.RestoreTaskFromComplete(id) err = db.RestoreTaskFromComplete(id)
if err != nil { if err != nil {
@ -105,10 +118,10 @@ func RestoreFromCompleteFunc(w http.ResponseWriter, r *http.Request) {
} else { } else {
message = "Task restored" message = "Task restored"
} }
http.Redirect(w, r, "/pending/", http.StatusFound) http.Redirect(w, r, "/completed", http.StatusFound)
} }
} else { } else {
message = "Method not allowed" message = "Method not allowed"
http.Redirect(w, r, "/", http.StatusFound) http.Redirect(w, r, "/completed", http.StatusFound)
} }
} }

View File

@ -1,7 +1,6 @@
package views package views
import ( import (
"github.com/thewhitetulip/Tasks/db"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
@ -9,6 +8,8 @@ import (
"strconv" "strconv"
"strings" "strings"
"text/template" "text/template"
"github.com/thewhitetulip/Tasks/db"
) )
//PopulateTemplates is used to parse all templates present in //PopulateTemplates is used to parse all templates present in

View File

@ -2,12 +2,13 @@ package views
import ( import (
"bufio" "bufio"
"github.com/thewhitetulip/Tasks/db"
"net/http" "net/http"
"os" "os"
"strings" "strings"
"text/template" "text/template"
"time" "time"
"github.com/thewhitetulip/Tasks/db"
) )
var homeTemplate *template.Template var homeTemplate *template.Template
@ -23,7 +24,10 @@ var err error
//TODO add http404 error //TODO add http404 error
func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request) { func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" { if r.Method == "GET" {
context := db.GetTasks("pending") //true when you want non deleted notes context, err := db.GetTasks("pending")
if err != nil {
http.Redirect(w, r, "/", http.StatusInternalServerError)
}
if message != "" { if message != "" {
context.Message = message context.Message = message
} }
@ -42,7 +46,10 @@ func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request) {
//ShowTrashTaskFunc is used to handle the "/trash" URL which is used to show the deleted tasks //ShowTrashTaskFunc is used to handle the "/trash" URL which is used to show the deleted tasks
func ShowTrashTaskFunc(w http.ResponseWriter, r *http.Request) { func ShowTrashTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" { if r.Method == "GET" {
context := db.GetTasks("deleted") //false when you want deleted notes context, err := db.GetTasks("deleted")
if err != nil {
http.Redirect(w, r, "/trash", http.StatusInternalServerError)
}
if message != "" { if message != "" {
context.Message = message context.Message = message
message = "" message = ""
@ -57,7 +64,10 @@ func ShowTrashTaskFunc(w http.ResponseWriter, r *http.Request) {
//ShowCompleteTasksFunc is used to populate the "/completed/" URL //ShowCompleteTasksFunc is used to populate the "/completed/" URL
func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) { func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" { if r.Method == "GET" {
context := db.GetTasks("completed") //false when you want finished notes context, err := db.GetTasks("completed")
if err != nil {
http.Redirect(w, r, "/completed", http.StatusInternalServerError)
}
completedTemplate.Execute(w, context) completedTemplate.Execute(w, context)
} else { } else {
message = "Method not allowed" message = "Method not allowed"