Skip to content

Commit

Permalink
Added resource type and duplicate key information to the error details
Browse files Browse the repository at this point in the history
  • Loading branch information
hossenlopp committed Aug 30, 2024
1 parent 464d025 commit 51912ee
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
36 changes: 24 additions & 12 deletions service/src/db/dbOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export async function createResource(data: FhirArtifact, resourceType: string) {
await collection.insertOne(data);
return { id: data.id as string };
} catch (e) {
throw handlePossibleDuplicateKeyError(e);
throw handlePossibleDuplicateKeyError(e, resourceType);
}
}

Expand All @@ -119,7 +119,7 @@ export async function updateResource(id: string, data: FhirArtifact, resourceTyp

return { id, created: false };
} catch (e) {
throw handlePossibleDuplicateKeyError(e);
throw handlePossibleDuplicateKeyError(e, resourceType);
}
}

Expand Down Expand Up @@ -148,14 +148,19 @@ export async function batchInsert(artifacts: FhirArtifact[], action: string) {
await insertSession?.withTransaction(async () => {
for (const artifact of artifacts) {
const collection = await Connection.db.collection(artifact.resourceType);
await collection.insertOne(artifact as any, { session: insertSession });
results.push(artifact);
try {
await collection.insertOne(artifact as any, { session: insertSession });
results.push(artifact);
} catch (e) {
// this needs to be handled here to have access to the resource type
throw handlePossibleDuplicateKeyError(e, artifact.resourceType);
}
}
});
console.log(`Batch ${action} transaction committed.`);
} catch (err) {
console.log(`Batch ${action} transaction failed: ` + err);
error = handlePossibleDuplicateKeyError(err);
error = err;
} finally {
await insertSession?.endSession();
}
Expand All @@ -175,27 +180,34 @@ export async function batchUpdate(artifacts: FhirArtifact[], action: string) {
await updateSession?.withTransaction(async () => {
for (const artifact of artifacts) {
const collection = await Connection.db.collection(artifact.resourceType);
await collection.replaceOne({ id: artifact.id }, artifact);
results.push(artifact);
try {
await collection.replaceOne({ id: artifact.id }, artifact);
results.push(artifact);
} catch (e) {
// this needs to be handled here to have access to the resource type
throw handlePossibleDuplicateKeyError(e, artifact.resourceType);
}
}
});
console.log(`Batch ${action} transaction committed.`);
} catch (err) {
console.log(`Batch ${action} transaction failed: ` + err);
error = handlePossibleDuplicateKeyError(err);
error = err;
} finally {
await updateSession?.endSession();
}
if (error) throw error;
return results;
}

function handlePossibleDuplicateKeyError(e: any) {
function handlePossibleDuplicateKeyError(e: any, resourceType?: string) {
if (e instanceof MongoServerError && e.code === 11000) {
let errorString = 'Resource with primary identifiers already in repository.';
if (e.keyPattern) {
errorString =
'Resource with identifiers (' + Object.keys(e.keyPattern).join(',') + ') already exists in the repository.';
if (e.keyValue) {
const identifiers = Object.entries(e.keyValue).map(pair => `${pair[0]}: ${pair[1]}`);
errorString = `${resourceType ? resourceType + ' ' : ''}Resource with identifiers (${identifiers.join(
', '
)}) already exists in the repository.`;
}
return new BadRequestError(errorString);
}
Expand Down
4 changes: 2 additions & 2 deletions service/test/services/BaseService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ describe('BaseService', () => {
.then(response => {
expect(response.body.issue[0].code).toEqual('invalid');
expect(response.body.issue[0].details.text).toEqual(
'Resource with identifiers (url,version) already exists in the repository.'
'Library Resource with identifiers (url: http://example.com/testActiveLibrary, version: 1) already exists in the repository.'
);
});
});
Expand Down Expand Up @@ -359,7 +359,7 @@ describe('BaseService', () => {
.then(response => {
expect(response.body.issue[0].code).toEqual('invalid');
expect(response.body.issue[0].details.text).toEqual(
'Resource with identifiers (url,version) already exists in the repository.'
'Library Resource with identifiers (url: http://example.com/testActiveLibrary2, version: 1) already exists in the repository.'
);
});
});
Expand Down
8 changes: 4 additions & 4 deletions service/test/services/LibraryService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ describe('LibraryService', () => {
.then(response => {
expect(response.body.issue[0].code).toEqual('invalid');
expect(response.body.issue[0].details.text).toEqual(
'Resource with identifiers (url,version) already exists in the repository.'
'Library Resource with identifiers (url: http://example.com/testActiveLibrary, version: 1) already exists in the repository.'
);
});
});
Expand Down Expand Up @@ -577,7 +577,7 @@ describe('LibraryService', () => {
.then(response => {
expect(response.body.issue[0].code).toEqual('invalid');
expect(response.body.issue[0].details.text).toEqual(
'Resource with identifiers (url,version) already exists in the repository.'
'Library Resource with identifiers (url: http://example.com/testActiveLibrary, version: 1) already exists in the repository.'
);
});
});
Expand Down Expand Up @@ -687,7 +687,7 @@ describe('LibraryService', () => {
.then(response => {
expect(response.body.issue[0].code).toEqual('invalid');
expect(response.body.issue[0].details.text).toEqual(
'Resource with identifiers (url,version) already exists in the repository.'
'Library Resource with identifiers (url: http://parent-library.com, version: 1.0.0.4) already exists in the repository.'
);
});
});
Expand Down Expand Up @@ -754,7 +754,7 @@ describe('LibraryService', () => {
.then(response => {
expect(response.body.issue[0].code).toEqual('invalid');
expect(response.body.issue[0].details.text).toEqual(
'Resource with identifiers (url,version) already exists in the repository.'
'Library Resource with identifiers (url: http://clone-example.com, version: 1.0.0.8) already exists in the repository.'
);
});
});
Expand Down
8 changes: 4 additions & 4 deletions service/test/services/MeasureService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ describe('MeasureService', () => {
.then(response => {
expect(response.body.issue[0].code).toEqual('invalid');
expect(response.body.issue[0].details.text).toEqual(
'Resource with identifiers (url,version) already exists in the repository.'
'Measure Resource with identifiers (url: http://example.com/testActiveMeasure, version: 1) already exists in the repository.'
);
});
});
Expand Down Expand Up @@ -579,7 +579,7 @@ describe('MeasureService', () => {
.then(response => {
expect(response.body.issue[0].code).toEqual('invalid');
expect(response.body.issue[0].details.text).toEqual(
'Resource with identifiers (url,version) already exists in the repository.'
'Measure Resource with identifiers (url: http://example.com/testActiveMeasure, version: 1) already exists in the repository.'
);
});
});
Expand Down Expand Up @@ -692,7 +692,7 @@ describe('MeasureService', () => {
.then(response => {
expect(response.body.issue[0].code).toEqual('invalid');
expect(response.body.issue[0].details.text).toEqual(
'Resource with identifiers (url,version) already exists in the repository.'
'Measure Resource with identifiers (url: http://parent-measure.com, version: 1.0.0.4) already exists in the repository.'
);
});
});
Expand Down Expand Up @@ -759,7 +759,7 @@ describe('MeasureService', () => {
.then(response => {
expect(response.body.issue[0].code).toEqual('invalid');
expect(response.body.issue[0].details.text).toEqual(
'Resource with identifiers (url,version) already exists in the repository.'
'Measure Resource with identifiers (url: http://clone-example.com, version: 1.0.0.8) already exists in the repository.'
);
});
});
Expand Down

0 comments on commit 51912ee

Please sign in to comment.