diff --git a/src/routes/__tests__/fhir.test.ts b/src/routes/__tests__/fhir.test.ts index 6874769..8ed434d 100644 --- a/src/routes/__tests__/fhir.test.ts +++ b/src/routes/__tests__/fhir.test.ts @@ -1,6 +1,8 @@ import request from 'supertest' import express from 'express' import { router } from '../fhir' +import got from 'got' +import { saveResource } from '../fhir' const app = express() app.use('/', router) @@ -16,5 +18,27 @@ describe('FHIR Routes', () => { expect(response.status).toBe(400) expect(response.body).toEqual({ message: 'Invalid resource type invalid-resource' }) }) +}) -}) \ No newline at end of file +it('should return 500 Internal Server Error when the post request fails', async () => { + const req = { + body: { + + }, + params: { + resourceType: 'Observation', + id: '123', + }, + } + const res = { + status: jest.fn().mockReturnThis(), + json: jest.fn(), + } + + // Mock the post request to fail + jest.spyOn(got, 'post').mockRejectedValue(new Error('Post request failed')) + + await saveResource(req, res) + + expect(res.status).toHaveBeenCalledWith(400) +}) diff --git a/src/routes/fhir.ts b/src/routes/fhir.ts index 04da619..6ccdd8c 100644 --- a/src/routes/fhir.ts +++ b/src/routes/fhir.ts @@ -113,18 +113,18 @@ router.post('/', async (req, res) => { }) // Create resource -router.post('/:resourceType', (req, res) => { +router.post('/:resourceType', (req: any, res: any) => { saveResource(req, res) }) // Update resource -router.put('/:resourceType/:id', (req, res) => { +router.put('/:resourceType/:id', (req: any, res: any) => { saveResource(req, res) }) /** Helpers */ -async function saveResource(req: any, res: any) { +export async function saveResource(req: any, res: any, operation?: string) { const resource = req.body const resourceType = req.params.resourceType const id = req.params.id @@ -133,13 +133,42 @@ async function saveResource(req: any, res: any) { } logger.info('Received a request to add resource type ' + resourceType + ' with id ' + id) + + let ret, uri, errorFromHapi + try { + if (req.method === 'POST') { + uri = config.get('fhirServer:baseURL') + '/' + getResourceTypeEnum(resourceType).toString() + } else if (req.method === 'PUT') { + uri = config.get('fhirServer:baseURL') + '/' + getResourceTypeEnum(resourceType).toString() + '/' + id + } else { + // Invalid request method + res.status(400).json({ error: 'Invalid request method' }) + return + } - const ret = await got.post( - config.get('fhirServer:baseURL') + '/' + getResourceTypeEnum(resourceType).toString, - { json: resource }, - ) - - res.status(ret.statusCode).json(ret.body) + // Perform request + logger.info('Sending ' + req.method + ' request to ' + uri) + ret = await got({ + method: req.method, + url: uri, + json: resource, + hooks: { + beforeError: [ + error => { + if (error.response) { + logger.error('Error response from FHIR server: ' + JSON.stringify(error.response.body)) + errorFromHapi = JSON.parse(error.response.body as string) + } + return error + }, + ], + }, + }); + + res.status(ret.statusCode).json(JSON.parse(ret.body)) + } catch (error) { + res.status(500).json(errorFromHapi || error) + } } - + export default router diff --git a/src/server/shrMediator.ts b/src/server/shrMediator.ts index 4fac110..f80a87d 100644 --- a/src/server/shrMediator.ts +++ b/src/server/shrMediator.ts @@ -42,10 +42,9 @@ export class ShrMediator { errorTypes.map(type => { process.on(type, async e => { try { - logger.error(`process.on ${type}`) + logger.error(`Caught error: process.on ${type}`) logger.error(e) - await WorkflowHandler.shutdownKafkaProducer() - process.exit(0) + logger.error(e.stack) } catch (_) { process.exit(1) }