Move internal error handling into his own func
This commit is contained in:
parent
50757e3feb
commit
4b1bd2c8e0
23
main.go
23
main.go
@ -119,6 +119,14 @@ func RandStringBytesMaskImprSrc(n int) string {
|
|||||||
return string(b)
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// internalError receives a http.ResponseWriter, msg and error and
|
||||||
|
// return a internal error page with http code 500 to the user
|
||||||
|
func internalError(w http.ResponseWriter, msg string, err error) {
|
||||||
|
log.Println(err)
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
internalErrorTmpl.Execute(w, msg+err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
func itemsCount(w http.ResponseWriter, r *http.Request) {
|
func itemsCount(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write(
|
w.Write(
|
||||||
[]byte(
|
[]byte(
|
||||||
@ -134,9 +142,7 @@ func itemsDump(w http.ResponseWriter, r *http.Request) {
|
|||||||
pool.Items(),
|
pool.Items(),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
internalError(w, "Unable to dump key value db: ", err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
internalErrorTmpl.Execute(w, err.Error())
|
|
||||||
}
|
}
|
||||||
w.Write(
|
w.Write(
|
||||||
[]byte(dumpObj),
|
[]byte(dumpObj),
|
||||||
@ -148,9 +154,7 @@ func itemsFromFile(w http.ResponseWriter, r *http.Request, dumpFile string) {
|
|||||||
var dumpObj map[string]cache.Item
|
var dumpObj map[string]cache.Item
|
||||||
json.Unmarshal([]byte(jsonFile), &dumpObj)
|
json.Unmarshal([]byte(jsonFile), &dumpObj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
internalError(w, "Cannot open file "+dumpFile+": ", err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
internalErrorTmpl.Execute(w, "Cannot open file "+dumpFile+"\n"+err.Error())
|
|
||||||
} else {
|
} else {
|
||||||
pool = cache.NewFrom(240*time.Hour, 1*time.Hour, dumpObj)
|
pool = cache.NewFrom(240*time.Hour, 1*time.Hour, dumpObj)
|
||||||
okTmpl.Execute(w, "Imported "+strconv.Itoa(len(dumpObj))+" items to the DB")
|
okTmpl.Execute(w, "Imported "+strconv.Itoa(len(dumpObj))+" items to the DB")
|
||||||
@ -162,9 +166,7 @@ func itemsFromPost(w http.ResponseWriter, r *http.Request) {
|
|||||||
var dumpObj map[string]cache.Item
|
var dumpObj map[string]cache.Item
|
||||||
err := decoder.Decode(&dumpObj)
|
err := decoder.Decode(&dumpObj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
internalError(w, "Cannot parse JSON: ", err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
internalErrorTmpl.Execute(w, "Cannot parse JSON: "+err.Error())
|
|
||||||
} else {
|
} else {
|
||||||
pool = cache.NewFrom(240*time.Hour, 1*time.Hour, dumpObj)
|
pool = cache.NewFrom(240*time.Hour, 1*time.Hour, dumpObj)
|
||||||
okTmpl.Execute(w, "Imported "+strconv.Itoa(len(dumpObj))+" items to the DB")
|
okTmpl.Execute(w, "Imported "+strconv.Itoa(len(dumpObj))+" items to the DB")
|
||||||
@ -177,8 +179,7 @@ func itemsDumpToFile(w http.ResponseWriter, r *http.Request, dumpFile string) {
|
|||||||
)
|
)
|
||||||
err := ioutil.WriteFile(dumpFile, dumpObj, 0644)
|
err := ioutil.WriteFile(dumpFile, dumpObj, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
internalError(w, "Failed to open json file: ", err)
|
||||||
internalErrorTmpl.Execute(w, "Failed to open json file: "+err.Error())
|
|
||||||
} else {
|
} else {
|
||||||
okTmpl.Execute(w, "Dump writen to: "+dumpFile)
|
okTmpl.Execute(w, "Dump writen to: "+dumpFile)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user