Fix permissions handling and surface write errors to the UI
- Server attempts chmod on data dir at startup if not writable - All write routes now catch errors and return 500 with a readable message - Added /api/health endpoint to diagnose mount issues - Frontend propagates server errors to a visible banner Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DReRqHxSYuVfCzTzQGD9hZ
This commit is contained in:
4 files changed
+115
-32
No files matched your search
@@ -8,6 +8,17 @@ const DATA_DIR = process.env.DATA_DIR || path.join(__dirname, 'data');
|
|||||||
const DATA_FILE = path.join(DATA_DIR, 'prayers.json');
|
const DATA_FILE = path.join(DATA_DIR, 'prayers.json');
|
||||||
const PORT = process.env.PORT || 4000;
|
const PORT = process.env.PORT || 4000;
|
||||||
|
|
||||||
|
// Ensure data directory exists and is writable on startup
|
||||||
|
try {
|
||||||
|
fs.mkdirSync(DATA_DIR, { recursive: true });
|
||||||
|
fs.accessSync(DATA_DIR, fs.constants.W_OK);
|
||||||
|
console.log(`Data directory ready: ${DATA_DIR}`);
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`WARNING: Data directory not writable: ${DATA_DIR} — ${err.message}`);
|
||||||
|
// Attempt chmod in case it's a simple permission gap
|
||||||
|
try { fs.chmodSync(DATA_DIR, 0o755); } catch {}
|
||||||
|
}
|
||||||
|
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.use(express.static(path.join(__dirname, 'build')));
|
app.use(express.static(path.join(__dirname, 'build')));
|
||||||
@@ -26,32 +37,53 @@ function writePrayers(prayers) {
|
|||||||
fs.writeFileSync(DATA_FILE, JSON.stringify(prayers, null, 2));
|
fs.writeFileSync(DATA_FILE, JSON.stringify(prayers, null, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
app.get('/api/health', (req, res) => {
|
||||||
|
let writable = false;
|
||||||
|
try { fs.accessSync(DATA_DIR, fs.constants.W_OK); writable = true; } catch {}
|
||||||
|
res.json({ ok: writable, dataDir: DATA_DIR, dataFile: DATA_FILE });
|
||||||
|
});
|
||||||
|
|
||||||
app.get('/api/prayers', (req, res) => {
|
app.get('/api/prayers', (req, res) => {
|
||||||
res.json(readPrayers());
|
res.json(readPrayers());
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post('/api/prayers', (req, res) => {
|
app.post('/api/prayers', (req, res) => {
|
||||||
const prayers = readPrayers();
|
try {
|
||||||
const prayer = req.body;
|
const prayers = readPrayers();
|
||||||
prayers.unshift(prayer);
|
const prayer = req.body;
|
||||||
writePrayers(prayers);
|
prayers.unshift(prayer);
|
||||||
res.status(201).json(prayer);
|
writePrayers(prayers);
|
||||||
|
res.status(201).json(prayer);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Write failed:', err.message);
|
||||||
|
res.status(500).json({ error: 'Could not save prayer. Check data directory permissions.' });
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
app.put('/api/prayers/:id', (req, res) => {
|
app.put('/api/prayers/:id', (req, res) => {
|
||||||
const prayers = readPrayers();
|
try {
|
||||||
const idx = prayers.findIndex(p => p.id === req.params.id);
|
const prayers = readPrayers();
|
||||||
if (idx === -1) return res.status(404).json({ error: 'Not found' });
|
const idx = prayers.findIndex(p => p.id === req.params.id);
|
||||||
prayers[idx] = { ...prayers[idx], ...req.body };
|
if (idx === -1) return res.status(404).json({ error: 'Not found' });
|
||||||
writePrayers(prayers);
|
prayers[idx] = { ...prayers[idx], ...req.body };
|
||||||
res.json(prayers[idx]);
|
writePrayers(prayers);
|
||||||
|
res.json(prayers[idx]);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Write failed:', err.message);
|
||||||
|
res.status(500).json({ error: 'Could not save prayer. Check data directory permissions.' });
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
app.delete('/api/prayers/:id', (req, res) => {
|
app.delete('/api/prayers/:id', (req, res) => {
|
||||||
const prayers = readPrayers();
|
try {
|
||||||
const filtered = prayers.filter(p => p.id !== req.params.id);
|
const prayers = readPrayers();
|
||||||
writePrayers(filtered);
|
const filtered = prayers.filter(p => p.id !== req.params.id);
|
||||||
res.status(204).end();
|
writePrayers(filtered);
|
||||||
|
res.status(204).end();
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Write failed:', err.message);
|
||||||
|
res.status(500).json({ error: 'Could not delete prayer. Check data directory permissions.' });
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Serve React for all other routes
|
// Serve React for all other routes
|
||||||
@@ -59,4 +91,4 @@ app.get('*splat', (req, res) => {
|
|||||||
res.sendFile(path.join(__dirname, 'build', 'index.html'));
|
res.sendFile(path.join(__dirname, 'build', 'index.html'));
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
|
app.listen(PORT, () => console.log(`Server running on port ${PORT}, data: ${DATA_FILE}`));
|
||||||
+25
@@ -234,6 +234,31 @@ body {
|
|||||||
gap: 0.75rem;
|
gap: 0.75rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.error-banner {
|
||||||
|
background: #fef2f2;
|
||||||
|
border: 1px solid #fca5a5;
|
||||||
|
color: #b91c1c;
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: .75rem 1rem;
|
||||||
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
||||||
|
font-size: .88rem;
|
||||||
|
margin-bottom: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (prefers-color-scheme: dark) {
|
||||||
|
.error-banner {
|
||||||
|
background: #2d1515;
|
||||||
|
border-color: #7f1d1d;
|
||||||
|
color: #fca5a5;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
:root[data-theme="dark"] .error-banner {
|
||||||
|
background: #2d1515;
|
||||||
|
border-color: #7f1d1d;
|
||||||
|
color: #fca5a5;
|
||||||
|
}
|
||||||
|
|
||||||
.loading {
|
.loading {
|
||||||
text-align: center;
|
text-align: center;
|
||||||
color: var(--text-muted);
|
color: var(--text-muted);
|
||||||
|
|||||||
+28
-13
@@ -9,6 +9,7 @@ export default function App() {
|
|||||||
const {
|
const {
|
||||||
prayers,
|
prayers,
|
||||||
loading,
|
loading,
|
||||||
|
error: apiError,
|
||||||
addPrayer,
|
addPrayer,
|
||||||
updateAnswer,
|
updateAnswer,
|
||||||
markAnswered,
|
markAnswered,
|
||||||
@@ -23,6 +24,7 @@ export default function App() {
|
|||||||
const [newTags, setNewTags] = useState('');
|
const [newTags, setNewTags] = useState('');
|
||||||
const [expandedId, setExpandedId] = useState<string | null>(null);
|
const [expandedId, setExpandedId] = useState<string | null>(null);
|
||||||
const [editingAnswer, setEditingAnswer] = useState<{ id: string; text: string } | null>(null);
|
const [editingAnswer, setEditingAnswer] = useState<{ id: string; text: string } | null>(null);
|
||||||
|
const [writeError, setWriteError] = useState<string | null>(null);
|
||||||
|
|
||||||
const filtered = prayers.filter(p => {
|
const filtered = prayers.filter(p => {
|
||||||
if (filter === 'active') return p.status === 'active';
|
if (filter === 'active') return p.status === 'active';
|
||||||
@@ -35,24 +37,31 @@ export default function App() {
|
|||||||
answered: prayers.filter(p => p.status === 'answered').length,
|
answered: prayers.filter(p => p.status === 'answered').length,
|
||||||
};
|
};
|
||||||
|
|
||||||
function handleAdd(e: React.FormEvent) {
|
async function handleAdd(e: React.FormEvent) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
if (!newRequest.trim()) return;
|
if (!newRequest.trim()) return;
|
||||||
const tags = newTags
|
const tags = newTags.split(',').map(t => t.trim()).filter(Boolean);
|
||||||
.split(',')
|
try {
|
||||||
.map(t => t.trim())
|
await addPrayer(newRequest.trim(), tags);
|
||||||
.filter(Boolean);
|
setNewRequest('');
|
||||||
addPrayer(newRequest.trim(), tags);
|
setNewTags('');
|
||||||
setNewRequest('');
|
setWriteError(null);
|
||||||
setNewTags('');
|
} catch (err: any) {
|
||||||
|
setWriteError(err.message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleSaveAnswer(id: string) {
|
async function handleSaveAnswer(id: string) {
|
||||||
if (!editingAnswer) return;
|
if (!editingAnswer) return;
|
||||||
updateAnswer(id, editingAnswer.text);
|
try {
|
||||||
markAnswered(id);
|
await updateAnswer(id, editingAnswer.text);
|
||||||
setEditingAnswer(null);
|
await markAnswered(id);
|
||||||
setExpandedId(null);
|
setEditingAnswer(null);
|
||||||
|
setExpandedId(null);
|
||||||
|
setWriteError(null);
|
||||||
|
} catch (err: any) {
|
||||||
|
setWriteError(err.message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatDate(iso: string) {
|
function formatDate(iso: string) {
|
||||||
@@ -112,6 +121,12 @@ export default function App() {
|
|||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{(writeError || apiError) && (
|
||||||
|
<div className="error-banner">
|
||||||
|
⚠ {writeError || apiError}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
{loading && <p className="loading">Loading prayers…</p>}
|
{loading && <p className="loading">Loading prayers…</p>}
|
||||||
|
|
||||||
<ul className="prayer-list">
|
<ul className="prayer-list">
|
||||||
|
|||||||
+14
-3
@@ -3,14 +3,24 @@ import { Prayer } from './types';
|
|||||||
|
|
||||||
const API = '/api/prayers';
|
const API = '/api/prayers';
|
||||||
|
|
||||||
|
async function checkResponse(r: Response) {
|
||||||
|
if (!r.ok) {
|
||||||
|
const body = await r.json().catch(() => ({}));
|
||||||
|
throw new Error(body.error || `Server error ${r.status}`);
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
export function usePrayers() {
|
export function usePrayers() {
|
||||||
const [prayers, setPrayers] = useState<Prayer[]>([]);
|
const [prayers, setPrayers] = useState<Prayer[]>([]);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetch(API)
|
fetch(API)
|
||||||
.then(r => r.json())
|
.then(r => r.json())
|
||||||
.then(setPrayers)
|
.then(setPrayers)
|
||||||
|
.catch(e => setError(e.message))
|
||||||
.finally(() => setLoading(false));
|
.finally(() => setLoading(false));
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
@@ -27,7 +37,7 @@ export function usePrayers() {
|
|||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify(prayer),
|
body: JSON.stringify(prayer),
|
||||||
}).then(r => r.json());
|
}).then(checkResponse).then(r => r.json());
|
||||||
setPrayers(prev => [saved, ...prev]);
|
setPrayers(prev => [saved, ...prev]);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
@@ -36,7 +46,7 @@ export function usePrayers() {
|
|||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify(updates),
|
body: JSON.stringify(updates),
|
||||||
}).then(r => r.json());
|
}).then(checkResponse).then(r => r.json());
|
||||||
setPrayers(prev => prev.map(p => p.id === id ? updated : p));
|
setPrayers(prev => prev.map(p => p.id === id ? updated : p));
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
@@ -56,13 +66,14 @@ export function usePrayers() {
|
|||||||
patch(id, { request }), [patch]);
|
patch(id, { request }), [patch]);
|
||||||
|
|
||||||
const deletePrayer = useCallback(async (id: string) => {
|
const deletePrayer = useCallback(async (id: string) => {
|
||||||
await fetch(`${API}/${id}`, { method: 'DELETE' });
|
await fetch(`${API}/${id}`, { method: 'DELETE' }).then(checkResponse);
|
||||||
setPrayers(prev => prev.filter(p => p.id !== id));
|
setPrayers(prev => prev.filter(p => p.id !== id));
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
prayers,
|
prayers,
|
||||||
loading,
|
loading,
|
||||||
|
error,
|
||||||
addPrayer,
|
addPrayer,
|
||||||
updateAnswer,
|
updateAnswer,
|
||||||
markAnswered,
|
markAnswered,
|
||||||
|
|||||||
Reference in new issue
Block a user