mirror of
https://codeberg.org/git-pages/git-pages.git
synced 2026-05-14 03:01:48 +00:00
Wrap errors when calling fmt.Errorf.
This commit is contained in:
@@ -72,7 +72,7 @@ func Authorize(w http.ResponseWriter, r *http.Request) error {
|
||||
actualChallenges, err := net.LookupTXT(challengeHostname)
|
||||
if err != nil {
|
||||
http.Error(w, "failed to look up DNS challenge", http.StatusUnauthorized)
|
||||
return fmt.Errorf("failed to look up %s: %s", challengeHostname, err)
|
||||
return fmt.Errorf("failed to look up %s: %w", challengeHostname, err)
|
||||
}
|
||||
|
||||
expectedChallenge := fmt.Sprintf("%x", sha256.Sum256(fmt.Appendf(nil, "%s %s", host, param)))
|
||||
|
||||
@@ -72,12 +72,12 @@ func maybeCreateOpenRoot(dir string, name string) (*os.Root, error) {
|
||||
dirName := filepath.Join(dir, name)
|
||||
|
||||
if err := os.Mkdir(dirName, 0o755); err != nil && !errors.Is(err, os.ErrExist) {
|
||||
return nil, fmt.Errorf("mkdir: %s", err)
|
||||
return nil, fmt.Errorf("mkdir: %w", err)
|
||||
}
|
||||
|
||||
root, err := os.OpenRoot(dirName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open: %s", err)
|
||||
return nil, fmt.Errorf("open: %w", err)
|
||||
}
|
||||
|
||||
return root, nil
|
||||
@@ -86,17 +86,17 @@ func maybeCreateOpenRoot(dir string, name string) (*os.Root, error) {
|
||||
func createTempInRoot(root *os.Root, name string, data []byte) (string, error) {
|
||||
tempFile, err := os.CreateTemp(root.Name(), name)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("mktemp: %s", err)
|
||||
return "", fmt.Errorf("mktemp: %w", err)
|
||||
}
|
||||
_, err = tempFile.Write(data)
|
||||
tempFile.Close()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("write: %s", err)
|
||||
return "", fmt.Errorf("write: %w", err)
|
||||
}
|
||||
|
||||
tempPath, err := filepath.Rel(root.Name(), tempFile.Name())
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("relpath: %s", err)
|
||||
return "", fmt.Errorf("relpath: %w", err)
|
||||
}
|
||||
|
||||
return tempPath, nil
|
||||
@@ -105,11 +105,11 @@ func createTempInRoot(root *os.Root, name string, data []byte) (string, error) {
|
||||
func NewFSBackend(dir string) (*FSBackend, error) {
|
||||
blobRoot, err := maybeCreateOpenRoot(dir, "blob")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("blob: %s", err)
|
||||
return nil, fmt.Errorf("blob: %w", err)
|
||||
}
|
||||
siteRoot, err := maybeCreateOpenRoot(dir, "site")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("site: %s", err)
|
||||
return nil, fmt.Errorf("site: %w", err)
|
||||
}
|
||||
return &FSBackend{blobRoot, siteRoot}, nil
|
||||
}
|
||||
@@ -122,11 +122,11 @@ func (fs *FSBackend) GetBlob(name string) (io.ReadSeeker, time.Time, error) {
|
||||
blobPath := filepath.Join(splitBlobName(name)...)
|
||||
stat, err := fs.blobRoot.Stat(blobPath)
|
||||
if err != nil {
|
||||
return nil, time.Time{}, fmt.Errorf("stat: %s", err)
|
||||
return nil, time.Time{}, fmt.Errorf("stat: %w", err)
|
||||
}
|
||||
file, err := fs.blobRoot.Open(blobPath)
|
||||
if err != nil {
|
||||
return nil, time.Time{}, fmt.Errorf("open: %s", err)
|
||||
return nil, time.Time{}, fmt.Errorf("open: %w", err)
|
||||
}
|
||||
return file, stat.ModTime(), nil
|
||||
}
|
||||
@@ -141,15 +141,15 @@ func (fs *FSBackend) PutBlob(name string, data []byte) error {
|
||||
}
|
||||
|
||||
if err := fs.blobRoot.Chmod(tempPath, 0o444); err != nil {
|
||||
return fmt.Errorf("chmod: %s", err)
|
||||
return fmt.Errorf("chmod: %w", err)
|
||||
}
|
||||
|
||||
if err := fs.blobRoot.MkdirAll(blobDir, 0o755); err != nil {
|
||||
return fmt.Errorf("mkdir: %s", err)
|
||||
return fmt.Errorf("mkdir: %w", err)
|
||||
}
|
||||
|
||||
if err := fs.blobRoot.Rename(tempPath, blobPath); err != nil {
|
||||
return fmt.Errorf("rename: %s", err)
|
||||
return fmt.Errorf("rename: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -182,7 +182,7 @@ func (fs *FSBackend) StageManifest(manifest *Manifest) error {
|
||||
}
|
||||
|
||||
if err := fs.siteRoot.Rename(tempPath, stagedManifestName(manifestData)); err != nil {
|
||||
return fmt.Errorf("rename: %s", err)
|
||||
return fmt.Errorf("rename: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -197,11 +197,11 @@ func (fs *FSBackend) CommitManifest(name string, manifest *Manifest) error {
|
||||
}
|
||||
|
||||
if err := fs.siteRoot.MkdirAll(filepath.Dir(name), 0o755); err != nil {
|
||||
return fmt.Errorf("mkdir: %s", err)
|
||||
return fmt.Errorf("mkdir: %w", err)
|
||||
}
|
||||
|
||||
if err := fs.siteRoot.Rename(manifestHashName, name); err != nil {
|
||||
return fmt.Errorf("rename: %s", err)
|
||||
return fmt.Errorf("rename: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -250,7 +250,7 @@ func defaultCacheConfig[K comparable, V any](
|
||||
if config.MaxAge != "" {
|
||||
maxAge, err = time.ParseDuration(config.MaxAge)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("max-age: %s", err)
|
||||
return nil, fmt.Errorf("max-age: %w", err)
|
||||
}
|
||||
}
|
||||
if config.MaxSize != 0 {
|
||||
|
||||
16
src/fetch.go
16
src/fetch.go
@@ -23,22 +23,22 @@ func FetchRepository(repoURL string, branch string) (*Manifest, error) {
|
||||
Tags: git.NoTags,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("git clone: %s", err)
|
||||
return nil, fmt.Errorf("git clone: %w", err)
|
||||
}
|
||||
|
||||
ref, err := repo.Head()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("git head: %s", err)
|
||||
return nil, fmt.Errorf("git head: %w", err)
|
||||
}
|
||||
|
||||
commit, err := repo.CommitObject(ref.Hash())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("git commit: %s", err)
|
||||
return nil, fmt.Errorf("git commit: %w", err)
|
||||
}
|
||||
|
||||
tree, err := repo.TreeObject(commit.TreeHash)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("git tree: %s", err)
|
||||
return nil, fmt.Errorf("git tree: %w", err)
|
||||
}
|
||||
|
||||
walker := object.NewTreeWalker(tree, true, make(map[plumbing.Hash]bool))
|
||||
@@ -56,23 +56,23 @@ func FetchRepository(repoURL string, branch string) (*Manifest, error) {
|
||||
if err == io.EOF {
|
||||
break
|
||||
} else if err != nil {
|
||||
return nil, fmt.Errorf("git walker: %s", err)
|
||||
return nil, fmt.Errorf("git walker: %w", err)
|
||||
} else {
|
||||
manifestEntry := Entry{}
|
||||
if entry.Mode.IsFile() {
|
||||
blob, err := repo.BlobObject(entry.Hash)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("git blob %s: %s", name, err)
|
||||
return nil, fmt.Errorf("git blob %s: %w", name, err)
|
||||
}
|
||||
|
||||
reader, err := blob.Reader()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("git blob open: %s", err)
|
||||
return nil, fmt.Errorf("git blob open: %w", err)
|
||||
}
|
||||
|
||||
data, err := io.ReadAll(reader)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("git blob read: %s", err)
|
||||
return nil, fmt.Errorf("git blob read: %w", err)
|
||||
}
|
||||
|
||||
if entry.Mode == filemode.Symlink {
|
||||
|
||||
@@ -111,7 +111,7 @@ func StoreManifest(backend Backend, name string, manifest *Manifest) (*Manifest,
|
||||
}
|
||||
|
||||
if err := backend.StageManifest(extManifest); err != nil {
|
||||
return nil, fmt.Errorf("stage: %s", err)
|
||||
return nil, fmt.Errorf("stage: %w", err)
|
||||
}
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
@@ -121,7 +121,7 @@ func StoreManifest(backend Backend, name string, manifest *Manifest) (*Manifest,
|
||||
wg.Go(func() {
|
||||
err := backend.PutBlob(string(entry.Data), manifest.Tree[name].Data)
|
||||
if err != nil {
|
||||
ch <- fmt.Errorf("put blob %s: %s", name, err)
|
||||
ch <- fmt.Errorf("put blob %s: %w", name, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -133,7 +133,7 @@ func StoreManifest(backend Backend, name string, manifest *Manifest) (*Manifest,
|
||||
}
|
||||
|
||||
if err := backend.CommitManifest(name, extManifest); err != nil {
|
||||
return nil, fmt.Errorf("commit: %s", err)
|
||||
return nil, fmt.Errorf("commit: %w", err)
|
||||
}
|
||||
|
||||
return extManifest, nil
|
||||
|
||||
@@ -164,7 +164,7 @@ func putPage(w http.ResponseWriter, r *http.Request) error {
|
||||
|
||||
requestBody, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("body read: %s", err)
|
||||
return fmt.Errorf("body read: %w", err)
|
||||
}
|
||||
|
||||
// request body contains git repository URL `https://codeberg.org/...`
|
||||
@@ -257,7 +257,7 @@ func postPage(w http.ResponseWriter, r *http.Request) error {
|
||||
|
||||
requestBody, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("body read: %s", err)
|
||||
return fmt.Errorf("body read: %w", err)
|
||||
}
|
||||
|
||||
var event map[string]any
|
||||
|
||||
Reference in New Issue
Block a user