fix(web): fix panic on nil file info (#8907)

fix panic on nil file info
This commit is contained in:
Vibhu Pandey 2025-08-25 09:01:32 +05:30 committed by GitHub
parent b21db878e8
commit ee19f1749b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 8 deletions

View File

@ -72,17 +72,22 @@ func (provider *provider) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// check whether a file exists or is a directory at the given path
fi, err := os.Stat(path)
if os.IsNotExist(err) || fi.IsDir() {
// file does not exist or path is a directory, serve index.html
http.ServeFile(rw, req, filepath.Join(provider.config.Directory, indexFileName))
if err != nil {
// if the file doesn't exist, serve index.html
if os.IsNotExist(err) {
http.ServeFile(rw, req, filepath.Join(provider.config.Directory, indexFileName))
return
}
// if we got an error (that wasn't that the file doesn't exist) stating the
// file, return a 500 internal server error and stop
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
if err != nil {
// if we got an error (that wasn't that the file doesn't exist) stating the
// file, return a 500 internal server error and stop
// TODO: Put down a crash html page here
http.Error(rw, err.Error(), http.StatusInternalServerError)
if fi.IsDir() {
// path is a directory, serve index.html
http.ServeFile(rw, req, filepath.Join(provider.config.Directory, indexFileName))
return
}

View File

@ -61,6 +61,10 @@ func TestServeHttpWithoutPrefix(t *testing.T) {
name: "DoesNotExist",
path: "/does-not-exist",
},
{
name: "Directory",
path: "/assets",
},
}
for _, tc := range testCases {
@ -130,6 +134,11 @@ func TestServeHttpWithPrefix(t *testing.T) {
path: "/web/does-not-exist",
found: true,
},
{
name: "Directory",
path: "/web/assets",
found: true,
},
{
name: "DoesNotExist",
path: "/does-not-exist",

View File

@ -0,0 +1,3 @@
#root {
background-color: red;
}