mirror of
https://github.com/1f349/site-hosting.git
synced 2025-01-21 06:36:33 +00:00
This seems better for the safety of error messages
This commit is contained in:
parent
d655cc7bb4
commit
bda939cb67
@ -9,7 +9,6 @@ import (
|
||||
"fmt"
|
||||
"github.com/1f349/bluebell/database"
|
||||
"github.com/1f349/bluebell/hook"
|
||||
"github.com/1f349/bluebell/logger"
|
||||
"github.com/1f349/bluebell/validation"
|
||||
"github.com/1f349/syncmap"
|
||||
"github.com/dustin/go-humanize"
|
||||
@ -78,7 +77,11 @@ func (h *Handler) Handle(rw http.ResponseWriter, req *http.Request, params httpr
|
||||
|
||||
err = h.extractTarGzUpload(fileData, site, branch)
|
||||
if err != nil {
|
||||
http.Error(rw, fmt.Sprintf("Invalid upload: %s", err), http.StatusBadRequest)
|
||||
if errors.Unwrap(err) == nil {
|
||||
http.Error(rw, fmt.Sprintf("Invalid upload: %s", err), http.StatusBadRequest)
|
||||
} else {
|
||||
http.Error(rw, "Invalid upload: [Internal Server Error]", http.StatusBadRequest)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@ -98,7 +101,7 @@ func (h *Handler) extractTarGzUpload(fileData io.Reader, site, branch string) er
|
||||
|
||||
_, err := h.db.GetSiteByDomain(context.Background(), site)
|
||||
if err != nil {
|
||||
return userSafeErrorf("invalid site: %w", err)
|
||||
return fmt.Errorf("invalid site: %w", err)
|
||||
}
|
||||
|
||||
key := site + "@" + branch
|
||||
@ -121,23 +124,23 @@ func (h *Handler) extractTarGzUpload(fileData io.Reader, site, branch string) er
|
||||
// try the new "old@[...]" and old "@[...].old" paths
|
||||
err = h.storageFs.RemoveAll(siteBranchPath + ".old")
|
||||
if err != nil && !errors.Is(err, fs.ErrNotExist) {
|
||||
return userSafeErrorf("failed to remove old site branch %s: %w", siteBranchPath, err)
|
||||
return fmt.Errorf("failed to remove old site branch %s: %w", siteBranchPath, err)
|
||||
}
|
||||
err = h.storageFs.RemoveAll(siteBranchOldPath)
|
||||
if err != nil && !errors.Is(err, fs.ErrNotExist) {
|
||||
return userSafeErrorf("failed to remove old site branch %s: %w", siteBranchPath, err)
|
||||
return fmt.Errorf("failed to remove old site branch %s: %w", siteBranchPath, err)
|
||||
}
|
||||
|
||||
err = h.storageFs.MkdirAll(siteBranchWorkPath, fs.ModePerm)
|
||||
if err != nil {
|
||||
return userSafeErrorf("failed to make site directory: %w", err)
|
||||
return fmt.Errorf("failed to make site directory: %w", err)
|
||||
}
|
||||
branchFs := afero.NewBasePathFs(h.storageFs, siteBranchWorkPath)
|
||||
|
||||
// decompress gzip wrapper
|
||||
gzipReader, err := gzip.NewReader(fileData)
|
||||
if err != nil {
|
||||
return userSafeErrorf("invalid gzip file: %w", err)
|
||||
return fmt.Errorf("invalid gzip file: %w", err)
|
||||
}
|
||||
|
||||
// parse tar encoding
|
||||
@ -149,12 +152,12 @@ func (h *Handler) extractTarGzUpload(fileData io.Reader, site, branch string) er
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return userSafeErrorf("invalid tar archive: %w", err)
|
||||
return fmt.Errorf("invalid tar archive: %w", err)
|
||||
}
|
||||
|
||||
err = branchFs.MkdirAll(filepath.Dir(next.Name), fs.ModePerm)
|
||||
if err != nil {
|
||||
return userSafeErrorf("failed to make directory tree: %w", err)
|
||||
return fmt.Errorf("failed to make directory tree: %w", err)
|
||||
}
|
||||
|
||||
if next.FileInfo().IsDir() {
|
||||
@ -163,12 +166,12 @@ func (h *Handler) extractTarGzUpload(fileData io.Reader, site, branch string) er
|
||||
|
||||
create, err := branchFs.Create(next.Name)
|
||||
if err != nil {
|
||||
return userSafeErrorf("failed to create output file: '%s': %w", next.Name, err)
|
||||
return fmt.Errorf("failed to create output file: '%s': %w", next.Name, err)
|
||||
}
|
||||
|
||||
_, err = io.Copy(create, tarReader)
|
||||
if err != nil {
|
||||
return userSafeErrorf("failed to copy from archive to output file: '%s': %w", next.Name, err)
|
||||
return fmt.Errorf("failed to copy from archive to output file: '%s': %w", next.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -182,12 +185,12 @@ func (h *Handler) extractTarGzUpload(fileData io.Reader, site, branch string) er
|
||||
|
||||
err = h.storageFs.Rename(siteBranchPath, siteBranchOldPath)
|
||||
if err != nil && !errors.Is(err, fs.ErrNotExist) {
|
||||
return userSafeErrorf("failed to save an old copy of the site: %w", err)
|
||||
return fmt.Errorf("failed to save an old copy of the site: %w", err)
|
||||
}
|
||||
|
||||
err = h.storageFs.Rename(siteBranchWorkPath, siteBranchPath)
|
||||
if err != nil && !errors.Is(err, fs.ErrNotExist) {
|
||||
return userSafeErrorf("failed to save an old copy of the site: %w", err)
|
||||
return fmt.Errorf("failed to save an old copy of the site: %w", err)
|
||||
}
|
||||
|
||||
n := time.Now().UTC()
|
||||
@ -207,16 +210,3 @@ func (h *Handler) extractTarGzUpload(fileData io.Reader, site, branch string) er
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func userSafeErrorf(format string, args ...any) error {
|
||||
logger.Logger.Helper()
|
||||
logger.Logger.Error(fmt.Errorf(format, args))
|
||||
|
||||
for i := range args {
|
||||
if _, ok := args[i].(error); ok {
|
||||
args[i] = "[Internal Server Error]"
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf(format, args)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user