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