changeset 5322:80d9fd782f00 extented-report

Straightened the error logging in report controller.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 27 May 2021 15:09:13 +0200
parents 0919946f624b
children eec88a166251 96ceb150ea46
files pkg/controllers/report.go
diffstat 1 files changed, 25 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/report.go	Thu May 27 01:22:10 2021 +0200
+++ b/pkg/controllers/report.go	Thu May 27 15:09:13 2021 +0200
@@ -30,46 +30,49 @@
 
 func report(rw http.ResponseWriter, req *http.Request) {
 
-	vars := mux.Vars(req)
-
-	name := vars["name"]
-
-	config.WaitReady()
 	path := config.ReportPath()
 	if path == "" {
 		http.NotFound(rw, req)
 		return
 	}
 
-	stat, err := os.Stat(path)
-	if err != nil {
-		http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError)
+	if stat, err := os.Stat(path); err != nil {
+		if os.IsNotExist(err) {
+			log.Printf("error: report dir '%s' does not exists.\n", path)
+			http.NotFound(rw, req)
+		} else {
+			log.Printf("error: %v\n", err)
+			http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError)
+		}
+		return
+	} else if !stat.Mode().IsDir() {
+		log.Printf("error: report dir '%s' is not a directory.\n", path)
+		http.NotFound(rw, req)
 		return
 	}
 
-	if !stat.Mode().IsDir() {
-		http.NotFound(rw, req)
-		return
-	}
+	vars := mux.Vars(req)
+	name := vars["name"]
 
 	xlsxFilename := filepath.Join(path, name+".xlsx")
 	yamlFilename := filepath.Join(path, name+".yaml")
 
-	_, errX := os.Stat(xlsxFilename)
-	_, errY := os.Stat(yamlFilename)
-	if errX != nil || errX != nil {
-		if (errX != nil && os.IsNotExist(errX)) || (errY != nil && os.IsNotExist(errY)) {
-			http.NotFound(rw, req)
-		} else {
-			http.Error(rw, "Something is wrong", http.StatusInternalServerError)
+	for _, check := range []string{xlsxFilename, yamlFilename} {
+		if _, err := os.Stat(check); err != nil {
+			if os.IsNotExist(err) {
+				http.NotFound(rw, req)
+			} else {
+				log.Printf("error: %v\n", err)
+				http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError)
+			}
+			return
 		}
-		return
 	}
 
 	template, err := excelize.OpenFile(xlsxFilename)
 	if err != nil {
+		log.Printf("error: %v\n", err)
 		http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError)
-		log.Printf("error: %v\n", err)
 		return
 	}
 
@@ -84,8 +87,8 @@
 	conn := middleware.GetDBConn(req)
 
 	if err := action.Execute(ctx, conn, template); err != nil {
+		log.Printf("error: %v\n", err)
 		http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError)
-		log.Printf("error: %v\n", err)
 		return
 	}
 	rw.Header().Set(