Skip to content

Commit

Permalink
fix: Throwing errors for non existing entities
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Oct 5, 2020
1 parent 9030616 commit a48e7ef
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
17 changes: 17 additions & 0 deletions __tests__/shared/verifiableData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,22 @@ export default (testContext: {
const presentationCount = await agent.dataStoreORMGetVerifiablePresentationsCount()
expect(allPresentations.length).toEqual(presentationCount)
})

it('should throw error for non existing verifiable credential', async () => {
await expect(
agent.dataStoreGetVerifiableCredential({
hash: 'foobar',
}),
).rejects.toThrow('Verifiable credential not found')
})

it('should throw error for non existing verifiable presentation', async () => {
await expect(
agent.dataStoreGetVerifiablePresentation({
hash: 'foobar',
}),
).rejects.toThrow('Verifiable presentation not found')
})

})
}
34 changes: 24 additions & 10 deletions packages/daf-typeorm/src/data-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@ export class DataStore implements IAgentPlugin {
}

async dataStoreGetMessage(args: IDataStoreGetMessageArgs): Promise<IMessage> {
const messageEntity = await (await this.dbConnection).getRepository(Message).findOneOrFail(args.id)
return createMessage(messageEntity)
try {
const messageEntity = await (await this.dbConnection).getRepository(Message).findOneOrFail(args.id, {
relations: ['credentials', 'presentations']
})
return createMessage(messageEntity)
} catch (e) {
throw Error('Message not found')
}
}

async dataStoreSaveVerifiableCredential(args: IDataStoreSaveVerifiableCredentialArgs): Promise<string> {
Expand All @@ -55,10 +61,14 @@ export class DataStore implements IAgentPlugin {
async dataStoreGetVerifiableCredential(
args: IDataStoreGetVerifiableCredentialArgs,
): Promise<VerifiableCredential> {
const credentialEntity = await (await this.dbConnection)
.getRepository(Credential)
.findOneOrFail(args.hash)
return credentialEntity.raw
try {
const credentialEntity = await (await this.dbConnection)
.getRepository(Credential)
.findOneOrFail(args.hash)
return credentialEntity.raw
} catch (e) {
throw Error('Verifiable credential not found')
}
}

async dataStoreSaveVerifiablePresentation(args: IDataStoreSaveVerifiablePresentationArgs): Promise<string> {
Expand All @@ -71,9 +81,13 @@ export class DataStore implements IAgentPlugin {
async dataStoreGetVerifiablePresentation(
args: IDataStoreGetVerifiablePresentationArgs,
): Promise<VerifiablePresentation> {
const presentationEntity = await (await this.dbConnection)
.getRepository(Presentation)
.findOneOrFail(args.hash)
return presentationEntity.raw
try {
const presentationEntity = await (await this.dbConnection)
.getRepository(Presentation)
.findOneOrFail(args.hash)
return presentationEntity.raw
} catch (e) {
throw Error('Verifiable presentation not found')
}
}
}

0 comments on commit a48e7ef

Please sign in to comment.