Skip to content

Commit

Permalink
Fix save resource bug for puts and return formatting (#103)
Browse files Browse the repository at this point in the history
* Error handling fix

* Fixed major issues on request management for PUTs

* FHIR format fix
  • Loading branch information
pmanko authored Feb 21, 2024
1 parent 65b96d6 commit 48d5346
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 14 deletions.
26 changes: 25 additions & 1 deletion src/routes/__tests__/fhir.test.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -16,5 +18,27 @@ describe('FHIR Routes', () => {
expect(response.status).toBe(400)
expect(response.body).toEqual({ message: 'Invalid resource type invalid-resource' })
})
})

})
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)
})
49 changes: 39 additions & 10 deletions src/routes/fhir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
5 changes: 2 additions & 3 deletions src/server/shrMediator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 48d5346

Please sign in to comment.