Skip to content

Commit 3404797

Browse files
authored
fix: map manager crashes on empty maps directory (#280)
* fix: return empty array from ScanMaps when maps dir is missing When the maps directory doesn't exist, ScanMaps returned nil which Go serializes as JSON `null`. The frontend crashed trying to spread null into an array, and the silent catch block redirected to `/` with no console output — making the map manager appear broken. Return `[]MapInfo{}` (JSON `[]`) instead, and log errors before redirecting. * fix: ensure ScanMaps never returns nil slice Initialize maps as []MapInfo{} so an existing-but-empty directory also serializes as JSON [] instead of null. Update test to match. * fix: update handler test to expect [] instead of null
1 parent f4fe70e commit 3404797

File tree

4 files changed

+7
-6
lines changed

4 files changed

+7
-6
lines changed

internal/maptool/scanner.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func ScanMaps(mapsDir string) ([]MapInfo, error) {
4949
entries, err := os.ReadDir(mapsDir)
5050
if err != nil {
5151
if os.IsNotExist(err) {
52-
return nil, nil
52+
return []MapInfo{}, nil
5353
}
5454
return nil, fmt.Errorf("read maps dir: %w", err)
5555
}
@@ -72,7 +72,7 @@ func ScanMaps(mapsDir string) ([]MapInfo, error) {
7272
{"color-relief.pmtiles", "tiles"},
7373
}
7474

75-
var maps []MapInfo
75+
maps := []MapInfo{}
7676
for _, entry := range entries {
7777
if !entry.IsDir() {
7878
continue

internal/maptool/scanner_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func TestScanMaps_EmptyDir(t *testing.T) {
8787
func TestScanMaps_NonExistent(t *testing.T) {
8888
maps, err := ScanMaps("/tmp/nonexistent-dir-12345")
8989
require.NoError(t, err)
90-
assert.Nil(t, maps)
90+
assert.Empty(t, maps)
9191
}
9292

9393
func TestScanMaps_NotADirectory(t *testing.T) {

internal/server/handler_maptool_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ func TestGetMapToolMaps_Empty(t *testing.T) {
8686
require.NoError(t, err)
8787
assert.Equal(t, http.StatusOK, rec.Code)
8888

89-
// Empty maps dir returns null JSON (nil slice)
90-
assert.Equal(t, "null", jsonTrimmed(rec))
89+
// Empty maps dir returns empty JSON array
90+
assert.Equal(t, "[]", jsonTrimmed(rec))
9191
}
9292

9393
func TestGetMapToolMaps_WithMaps(t *testing.T) {

ui/src/pages/map-manager/MapManager.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ export function MapManager(): JSX.Element {
9494
]);
9595
setTools(t);
9696
setMaps(m);
97-
} catch {
97+
} catch (err) {
98+
console.error("Map manager failed to load:", err);
9899
navigate("/", { replace: true });
99100
return;
100101
}

0 commit comments

Comments
 (0)