Skip to content
This repository was archived by the owner on Jan 27, 2025. It is now read-only.

Commit 15b6691

Browse files
authored
add error handling to images api fetch (#8)
1 parent 3bb45ed commit 15b6691

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ You can configure a few things by editing the file `src/config/index.tsx`.
1919
- `DIR` - The directory to watch.
2020
- `MAX_IMAGES` - How many images to request. Default 10.
2121
- `POLL_INTERVAL` - How often should the frontend look for updates. Default 5 seconds.
22+
- `RETRY_INTERVAL` - How frequently should the frontend retry polling on failure.
2223
- `FILE_EXTENSION` - The filetypes supported. Default `png`.
2324
- `FILE_MIME` - The file mime type. Default `image/png`.
2425
- `SHOW_TITLE` - If the title should be overlaid. Default `false`.

src/config/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export const DIR = `D:\\stable-diffusion-webui\\outputs\\txt2img-images`;
22
export const MAX_IMAGES = 10;
33
export const POLL_INTERVAL = 5 * 1000;
4+
export const RETRY_INTERVAL = 10 * 1000;
45
export const FILE_EXTENSION = "png";
56
export const FILE_MIME = "image/png";
67
export const SHOW_TITLE = false;

src/hooks/useImages.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
import { useCallback, useState, useEffect } from "react";
22

3-
import { POLL_INTERVAL } from "@/config";
3+
import { POLL_INTERVAL, RETRY_INTERVAL } from "@/config";
44

55
const useImages = () => {
66
const [images, setImages] = useState<string[]>([]);
77

88
const getImages = useCallback(async () => {
9-
const data = await fetch("/api/images");
10-
const json = await data.json();
11-
setImages(json.images);
12-
setTimeout(() => getImages(), POLL_INTERVAL);
9+
try {
10+
const data = await fetch("/api/images");
11+
const json = await data.json();
12+
setImages(json.images);
13+
setTimeout(() => getImages(), POLL_INTERVAL);
14+
} catch (error) {
15+
console.error(error);
16+
setTimeout(() => {
17+
getImages();
18+
}, RETRY_INTERVAL);
19+
}
1320
}, []);
1421

1522
useEffect(() => {

0 commit comments

Comments
 (0)