Improve: error handling and data fetching in PastePanel and useData hooks

This commit is contained in:
2026-03-16 01:20:01 +01:00
parent 69140e6da1
commit f29d82f311
3 changed files with 43 additions and 24 deletions

View File

@@ -60,19 +60,35 @@ const PastePanel = ({ onSubmit, publicPastes }) => {
? `https://api.miarma.net/v2/mpaste/pastes/${key}`
: `http://localhost:8081/v2/mpaste/pastes/${key}`;
const data = await getData(url, { password: pwd }, false);
const headers = pwd ? { "X-Paste-Password": pwd } : {};
if (!data) return;
try {
const response = await getData(url, null, false, headers);
setSelectedPaste(data);
setFormData({
title: data.title ?? "",
content: data.content ?? "",
syntax: data.syntax || "plaintext",
burnAfter: data.burnAfter || false,
isPrivate: data.isPrivate || false,
password: ""
});
if (response) {
setSelectedPaste(response);
setShowPasswordModal(false);
setFormData({
title: response.title ?? "",
content: response.content ?? "",
syntax: response.syntax || "plaintext",
burnAfter: response.burnAfter || false,
isPrivate: response.isPrivate || false,
password: ""
});
}
} catch (error) {
const status = error?.status ?? error?.response?.status;
if (status === 403) {
setShowPasswordModal(true);
return;
}
if (status === 404) {
setShowPasswordModal(false);
navigate("/", { replace: true });
}
}
};
useEffect(() => { if (pasteKey) fetchPaste(pasteKey); }, [pasteKey]);