Skip to content

Commit 4881397

Browse files
committed
fix(gateway): add onError handlers to admissions/appointments/prescriptions proxies
1 parent 5db1af3 commit 4881397

1 file changed

Lines changed: 41 additions & 3 deletions

File tree

api-gateway/src/app.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,22 @@ app.use(
103103
next();
104104
},
105105
createProxyMiddleware({
106-
target: process.env.PATIENT_SERVICE_URL,
106+
target: process.env.PATIENT_SERVICE_URL || 'http://localhost:3001',
107107
changeOrigin: true,
108108
pathRewrite: (path) => `/admissions${path}`,
109+
on: {
110+
error: (err: any, req: any, res: any) => {
111+
console.error('[Gateway] /admissions proxy error:', err.message);
112+
if (!res.headersSent) {
113+
res
114+
.status(502)
115+
.json({
116+
error: 'Patient service unavailable',
117+
details: err.message,
118+
});
119+
}
120+
},
121+
},
109122
})
110123
);
111124
//Appointment routes (accessible by PATIENT + STAFF + ADMIN)
@@ -125,9 +138,22 @@ app.use(
125138
next();
126139
},
127140
createProxyMiddleware({
128-
target: process.env.PATIENT_SERVICE_URL,
141+
target: process.env.PATIENT_SERVICE_URL || 'http://localhost:3001',
129142
changeOrigin: true,
130143
pathRewrite: (path) => `/appointments${path}`,
144+
on: {
145+
error: (err: any, req: any, res: any) => {
146+
console.error('[Gateway] /appointments proxy error:', err.message);
147+
if (!res.headersSent) {
148+
res
149+
.status(502)
150+
.json({
151+
error: 'Patient service unavailable',
152+
details: err.message,
153+
});
154+
}
155+
},
156+
},
131157
})
132158
);
133159

@@ -138,12 +164,24 @@ app.use(
138164
if (req.user?.sub) {
139165
req.headers['x-user-id'] = String(req.user.sub);
140166
}
167+
if (req.user?.role) req.headers['x-user-role'] = String(req.user.role);
168+
if (req.user?.type) req.headers['x-user-type'] = String(req.user.type);
141169
next();
142170
},
143171
createProxyMiddleware({
144-
target: process.env.STAFF_SERVICE_URL,
172+
target: process.env.STAFF_SERVICE_URL || 'http://localhost:3002',
145173
changeOrigin: true,
146174
pathRewrite: (path) => `/staff/pharmacy${path}`,
175+
on: {
176+
error: (err: any, req: any, res: any) => {
177+
console.error('[Gateway] /prescriptions proxy error:', err.message);
178+
if (!res.headersSent) {
179+
res
180+
.status(502)
181+
.json({ error: 'Staff service unavailable', details: err.message });
182+
}
183+
},
184+
},
147185
})
148186
);
149187

0 commit comments

Comments
 (0)