More verbose errors
This commit is contained in:
parent
335517bf17
commit
d40803f165
|
@ -35,7 +35,7 @@ func deleteHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
|||
metaDelErr := os.Remove(metaPath)
|
||||
|
||||
if (fileDelErr != nil) || (metaDelErr != nil) {
|
||||
oopsHandler(c, w, r) // 500 - can't delete something
|
||||
oopsHandler(c, w, r, RespPLAIN, "Could not delete")
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -91,6 +91,6 @@ func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
|||
}, w)
|
||||
|
||||
if err != nil {
|
||||
oopsHandler(c, w, r)
|
||||
oopsHandler(c, w, r, RespHTML, "")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ func staticHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
|||
filePath := strings.TrimPrefix(path, "/static/")
|
||||
file, err := staticBox.Open(filePath)
|
||||
if err != nil {
|
||||
oopsHandler(c, w, r)
|
||||
notFoundHandler(c, w, r)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
50
pages.go
50
pages.go
|
@ -1,12 +1,24 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/flosch/pongo2"
|
||||
"github.com/zenazn/goji/web"
|
||||
)
|
||||
|
||||
type RespType int
|
||||
|
||||
const (
|
||||
RespPLAIN RespType = iota
|
||||
RespJSON
|
||||
RespHTML
|
||||
RespAUTO
|
||||
)
|
||||
|
||||
func indexHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
||||
err := Templates["index.html"].ExecuteWriter(pongo2.Context{}, w)
|
||||
if err != nil {
|
||||
|
@ -17,7 +29,7 @@ func indexHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
|||
func pasteHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
||||
err := Templates["paste.html"].ExecuteWriter(pongo2.Context{}, w)
|
||||
if err != nil {
|
||||
oopsHandler(c, w, r)
|
||||
oopsHandler(c, w, r, RespHTML, "")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -25,15 +37,41 @@ func notFoundHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
|||
w.WriteHeader(404)
|
||||
err := Templates["404.html"].ExecuteWriter(pongo2.Context{}, w)
|
||||
if err != nil {
|
||||
oopsHandler(c, w, r)
|
||||
oopsHandler(c, w, r, RespHTML, "")
|
||||
}
|
||||
}
|
||||
|
||||
func oopsHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
||||
func oopsHandler(c web.C, w http.ResponseWriter, r *http.Request, rt RespType, msg string) {
|
||||
if msg == "" {
|
||||
msg = "Oops! Something went wrong..."
|
||||
}
|
||||
|
||||
if rt == RespHTML {
|
||||
Templates["oops.html"].ExecuteWriter(pongo2.Context{"msg": msg}, w)
|
||||
w.WriteHeader(500)
|
||||
err := Templates["oops.html"].ExecuteWriter(pongo2.Context{}, w)
|
||||
if err != nil {
|
||||
oopsHandler(c, w, r)
|
||||
return
|
||||
|
||||
} else if rt == RespPLAIN {
|
||||
fmt.Fprintf(w, "%s", msg)
|
||||
w.WriteHeader(500)
|
||||
return
|
||||
|
||||
} else if rt == RespJSON {
|
||||
js, _ := json.Marshal(map[string]string{
|
||||
"error": msg,
|
||||
})
|
||||
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(500)
|
||||
w.Write(js)
|
||||
return
|
||||
|
||||
} else if rt == RespAUTO {
|
||||
if strings.EqualFold("application/json", r.Header.Get("Accept")) {
|
||||
oopsHandler(c, w, r, RespJSON, msg)
|
||||
} else {
|
||||
oopsHandler(c, w, r, RespHTML, msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -136,8 +136,8 @@ func TestPutEmptyUpload(t *testing.T) {
|
|||
|
||||
goji.DefaultMux.ServeHTTP(w, req)
|
||||
|
||||
if !strings.Contains(w.Body.String(), "Oops! Something went wrong.") {
|
||||
t.Fatal("Response doesn't contain'Oops! Something went wrong.'")
|
||||
if !strings.Contains(w.Body.String(), "Empty file") {
|
||||
t.Fatal("Response doesn't contain'Empty file'")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -58,15 +58,15 @@ Dropzone.options.dropzone = {
|
|||
xhr.send();
|
||||
}.bind(this);
|
||||
},
|
||||
error: function(file, errMsg, xhrO) {
|
||||
error: function(file, resp, xhrO) {
|
||||
file.rightLeftElement.onclick = null;
|
||||
file.rightLeftElement.innerHTML = "";
|
||||
file.rightRightElement.innerHTML = "";
|
||||
if (file.status === "canceled") {
|
||||
file.leftElement.innerHTML = "Canceled " + file.name;
|
||||
file.leftElement.innerHTML = file.name + ": Canceled ";
|
||||
}
|
||||
else {
|
||||
file.leftElement.innerHTML = "Could not upload " + file.name;
|
||||
file.leftElement.innerHTML = file.name + ": " + resp.error;
|
||||
}
|
||||
file.leftElement.className = "error";
|
||||
},
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
{% block content %}
|
||||
<div id="main">
|
||||
<div id='inner_content' style='width: 400px'>
|
||||
<p>{{ error_message|default:"Oops! Something went wrong." }}</p>
|
||||
<p>{{ msg }}</p>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
|
|
@ -87,7 +87,7 @@ func fileTorrentHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
encoded, err := createTorrent(fileName, filePath)
|
||||
if err != nil {
|
||||
oopsHandler(c, w, r) // 500 - creating torrent failed
|
||||
oopsHandler(c, w, r, RespHTML, "Could not create torrent.")
|
||||
return
|
||||
}
|
||||
|
||||
|
|
49
upload.go
49
upload.go
|
@ -46,7 +46,7 @@ func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
|||
if strings.HasPrefix(contentType, "multipart/form-data") {
|
||||
file, headers, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
oopsHandler(c, w, r)
|
||||
oopsHandler(c, w, r, RespHTML, "Could not upload file.")
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
@ -60,7 +60,7 @@ func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
|||
upReq.filename = headers.Filename
|
||||
} else {
|
||||
if r.FormValue("content") == "" {
|
||||
oopsHandler(c, w, r)
|
||||
oopsHandler(c, w, r, RespHTML, "Could not upload file.")
|
||||
return
|
||||
}
|
||||
extension := r.FormValue("extension")
|
||||
|
@ -74,16 +74,22 @@ func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
upload, err := processUpload(upReq)
|
||||
|
||||
if strings.EqualFold("application/json", r.Header.Get("Accept")) {
|
||||
if err != nil {
|
||||
oopsHandler(c, w, r)
|
||||
oopsHandler(c, w, r, RespJSON, "Could not upload file: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if strings.EqualFold("application/json", r.Header.Get("Accept")) {
|
||||
js := generateJSONresponse(upload)
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.Write(js)
|
||||
} else {
|
||||
if err != nil {
|
||||
oopsHandler(c, w, r, RespHTML, "Could not upload file: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/"+upload.Filename, 301)
|
||||
}
|
||||
|
||||
|
@ -98,16 +104,22 @@ func uploadPutHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
|||
upReq.src = r.Body
|
||||
|
||||
upload, err := processUpload(upReq)
|
||||
|
||||
if strings.EqualFold("application/json", r.Header.Get("Accept")) {
|
||||
if err != nil {
|
||||
oopsHandler(c, w, r)
|
||||
oopsHandler(c, w, r, RespJSON, "Could not upload file: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if strings.EqualFold("application/json", r.Header.Get("Accept")) {
|
||||
js := generateJSONresponse(upload)
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.Write(js)
|
||||
} else {
|
||||
if err != nil {
|
||||
oopsHandler(c, w, r, RespPLAIN, "Could not upload file: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, Config.siteURL+upload.Filename)
|
||||
}
|
||||
}
|
||||
|
@ -123,7 +135,7 @@ func uploadRemote(c web.C, w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
resp, err := http.Get(grabUrl.String())
|
||||
if err != nil {
|
||||
oopsHandler(c, w, r)
|
||||
oopsHandler(c, w, r, RespAUTO, "Could not retrieve URL")
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -131,16 +143,22 @@ func uploadRemote(c web.C, w http.ResponseWriter, r *http.Request) {
|
|||
upReq.src = resp.Body
|
||||
|
||||
upload, err := processUpload(upReq)
|
||||
|
||||
if strings.EqualFold("application/json", r.Header.Get("Accept")) {
|
||||
if err != nil {
|
||||
oopsHandler(c, w, r)
|
||||
oopsHandler(c, w, r, RespJSON, "Could not upload file: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if strings.EqualFold("application/json", r.Header.Get("Accept")) {
|
||||
js := generateJSONresponse(upload)
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.Write(js)
|
||||
} else {
|
||||
if err != nil {
|
||||
oopsHandler(c, w, r, RespHTML, "Could not upload file: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/"+upload.Filename, 301)
|
||||
}
|
||||
}
|
||||
|
@ -174,8 +192,8 @@ func processUpload(upReq UploadRequest) (upload Upload, err error) {
|
|||
// Pull the first 512 bytes off for use in MIME detection
|
||||
header = make([]byte, 512)
|
||||
n, err := upReq.src.Read(header)
|
||||
if n == 0 {
|
||||
return upload, err
|
||||
if n == 0 || err != nil {
|
||||
return upload, errors.New("Empty file")
|
||||
}
|
||||
header = header[:n]
|
||||
|
||||
|
@ -228,12 +246,15 @@ func processUpload(upReq UploadRequest) (upload Upload, err error) {
|
|||
metadataWrite(upload.Filename, &upload)
|
||||
|
||||
bytes, err := io.Copy(dst, io.MultiReader(bytes.NewReader(header), upReq.src))
|
||||
if err != nil {
|
||||
return
|
||||
} else if bytes == 0 {
|
||||
if bytes == 0 {
|
||||
os.Remove(path.Join(Config.filesDir, upload.Filename))
|
||||
os.Remove(path.Join(Config.metaDir, upload.Filename))
|
||||
return upload, errors.New("Empty file")
|
||||
|
||||
} else if err != nil {
|
||||
os.Remove(path.Join(Config.filesDir, upload.Filename))
|
||||
os.Remove(path.Join(Config.metaDir, upload.Filename))
|
||||
return
|
||||
}
|
||||
|
||||
upload.Size = bytes
|
||||
|
|
Loading…
Reference in New Issue