Skip to content

Commit f6e0005

Browse files
committed
Improve file handle error cases
1 parent d7e7c57 commit f6e0005

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

asset/server.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ func (h *Handle[T]) Gen() int {
3939
return int(h.generation.Load())
4040
}
4141

42+
func (h *Handle[T]) SetErr(err error) {
43+
h.err = err
44+
h.done.Store(true)
45+
h.generation.Add(1)
46+
}
47+
4248
func (h *Handle[T]) Set(val *T) {
4349
h.err = nil
4450
h.ptr.Store(val)
@@ -332,7 +338,6 @@ func LoadDir[T any](server *Server, fpath string, recursive bool) []*Handle[T] {
332338
}
333339

334340
dirPath := path.Join(fsys.prefix, fpath, e.Name())
335-
fmt.Println("Directory:", dirPath)
336341
dirHandles := LoadDir[T](server, dirPath, recursive)
337342
ret = append(ret, dirHandles...)
338343
continue
@@ -343,23 +348,6 @@ func LoadDir[T any](server *Server, fpath string, recursive bool) []*Handle[T] {
343348
}
344349

345350
return ret
346-
347-
// fpath = filepath.Clean(fpath)
348-
349-
// dirEntries, err := fs.ReadDir(server.filesystem, fpath)
350-
// if err != nil {
351-
// return nil // TODO!!! : You're just snuffing an error here, which obviously isn't good
352-
// }
353-
354-
// ret := make([]*Handle[T], 0, len(dirEntries))
355-
// for _, e := range dirEntries {
356-
// if e.IsDir() { continue } // TODO: Recursive?
357-
358-
// handle := Load[T](server, filepath.Join(fpath, e.Name()))
359-
// ret = append(ret, handle)
360-
// }
361-
362-
// return ret
363351
}
364352

365353
// Gets the handle, returns true if the handle has already started loading
@@ -391,11 +379,15 @@ func Load[T any](server *Server, name string) *Handle[T] {
391379

392380
anyLoader, ok := server.extToLoader[ext]
393381
if !ok {
394-
panic(fmt.Sprintf("could not find loader for extension: %s (%s)", ext, name))
382+
handle.SetErr(fmt.Errorf("could not find loader for extension: %s (%s)", ext, name))
383+
close(handle.doneChan)
384+
return handle
395385
}
396386
loader, ok := anyLoader.(Loader[T])
397387
if !ok {
398-
panic(fmt.Sprintf("wrong type for registered loader on extension: %s", ext))
388+
handle.SetErr(fmt.Errorf("wrong type for registered loader on extension: %s", ext))
389+
close(handle.doneChan)
390+
return handle
399391
}
400392

401393
go func() {

0 commit comments

Comments
 (0)