improving error handling

This commit is contained in:
Remco 2014-11-12 13:44:06 +01:00
parent 3f81515cd2
commit 952f13634e

View file

@ -173,7 +173,7 @@ func notFoundHandler(w http.ResponseWriter, r *http.Request) {
func postHandler(w http.ResponseWriter, r *http.Request) { func postHandler(w http.ResponseWriter, r *http.Request) {
if err := r.ParseMultipartForm(_24K); nil != err { if err := r.ParseMultipartForm(_24K); nil != err {
log.Println(err) log.Printf("%s", err.Error())
http.Error(w, "Error occured copying to output stream", 500) http.Error(w, "Error occured copying to output stream", 500)
return return
} }
@ -195,7 +195,7 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
var err error var err error
if f, err = fheader.Open(); err != nil { if f, err = fheader.Open(); err != nil {
log.Print(err) log.Printf("%s", err.Error())
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
} }
@ -204,7 +204,7 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
n, err := io.CopyN(&b, f, _24K+1) n, err := io.CopyN(&b, f, _24K+1)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
log.Print(err) log.Printf("%s", err.Error())
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
} }
@ -222,7 +222,7 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
os.Remove(file.Name()) os.Remove(file.Name())
log.Print(err) log.Printf("%s", err.Error())
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
} }
@ -237,7 +237,7 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("Uploading %s %s %d %s", token, filename, contentLength, contentType) log.Printf("Uploading %s %s %d %s", token, filename, contentLength, contentType)
if err = storage.Put(token, filename, reader, contentType, uint64(contentLength)); err != nil { if err = storage.Put(token, filename, reader, contentType, uint64(contentLength)); err != nil {
log.Print(err) log.Printf("%s", err.Error())
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
@ -266,6 +266,7 @@ func scanHandler(w http.ResponseWriter, r *http.Request) {
response, err := c.ScanStream(reader) response, err := c.ScanStream(reader)
if err != nil { if err != nil {
log.Printf("%s", err.Error())
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
} }
@ -305,7 +306,7 @@ func putHandler(w http.ResponseWriter, r *http.Request) {
n, err := io.CopyN(&b, f, _24K+1) n, err := io.CopyN(&b, f, _24K+1)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
log.Print(err) log.Printf("%s", err.Error())
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
} }
@ -313,7 +314,7 @@ func putHandler(w http.ResponseWriter, r *http.Request) {
if n > _24K { if n > _24K {
file, err := ioutil.TempFile(config.Temp, "transfer-") file, err := ioutil.TempFile(config.Temp, "transfer-")
if err != nil { if err != nil {
log.Print(err) log.Printf("%s", err.Error())
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
} }
@ -323,7 +324,7 @@ func putHandler(w http.ResponseWriter, r *http.Request) {
n, err = io.Copy(file, io.MultiReader(&b, f)) n, err = io.Copy(file, io.MultiReader(&b, f))
if err != nil { if err != nil {
os.Remove(file.Name()) os.Remove(file.Name())
log.Print(err) log.Printf("%s", err.Error())
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
} }
@ -349,8 +350,8 @@ func putHandler(w http.ResponseWriter, r *http.Request) {
var err error var err error
if err = storage.Put(token, filename, reader, contentType, uint64(contentLength)); err != nil { if err = storage.Put(token, filename, reader, contentType, uint64(contentLength)); err != nil {
log.Printf("%s", err.Error())
http.Error(w, errors.New("Could not save file").Error(), 500) http.Error(w, errors.New("Could not save file").Error(), 500)
log.Printf(err)
return return
} }
@ -567,6 +568,7 @@ func getHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Connection", "close") w.Header().Set("Connection", "close")
if _, err = io.Copy(w, reader); err != nil { if _, err = io.Copy(w, reader); err != nil {
log.Printf("%s", err.Error())
http.Error(w, "Error occured copying to output stream", 500) http.Error(w, "Error occured copying to output stream", 500)
return return
} }