From 0a80a6c57f2e26a1b1513bdfd8de130f01ae7f2a Mon Sep 17 00:00:00 2001 From: Janne Koschinski <janne@kuschku.de> Date: Mon, 11 Mar 2019 22:22:04 +0100 Subject: [PATCH] Improved file parsing --- main.go | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index 29f18af..72034cf 100644 --- a/main.go +++ b/main.go @@ -20,11 +20,11 @@ import ( "fmt" "html/template" "log" - "net/url" - "path" "mime" "net/http" + "net/url" "os" + "path" "strconv" "strings" "time" @@ -132,16 +132,20 @@ func errorHandler(errorFilesPath string) func(http.ResponseWriter, *http.Request var file string for _, ext := range exts { - filePath := fmt.Sprintf("%v/%v%v", errorFilesPath, code, ext) - if _, err := os.Stat(file); os.IsNotExist(err) { - scode := strconv.Itoa(code) - filePath = fmt.Sprintf("%v/%cxx%v", errorFilesPath, scode[0], ext) - if _, err := os.Stat(file); os.IsNotExist(err) { - continue - } + var filePath string + + filePath = fmt.Sprintf("%v/%v%v", errorFilesPath, code, ext) + if _, err = os.Stat(filePath); err == nil { + file = filePath + break + } + + scode := strconv.Itoa(code) + filePath = fmt.Sprintf("%v/%cxx%v", errorFilesPath, scode[0], ext) + if _, err = os.Stat(filePath); err == nil { + file = filePath + break } - file = filePath - break } if file == "" { log.Printf("unexpected error opening file: %v", err) -- GitLab