mirror of
https://git.sr.ht/~adnano/go-gemini
synced 2024-11-23 16:52:06 +01:00
fs: Use better error messages
This commit is contained in:
parent
787a2445a8
commit
f86f8efdae
15
fs.go
15
fs.go
@ -2,6 +2,7 @@ package gemini
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
@ -118,14 +119,14 @@ func serveFile(w ResponseWriter, r *Request, fsys fs.FS, name string, redirect b
|
||||
|
||||
f, err := fsys.Open(name)
|
||||
if err != nil {
|
||||
w.WriteHeader(StatusNotFound, "Not found")
|
||||
w.WriteHeader(toGeminiError(err))
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
stat, err := f.Stat()
|
||||
if err != nil {
|
||||
w.WriteHeader(StatusTemporaryFailure, "Temporary failure")
|
||||
w.WriteHeader(toGeminiError(err))
|
||||
return
|
||||
}
|
||||
|
||||
@ -204,3 +205,13 @@ func dirList(w ResponseWriter, f fs.File) {
|
||||
fmt.Fprintln(w, link.String())
|
||||
}
|
||||
}
|
||||
|
||||
func toGeminiError(err error) (status Status, meta string) {
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return StatusNotFound, "Not found"
|
||||
}
|
||||
if errors.Is(err, fs.ErrPermission) {
|
||||
return StatusNotFound, "Forbidden"
|
||||
}
|
||||
return StatusTemporaryFailure, "Internal server error"
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user