better error pages
This commit is contained in:
@@ -10,6 +10,11 @@ import (
|
||||
|
||||
var conf *config.Config
|
||||
|
||||
type response struct {
|
||||
Message string `json:"message"`
|
||||
Location string `json:"location"`
|
||||
}
|
||||
|
||||
// ServeHTTP is used to serve the content of Browse page using Browse middleware
|
||||
// from Caddy. It handles the requests for DELETE, POST, GET and PUT related to
|
||||
// /browse interface.
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/hacdias/caddy-hugo/tools/server"
|
||||
s "github.com/hacdias/caddy-hugo/tools/server"
|
||||
)
|
||||
|
||||
// DELETE handles the delete requests on browse pages
|
||||
@@ -31,17 +31,11 @@ func DELETE(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
|
||||
// Check for errors
|
||||
if err != nil {
|
||||
return server.RespondJSON(w, map[string]string{
|
||||
"message": "Something went wrong.",
|
||||
}, 500, nil)
|
||||
return s.RespondJSON(w, &response{err.Error(), ""}, http.StatusInternalServerError, err)
|
||||
}
|
||||
} else {
|
||||
return server.RespondJSON(w, map[string]string{
|
||||
"message": "File not found.",
|
||||
}, 404, nil)
|
||||
return s.RespondJSON(w, &response{"File not found.", ""}, http.StatusNotFound, nil)
|
||||
}
|
||||
|
||||
return server.RespondJSON(w, map[string]string{
|
||||
"message": message,
|
||||
}, 200, nil)
|
||||
return s.RespondJSON(w, &response{message, ""}, http.StatusOK, nil)
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/hacdias/caddy-hugo/tools/commands"
|
||||
"github.com/hacdias/caddy-hugo/tools/server"
|
||||
s "github.com/hacdias/caddy-hugo/tools/server"
|
||||
)
|
||||
|
||||
// POST handles the POST method on browse page. It's used to create new files,
|
||||
@@ -36,15 +36,11 @@ func POST(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
// Check if filename and archetype are specified in
|
||||
// the request
|
||||
if _, ok := info["filename"]; !ok {
|
||||
return server.RespondJSON(w, map[string]string{
|
||||
"message": "Filename not specified.",
|
||||
}, 500, nil)
|
||||
return s.RespondJSON(w, &response{"Filename not specified.", ""}, http.StatusBadRequest, nil)
|
||||
}
|
||||
|
||||
if _, ok := info["archetype"]; !ok {
|
||||
return server.RespondJSON(w, map[string]string{
|
||||
"message": "Archtype not specified.",
|
||||
}, 500, nil)
|
||||
return s.RespondJSON(w, &response{"Archtype not specified.", ""}, http.StatusBadRequest, nil)
|
||||
}
|
||||
|
||||
// Sanitize the file name path
|
||||
@@ -66,9 +62,7 @@ func POST(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
}
|
||||
|
||||
if err := commands.Run(conf.Hugo, args, conf.Path); err != nil {
|
||||
return server.RespondJSON(w, map[string]string{
|
||||
"message": "Something went wrong.",
|
||||
}, 500, err)
|
||||
return s.RespondJSON(w, &response{"Something went wrong.", ""}, http.StatusInternalServerError, err)
|
||||
}
|
||||
} else {
|
||||
var err error
|
||||
@@ -83,26 +77,19 @@ func POST(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return server.RespondJSON(w, map[string]string{
|
||||
"message": "Something went wrong.",
|
||||
}, 500, err)
|
||||
return s.RespondJSON(w, &response{"Something went wrong.", ""}, http.StatusInternalServerError, err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return server.RespondJSON(w, map[string]string{
|
||||
"location": url,
|
||||
"message": "File created.",
|
||||
}, 200, nil)
|
||||
return s.RespondJSON(w, &response{"File created!", url}, http.StatusOK, nil)
|
||||
}
|
||||
|
||||
func upload(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
// Parse the multipart form in the request
|
||||
err := r.ParseMultipartForm(100000)
|
||||
if err != nil {
|
||||
return server.RespondJSON(w, map[string]string{
|
||||
"message": "Something went wrong.",
|
||||
}, 500, err)
|
||||
return s.RespondJSON(w, &response{"Something went wrong.", ""}, http.StatusInternalServerError, err)
|
||||
}
|
||||
|
||||
// For each file header in the multipart form
|
||||
@@ -112,29 +99,23 @@ func upload(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
// Open the first file
|
||||
var infile multipart.File
|
||||
if infile, err = hdr.Open(); nil != err {
|
||||
return server.RespondJSON(w, map[string]string{
|
||||
"message": "Something went wrong.",
|
||||
}, 500, err)
|
||||
return s.RespondJSON(w, &response{"Something went wrong.", ""}, http.StatusInternalServerError, err)
|
||||
}
|
||||
|
||||
// Create the file
|
||||
var outfile *os.File
|
||||
if outfile, err = os.Create(conf.Path + r.URL.Path + hdr.Filename); nil != err {
|
||||
return server.RespondJSON(w, map[string]string{
|
||||
"message": "Something went wrong.",
|
||||
}, 500, err)
|
||||
return s.RespondJSON(w, &response{"Something went wrong.", ""}, http.StatusInternalServerError, err)
|
||||
}
|
||||
|
||||
// Copy the file content
|
||||
if _, err = io.Copy(outfile, infile); nil != err {
|
||||
return server.RespondJSON(w, map[string]string{
|
||||
"message": "Something went wrong.",
|
||||
}, 500, err)
|
||||
return s.RespondJSON(w, &response{"Something went wrong.", ""}, http.StatusInternalServerError, err)
|
||||
}
|
||||
|
||||
defer outfile.Close()
|
||||
}
|
||||
}
|
||||
|
||||
return server.RespondJSON(w, nil, 200, nil)
|
||||
return s.RespondJSON(w, nil, http.StatusOK, nil)
|
||||
}
|
||||
|
||||
@@ -30,9 +30,7 @@ func PUT(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
// Check if filename and archetype are specified in
|
||||
// the request
|
||||
if _, ok := info["filename"]; !ok {
|
||||
return server.RespondJSON(w, map[string]string{
|
||||
"message": "Filename not specified.",
|
||||
}, 400, nil)
|
||||
return server.RespondJSON(w, &response{"Filename not specified.", ""}, http.StatusBadRequest, nil)
|
||||
}
|
||||
|
||||
// Sanitize the file name path
|
||||
@@ -43,12 +41,8 @@ func PUT(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
|
||||
// Renames the file/folder
|
||||
if err := os.Rename(old, new); err != nil {
|
||||
return server.RespondJSON(w, map[string]string{
|
||||
"message": "Something went wrong.",
|
||||
}, 500, err)
|
||||
return server.RespondJSON(w, &response{err.Error(), ""}, http.StatusInternalServerError, err)
|
||||
}
|
||||
|
||||
return server.RespondJSON(w, map[string]string{
|
||||
"message": "File renamed.",
|
||||
}, 200, nil)
|
||||
return server.RespondJSON(w, &response{"File renamed.", ""}, http.StatusOK, nil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user