Skip to content

Commit

Permalink
external_id postgres query error handling (#49)
Browse files Browse the repository at this point in the history
Wrap queries to postgres to return better error messages and not log potentially sensitive data. This only covers calls to the `external_id` table.

Signed-off-by: Jeff Kennedy <[email protected]>
  • Loading branch information
Jeff Kennedy authored Feb 9, 2021
1 parent 01ebe99 commit 9a9a054
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/db/external.id.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,17 @@ export class ExternalIdService {
});

// If we make it this far, we have something to look up
const externalIds: ExternalId[] = await this.externalIdRepository.find({
external_id: In(externalIdValues),
external_id_type: externalIdType
});
let externalIds: ExternalId[] = [];
try {
externalIds = await this.externalIdRepository.find({
external_id: In(externalIdValues),
external_id_type: externalIdType
});
} catch (e) {
throw new ProtocolException(ProtocolErrorCode.NO_CITIZEN_FOUND, `Failed to retrieve a DID for provided IDs ${externalIdType}`);
}
if (externalIds.length === 0) {
throw new ProtocolException(ProtocolErrorCode.NO_CITIZEN_FOUND, `Cannot find a DID for ${externalIdType}: ${idValues}`);
throw new ProtocolException(ProtocolErrorCode.NO_CITIZEN_FOUND, `Cannot find a DID for provided IDs ${externalIdType}`);
}
return externalIds;
}
Expand Down Expand Up @@ -73,7 +78,14 @@ export class ExternalIdService {
externalId2.external_id_type = 'sl_voter_id'; // TODO: update to be specifiable by the filters
externalIds.push(externalId2);
}
return this.externalIdRepository.save(externalIds);
let results: ExternalId[] = [];
try {
results = await this.externalIdRepository.save(externalIds);
} catch (e) {
const msg = externalIds.map((id: ExternalId) => `${id.external_id_type}`).join('; ');
throw new ProtocolException(ProtocolErrorCode.DUPLICATE_ENTRY, `Entry already exists for ${msg}`);
}
return results;
}

private async getOrCreateExternalId(externalId: ExternalId): Promise<ExternalId> {
Expand All @@ -82,7 +94,14 @@ export class ExternalIdService {
external_id: externalId.external_id,
external_id_type: externalId.external_id_type
});
return dbExternalId ?? await this.externalIdRepository.save(externalId);
const save: () => Promise<ExternalId> = async () => {
try {
return await this.externalIdRepository.save(externalId);
} catch (e) {
throw new ProtocolException(ProtocolErrorCode.DUPLICATE_ENTRY, `Entry already exists for ${externalId.external_id_type}`);
}
};
return dbExternalId ?? await save();
}

public async getOrCreateExternalIds(did: string, filters: CreateFiltersDto): Promise<Array<ExternalId>> {
Expand Down

0 comments on commit 9a9a054

Please sign in to comment.