From ec2bb72db5f6d837d0828f1c8cb29a5b1dec0ed4 Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Wed, 4 Apr 2018 15:45:20 +0200 Subject: [PATCH] adding new error when files key does not exist in the form data --- app/handlers/converter/converter.go | 10 +++++++++- app/handlers/handlers.go | 2 ++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app/handlers/converter/converter.go b/app/handlers/converter/converter.go index d69be9f7..b826672a 100644 --- a/app/handlers/converter/converter.go +++ b/app/handlers/converter/converter.go @@ -27,6 +27,14 @@ func (e *NoFileToConvertError) Error() string { return "There is no file to convert" } +// FilesKeyNotFoundError is raised when "files" key does not exist +// in the form data +type FilesKeyNotFoundError struct{} + +func (e *FilesKeyNotFoundError) Error() string { + return "\"files\" key was not found in the form data" +} + // NewConverter instantiates a converter by parsing a request. func NewConverter(r *http.Request, contentType ghttp.ContentType) (*Converter, error) { c := &Converter{ @@ -47,7 +55,7 @@ func NewConverter(r *http.Request, contentType ghttp.ContentType) (*Converter, e formData := r.MultipartForm files, ok := formData.File["files"] if !ok { - return nil, nil + return nil, &FilesKeyNotFoundError{} } for i := range files { diff --git a/app/handlers/handlers.go b/app/handlers/handlers.go index 32f66f3c..c5b8a7ce 100644 --- a/app/handlers/handlers.go +++ b/app/handlers/handlers.go @@ -72,6 +72,8 @@ func convertHandler(next http.Handler) http.Handler { if err != nil { if noFileToConvertError, ok := err.(*converter.NoFileToConvertError); ok { http.Error(w, noFileToConvertError.Error(), http.StatusBadRequest) + } else if filesKeyNotFoundError, ok := err.(*converter.FilesKeyNotFoundError); ok { + http.Error(w, filesKeyNotFoundError.Error(), http.StatusBadRequest) } else { http.Error(w, err.Error(), http.StatusInternalServerError) }