From 46eed8bb88c707e4cf94d708ed8e43d332f7a31c Mon Sep 17 00:00:00 2001 From: pranalidhanavade Date: Fri, 28 Jun 2024 17:20:34 +0530 Subject: [PATCH 1/6] refactor: error handling of polygon controller Signed-off-by: pranalidhanavade --- src/controllers/polygon/PolygonController.ts | 53 +++++++------------- 1 file changed, 19 insertions(+), 34 deletions(-) diff --git a/src/controllers/polygon/PolygonController.ts b/src/controllers/polygon/PolygonController.ts index 6c63526f..c028ab4d 100644 --- a/src/controllers/polygon/PolygonController.ts +++ b/src/controllers/polygon/PolygonController.ts @@ -3,11 +3,14 @@ import type { SchemaMetadata } from '../types' import { generateSecp256k1KeyPair } from '@ayanworks/credo-polygon-w3c-module' import { DidOperation } from '@ayanworks/credo-polygon-w3c-module/build/ledger' -import { Agent, CredoError } from '@credo-ts/core' +import { Agent } from '@credo-ts/core' import * as fs from 'fs' import { injectable } from 'tsyringe' -import { Route, Tags, Security, Controller, Post, TsoaResponse, Res, Body, Get, Path } from 'tsoa' +import ErrorHandlingService from '../../errorHandlingService' +import { BadRequestError, UnprocessableEntityError } from '../../errors' + +import { Route, Tags, Security, Controller, Post, Body, Get, Path } from 'tsoa' @Tags('Polygon') @Security('apiKey') @@ -27,7 +30,7 @@ export class Polygon extends Controller { * @returns Secp256k1KeyPair */ @Post('create-keys') - public async createKeyPair(@Res() internalServerError: TsoaResponse<500, { message: string }>): Promise<{ + public async createKeyPair(): Promise<{ privateKey: string publicKeyBase58: string address: string @@ -36,7 +39,7 @@ export class Polygon extends Controller { return await generateSecp256k1KeyPair() } catch (error) { // Handle the error here - return internalServerError(500, { message: `something went wrong: ${error}` }) + throw ErrorHandlingService.handle(error) } } @@ -52,16 +55,12 @@ export class Polygon extends Controller { did: string schemaName: string schema: { [key: string]: any } - }, - @Res() internalServerError: TsoaResponse<500, { message: string }>, - @Res() badRequestError: TsoaResponse<400, { reason: string }> + } ): Promise { try { const { did, schemaName, schema } = createSchemaRequest if (!did || !schemaName || !schema) { - return badRequestError(400, { - reason: `One or more parameters are empty or undefined.`, - }) + throw new BadRequestError('One or more parameters are empty or undefined.') } const schemaResponse = await this.agent.modules.polygon.createSchema({ @@ -72,7 +71,9 @@ export class Polygon extends Controller { if (schemaResponse.schemaState?.state === 'failed') { const reason = schemaResponse.schemaState?.reason?.toLowerCase() if (reason && reason.includes('insufficient') && reason.includes('funds')) { - throw new Error('Insufficient funds to the address, Please add funds to perform this operation') + throw new UnprocessableEntityError( + 'Insufficient funds to the address, Please add funds to perform this operation' + ) } else { throw new Error(schemaResponse.schemaState?.reason) } @@ -84,7 +85,7 @@ export class Polygon extends Controller { } if (!schemaResponse?.schemaId) { - throw new Error('Invalid schema response') + throw new BadRequestError('Invalid schema response') } const schemaPayload: SchemaMetadata = { schemaUrl: configJson.schemaFileServerURL + schemaResponse?.schemaId, @@ -94,7 +95,7 @@ export class Polygon extends Controller { } return schemaPayload } catch (error) { - return internalServerError(500, { message: `something went wrong: ${error}` }) + throw ErrorHandlingService.handle(error) } } @@ -109,17 +110,13 @@ export class Polygon extends Controller { estimateTransactionRequest: { operation: any transaction: any - }, - @Res() internalServerError: TsoaResponse<500, { message: string }>, - @Res() badRequestError: TsoaResponse<400, { reason: string }> + } ): Promise { try { const { operation } = estimateTransactionRequest if (!(operation in DidOperation)) { - return badRequestError(400, { - reason: `Invalid method parameter!`, - }) + throw new BadRequestError('Invalid method parameter!') } if (operation === DidOperation.Create) { return this.agent.modules.polygon.estimateFeeForDidOperation({ operation }) @@ -127,7 +124,7 @@ export class Polygon extends Controller { return this.agent.modules.polygon.estimateFeeForDidOperation({ operation }) } } catch (error) { - return internalServerError(500, { message: `something went wrong: ${error}` }) + throw ErrorHandlingService.handle(error) } } @@ -137,23 +134,11 @@ export class Polygon extends Controller { * @returns Schema Object */ @Get(':did/:schemaId') - public async getSchemaById( - @Path('did') did: string, - @Path('schemaId') schemaId: string, - @Res() internalServerError: TsoaResponse<500, { message: string }>, - @Res() forbiddenError: TsoaResponse<401, { reason: string }> - ): Promise { + public async getSchemaById(@Path('did') did: string, @Path('schemaId') schemaId: string): Promise { try { return this.agent.modules.polygon.getSchemaById(did, schemaId) } catch (error) { - if (error instanceof CredoError) { - if (error.message.includes('UnauthorizedClientRequest')) { - return forbiddenError(401, { - reason: 'this action is not allowed.', - }) - } - } - return internalServerError(500, { message: `something went wrong: ${error}` }) + throw ErrorHandlingService.handle(error) } } } From f216bf73a61418228159d9f3b4ac7c01cc1849e8 Mon Sep 17 00:00:00 2001 From: pranalidhanavade Date: Mon, 1 Jul 2024 17:16:57 +0530 Subject: [PATCH 2/6] refactor: error handling of multi-tenancy-question-answer APIs Signed-off-by: pranalidhanavade --- .../multi-tenancy/MultiTenancyController.ts | 73 +++++++++---------- 1 file changed, 33 insertions(+), 40 deletions(-) diff --git a/src/controllers/multi-tenancy/MultiTenancyController.ts b/src/controllers/multi-tenancy/MultiTenancyController.ts index cf194528..6189b3bf 100644 --- a/src/controllers/multi-tenancy/MultiTenancyController.ts +++ b/src/controllers/multi-tenancy/MultiTenancyController.ts @@ -52,6 +52,8 @@ import axios from 'axios' import * as fs from 'fs' import { CredentialEnum, DidMethod, Network, Role } from '../../enums/enum' +import ErrorHandlingService from '../../errorHandlingService' +import { NotFoundError } from '../../errors' import { BCOVRIN_REGISTER_URL, INDICIO_NYM_URL } from '../../utils/util' import { SchemaId, CredentialDefinitionId, RecordId, ProofRecordExample, ConnectionRecordExample } from '../examples' import { @@ -1350,6 +1352,8 @@ export class MultiTenancyController extends Controller { } } + ////My APIs + @Security('apiKey') @Post('/credentials/create-offer/:tenantId') public async createOffer( @@ -1899,16 +1903,20 @@ export class MultiTenancyController extends Controller { @Query('state') state?: QuestionAnswerState, @Query('threadId') threadId?: string ) { - let questionAnswerRecords: QuestionAnswerRecord[] = [] - await this.agent.modules.tenants.withTenantAgent({ tenantId }, async (tenantAgent) => { - questionAnswerRecords = await tenantAgent.modules.questionAnswer.findAllByQuery({ - connectionId, - role, - state, - threadId, + try { + let questionAnswerRecords: QuestionAnswerRecord[] = [] + await this.agent.modules.tenants.withTenantAgent({ tenantId }, async (tenantAgent) => { + questionAnswerRecords = await tenantAgent.modules.questionAnswer.findAllByQuery({ + connectionId, + role, + state, + threadId, + }) }) - }) - return questionAnswerRecords.map((record) => record.toJSON()) + return questionAnswerRecords.map((record) => record.toJSON()) + } catch (error) { + throw ErrorHandlingService.handle(error) + } } /** @@ -1928,9 +1936,7 @@ export class MultiTenancyController extends Controller { question: string validResponses: ValidResponse[] detail?: string - }, - @Res() notFoundError: TsoaResponse<404, { reason: string }>, - @Res() internalServerError: TsoaResponse<500, { message: string }> + } ) { try { const { question, validResponses, detail } = config @@ -1943,13 +1949,9 @@ export class MultiTenancyController extends Controller { }) questionAnswerRecord = questionAnswerRecord?.toJSON() }) - return questionAnswerRecord } catch (error) { - if (error instanceof RecordNotFoundError) { - return notFoundError(404, { reason: `connection with connection id "${connectionId}" not found.` }) - } - return internalServerError(500, { message: `something went wrong: ${error}` }) + throw ErrorHandlingService.handle(error) } } @@ -1965,9 +1967,7 @@ export class MultiTenancyController extends Controller { public async sendAnswer( @Path('id') id: RecordId, @Path('tenantId') tenantId: string, - @Body() request: Record<'response', string>, - @Res() notFoundError: TsoaResponse<404, { reason: string }>, - @Res() internalServerError: TsoaResponse<500, { message: string }> + @Body() request: Record<'response', string> ) { try { let questionAnswerRecord @@ -1977,10 +1977,7 @@ export class MultiTenancyController extends Controller { }) return questionAnswerRecord } catch (error) { - if (error instanceof RecordNotFoundError) { - return notFoundError(404, { reason: `record with connection id "${id}" not found.` }) - } - return internalServerError(500, { message: `something went wrong: ${error}` }) + throw ErrorHandlingService.handle(error) } } @@ -1993,23 +1990,19 @@ export class MultiTenancyController extends Controller { */ @Security('apiKey') @Get('/question-answer/:id/:tenantId') - public async getQuestionAnswerRecordById( - @Path('id') id: RecordId, - @Path('tenantId') tenantId: string, - @Res() notFoundError: TsoaResponse<404, { reason: string }> - ) { - let questionAnswerRecord - await this.agent.modules.tenants.withTenantAgent({ tenantId }, async (tenantAgent) => { - const record = await tenantAgent.modules.questionAnswer.findById(id) - questionAnswerRecord = record - }) - - if (!questionAnswerRecord) { - return notFoundError(404, { - reason: `Question Answer Record with id "${id}" not found.`, + public async getQuestionAnswerRecordById(@Path('id') id: RecordId, @Path('tenantId') tenantId: string) { + try { + let questionAnswerRecord + await this.agent.modules.tenants.withTenantAgent({ tenantId }, async (tenantAgent) => { + const record = await tenantAgent.modules.questionAnswer.findById(id) + questionAnswerRecord = record }) + if (!questionAnswerRecord) { + throw new NotFoundError(`Question Answer Record with id "${id}" not found.`) + } + return questionAnswerRecord + } catch (error) { + throw ErrorHandlingService.handle(error) } - - return questionAnswerRecord } } From 8948efc1e315899dad19b634068714a599ae712e Mon Sep 17 00:00:00 2001 From: pranalidhanavade Date: Tue, 2 Jul 2024 12:25:25 +0530 Subject: [PATCH 3/6] refactor: error handling for did-key and did-web APIs of multi-tenancy controller Signed-off-by: pranalidhanavade --- .../multi-tenancy/MultiTenancyController.ts | 40 +++++-------------- 1 file changed, 11 insertions(+), 29 deletions(-) diff --git a/src/controllers/multi-tenancy/MultiTenancyController.ts b/src/controllers/multi-tenancy/MultiTenancyController.ts index 6189b3bf..3cc33e78 100644 --- a/src/controllers/multi-tenancy/MultiTenancyController.ts +++ b/src/controllers/multi-tenancy/MultiTenancyController.ts @@ -1778,34 +1778,21 @@ export class MultiTenancyController extends Controller { return internalServerError(500, { message: `something went wrong: ${error}` }) } } - + //done @Security('apiKey') @Delete(':tenantId') - public async deleteTenantById( - @Path('tenantId') tenantId: string, - @Res() notFoundError: TsoaResponse<404, { reason: string }>, - @Res() internalServerError: TsoaResponse<500, { message: string }> - ) { + public async deleteTenantById(@Path('tenantId') tenantId: string) { try { const deleteTenant = await this.agent.modules.tenants.deleteTenantById(tenantId) return JsonTransformer.toJSON(deleteTenant) } catch (error) { - if (error instanceof RecordNotFoundError) { - return notFoundError(404, { - reason: `Tenant with id: ${tenantId} not found.`, - }) - } - return internalServerError(500, { message: `Something went wrong: ${error}` }) + throw ErrorHandlingService.handle(error) } } @Security('apiKey') @Post('/did/web/:tenantId') - public async createDidWeb( - @Path('tenantId') tenantId: string, - @Body() didOptions: DidCreate, - @Res() internalServerError: TsoaResponse<500, { message: string }> - ) { + public async createDidWeb(@Path('tenantId') tenantId: string, @Body() didOptions: DidCreate) { try { let didDoc await this.agent.modules.tenants.withTenantAgent({ tenantId }, async (tenantAgent) => { @@ -1815,6 +1802,9 @@ export class MultiTenancyController extends Controller { if (!didOptions.keyType) { throw Error('keyType is required') } + if (!didOptions.domain) { + throw Error('domain is required') + } if (didOptions.keyType !== KeyType.Ed25519 && didOptions.keyType !== KeyType.Bls12381g2) { throw Error('Only ed25519 and bls12381g2 key type supported') } @@ -1847,24 +1837,18 @@ export class MultiTenancyController extends Controller { }) return didDoc } catch (error) { - return internalServerError(500, { - message: `something went wrong: ${error}`, - }) + throw ErrorHandlingService.handle(error) } } @Security('apiKey') @Post('/did/key:tenantId') - public async createDidKey( - @Path('tenantId') tenantId: string, - @Body() didOptions: DidCreate, - @Res() internalServerError: TsoaResponse<500, { message: string }> - ) { + public async createDidKey(@Path('tenantId') tenantId: string, @Body() didOptions: DidCreate) { try { let didCreateResponse await this.agent.modules.tenants.withTenantAgent({ tenantId }, async (tenantAgent) => { if (!didOptions.seed) { - throw Error('Seed is required') + throw new BadRequestError('Seed is required') } didCreateResponse = await tenantAgent.dids.create({ method: 'key', @@ -1878,9 +1862,7 @@ export class MultiTenancyController extends Controller { }) return didCreateResponse } catch (error) { - return internalServerError(500, { - message: `something went wrong: ${error}`, - }) + throw ErrorHandlingService.handle(error) } } From 0e27d448c9f55a9d8c47843dbc79372da23c51fa Mon Sep 17 00:00:00 2001 From: pranalidhanavade Date: Tue, 2 Jul 2024 15:17:42 +0530 Subject: [PATCH 4/6] resolved: comments on pull request Signed-off-by: pranalidhanavade --- .../multi-tenancy/MultiTenancyController.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/controllers/multi-tenancy/MultiTenancyController.ts b/src/controllers/multi-tenancy/MultiTenancyController.ts index 3cc33e78..9da61abe 100644 --- a/src/controllers/multi-tenancy/MultiTenancyController.ts +++ b/src/controllers/multi-tenancy/MultiTenancyController.ts @@ -53,7 +53,7 @@ import * as fs from 'fs' import { CredentialEnum, DidMethod, Network, Role } from '../../enums/enum' import ErrorHandlingService from '../../errorHandlingService' -import { NotFoundError } from '../../errors' +import { BadRequestError, NotFoundError } from '../../errors' import { BCOVRIN_REGISTER_URL, INDICIO_NYM_URL } from '../../utils/util' import { SchemaId, CredentialDefinitionId, RecordId, ProofRecordExample, ConnectionRecordExample } from '../examples' import { @@ -1352,8 +1352,6 @@ export class MultiTenancyController extends Controller { } } - ////My APIs - @Security('apiKey') @Post('/credentials/create-offer/:tenantId') public async createOffer( @@ -1778,7 +1776,7 @@ export class MultiTenancyController extends Controller { return internalServerError(500, { message: `something went wrong: ${error}` }) } } - //done + @Security('apiKey') @Delete(':tenantId') public async deleteTenantById(@Path('tenantId') tenantId: string) { @@ -1800,10 +1798,10 @@ export class MultiTenancyController extends Controller { throw Error('Seed is required') } if (!didOptions.keyType) { - throw Error('keyType is required') + throw new BadRequestError('keyType is required') } if (!didOptions.domain) { - throw Error('domain is required') + throw new BadRequestError('domain is required') } if (didOptions.keyType !== KeyType.Ed25519 && didOptions.keyType !== KeyType.Bls12381g2) { throw Error('Only ed25519 and bls12381g2 key type supported') @@ -1914,7 +1912,8 @@ export class MultiTenancyController extends Controller { @Path('connectionId') connectionId: RecordId, @Path('tenantId') tenantId: string, @Body() - config: { + config: //TODO type for config + { question: string validResponses: ValidResponse[] detail?: string From d1f0c4cd09c5a7052c9bc174ccb1f29fa382989f Mon Sep 17 00:00:00 2001 From: pranalidhanavade Date: Tue, 2 Jul 2024 15:25:33 +0530 Subject: [PATCH 5/6] resolved: fixed comments on pull request Signed-off-by: pranalidhanavade --- src/controllers/multi-tenancy/MultiTenancyController.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/controllers/multi-tenancy/MultiTenancyController.ts b/src/controllers/multi-tenancy/MultiTenancyController.ts index 9da61abe..f8c6c3a9 100644 --- a/src/controllers/multi-tenancy/MultiTenancyController.ts +++ b/src/controllers/multi-tenancy/MultiTenancyController.ts @@ -1849,6 +1849,7 @@ export class MultiTenancyController extends Controller { throw new BadRequestError('Seed is required') } didCreateResponse = await tenantAgent.dids.create({ + //TODO enum for method method: 'key', options: { keyType: KeyType.Ed25519, From fddb796d6bf1bf87315b05744b9ca2945c682093 Mon Sep 17 00:00:00 2001 From: pranalidhanavade Date: Tue, 2 Jul 2024 19:49:31 +0530 Subject: [PATCH 6/6] refactor: error handling for proof APIs in multi-tenancy controller Signed-off-by: pranalidhanavade --- .../multi-tenancy/MultiTenancyController.ts | 153 +++++------------- 1 file changed, 44 insertions(+), 109 deletions(-) diff --git a/src/controllers/multi-tenancy/MultiTenancyController.ts b/src/controllers/multi-tenancy/MultiTenancyController.ts index f8c6c3a9..5dfd2806 100644 --- a/src/controllers/multi-tenancy/MultiTenancyController.ts +++ b/src/controllers/multi-tenancy/MultiTenancyController.ts @@ -1354,11 +1354,7 @@ export class MultiTenancyController extends Controller { @Security('apiKey') @Post('/credentials/create-offer/:tenantId') - public async createOffer( - @Body() createOfferOptions: CreateOfferOptions, - @Path('tenantId') tenantId: string, - @Res() internalServerError: TsoaResponse<500, { message: string }> - ) { + public async createOffer(@Body() createOfferOptions: CreateOfferOptions, @Path('tenantId') tenantId: string) { let offer try { await this.agent.modules.tenants.withTenantAgent({ tenantId }, async (tenantAgent) => { @@ -1372,17 +1368,13 @@ export class MultiTenancyController extends Controller { return offer } catch (error) { - return internalServerError(500, { message: `something went wrong: ${error}` }) + throw ErrorHandlingService.handle(error) } } @Security('apiKey') @Post('/credentials/create-offer-oob/:tenantId') - public async createOfferOob( - @Path('tenantId') tenantId: string, - @Body() createOfferOptions: CreateOfferOobOptions, - @Res() internalServerError: TsoaResponse<500, { message: string }> - ) { + public async createOfferOob(@Path('tenantId') tenantId: string, @Body() createOfferOptions: CreateOfferOobOptions) { let createOfferOobRecord try { @@ -1447,15 +1439,13 @@ export class MultiTenancyController extends Controller { }) return createOfferOobRecord } catch (error) { - return internalServerError(500, { message: `something went wrong: ${error}` }) + throw ErrorHandlingService.handle(error) } } @Security('apiKey') @Post('/credentials/accept-offer/:tenantId') public async acceptOffer( - @Res() notFoundError: TsoaResponse<404, { reason: string }>, - @Res() internalServerError: TsoaResponse<500, { message: string }>, @Path('tenantId') tenantId: string, @Body() acceptCredentialOfferOptions: AcceptCredentialOfferOptions ) { @@ -1476,13 +1466,7 @@ export class MultiTenancyController extends Controller { return acceptOffer } catch (error) { - if (error instanceof RecordNotFoundError) { - return notFoundError(404, { - reason: `credential with credential record id "${acceptCredentialOfferOptions.credentialRecordId}" not found.`, - }) - } - - return internalServerError(500, { message: `something went wrong: ${error}` }) + throw ErrorHandlingService.handle(error) } } @@ -1490,9 +1474,7 @@ export class MultiTenancyController extends Controller { @Get('/credentials/:credentialRecordId/:tenantId') public async getCredentialById( @Path('credentialRecordId') credentialRecordId: RecordId, - @Path('tenantId') tenantId: string, - @Res() notFoundError: TsoaResponse<404, { reason: string }>, - @Res() internalServerError: TsoaResponse<500, { message: string }> + @Path('tenantId') tenantId: string ) { let credentialRecord try { @@ -1503,13 +1485,7 @@ export class MultiTenancyController extends Controller { return credentialRecord } catch (error) { - if (error instanceof RecordNotFoundError) { - return notFoundError(404, { - reason: `credential with credential record id "${credentialRecordId}" not found.`, - }) - } - - return internalServerError(500, { message: `something went wrong: ${error}` }) + throw ErrorHandlingService.handle(error) } } @@ -1522,39 +1498,42 @@ export class MultiTenancyController extends Controller { @Query('state') state?: CredentialState ) { let credentialRecord - await this.agent.modules.tenants.withTenantAgent({ tenantId }, async (tenantAgent) => { - const credentialRepository = tenantAgent.dependencyManager.resolve(CredentialRepository) - const credentials = await credentialRepository.findByQuery(tenantAgent.context, { - connectionId, - threadId, - state, + try { + await this.agent.modules.tenants.withTenantAgent({ tenantId }, async (tenantAgent) => { + const credentialRepository = tenantAgent.dependencyManager.resolve(CredentialRepository) + const credentials = await credentialRepository.findByQuery(tenantAgent.context, { + connectionId, + threadId, + state, + }) + credentialRecord = credentials.map((c: any) => c.toJSON()) }) - credentialRecord = credentials.map((c: any) => c.toJSON()) - }) - return credentialRecord + return credentialRecord + } catch (error) { + throw ErrorHandlingService.handle(error) + } } @Security('apiKey') @Get('/proofs/:tenantId') public async getAllProofs(@Path('tenantId') tenantId: string, @Query('threadId') threadId?: string) { let proofRecord - await this.agent.modules.tenants.withTenantAgent({ tenantId }, async (tenantAgent) => { - let proofs = await tenantAgent.proofs.getAll() - if (threadId) proofs = proofs.filter((p: any) => p.threadId === threadId) - proofRecord = proofs.map((proof: any) => proof.toJSON()) - }) - return proofRecord + try { + await this.agent.modules.tenants.withTenantAgent({ tenantId }, async (tenantAgent) => { + let proofs = await tenantAgent.proofs.getAll() + if (threadId) proofs = proofs.filter((p: any) => p.threadId === threadId) + proofRecord = proofs.map((proof: any) => proof.toJSON()) + }) + return proofRecord + } catch (error) { + throw ErrorHandlingService.handle(error) + } } @Security('apiKey') @Get('/form-data/:tenantId/:proofRecordId') @Example(ProofRecordExample) - public async proofFormData( - @Path('proofRecordId') proofRecordId: string, - @Path('tenantId') tenantId: string, - @Res() notFoundError: TsoaResponse<404, { reason: string }>, - @Res() internalServerError: TsoaResponse<500, { message: string }> - ) { + public async proofFormData(@Path('proofRecordId') proofRecordId: string, @Path('tenantId') tenantId: string) { let proof try { await this.agent.modules.tenants.withTenantAgent({ tenantId }, async (tenantAgent) => { @@ -1562,25 +1541,14 @@ export class MultiTenancyController extends Controller { }) return proof } catch (error) { - if (error instanceof RecordNotFoundError) { - return notFoundError(404, { - reason: `proof with proofRecordId "${proofRecordId}" not found.`, - }) - } - - return internalServerError(500, { message: `something went wrong: ${error}` }) + throw ErrorHandlingService.handle(error) } } @Security('apiKey') @Post('/proofs/request-proof/:tenantId') @Example(ProofRecordExample) - public async requestProof( - @Body() requestProofOptions: RequestProofOptions, - @Path('tenantId') tenantId: string, - @Res() notFoundError: TsoaResponse<404, { reason: string }>, - @Res() internalServerError: TsoaResponse<500, { message: string }> - ) { + public async requestProof(@Body() requestProofOptions: RequestProofOptions, @Path('tenantId') tenantId: string) { let proof try { await this.agent.modules.tenants.withTenantAgent({ tenantId }, async (tenantAgent) => { @@ -1599,7 +1567,7 @@ export class MultiTenancyController extends Controller { return proof } catch (error) { - return internalServerError(500, { message: `something went wrong: ${error}` }) + throw ErrorHandlingService.handle(error) } } @@ -1607,8 +1575,7 @@ export class MultiTenancyController extends Controller { @Post('/proofs/create-request-oob/:tenantId') public async createRequest( @Path('tenantId') tenantId: string, - @Body() createRequestOptions: CreateProofRequestOobOptions, - @Res() internalServerError: TsoaResponse<500, { message: string }> + @Body() createRequestOptions: CreateProofRequestOobOptions ) { let oobProofRecord try { @@ -1675,7 +1642,7 @@ export class MultiTenancyController extends Controller { return oobProofRecord } catch (error) { - return internalServerError(500, { message: `something went wrong: ${error}` }) + throw ErrorHandlingService.handle(error) } } @@ -1686,13 +1653,12 @@ export class MultiTenancyController extends Controller { @Path('tenantId') tenantId: string, @Path('proofRecordId') proofRecordId: string, @Body() - request: { + request: //TODO type for request + { filterByPresentationPreview?: boolean filterByNonRevocationRequirements?: boolean comment?: string - }, - @Res() notFoundError: TsoaResponse<404, { reason: string }>, - @Res() internalServerError: TsoaResponse<500, { message: string }> + } ) { let proofRecord try { @@ -1700,65 +1666,40 @@ export class MultiTenancyController extends Controller { const requestedCredentials = await tenantAgent.proofs.selectCredentialsForRequest({ proofRecordId, }) - const acceptProofRequest: AcceptProofRequestOptions = { proofRecordId, comment: request.comment, proofFormats: requestedCredentials.proofFormats, } - const proof = await tenantAgent.proofs.acceptRequest(acceptProofRequest) proofRecord = proof.toJSON() }) return proofRecord } catch (error) { - if (error instanceof RecordNotFoundError) { - return notFoundError(404, { - reason: `proof with proofRecordId "${proofRecordId}" not found.`, - }) - } - - return internalServerError(500, { message: `something went wrong: ${error}` }) + throw ErrorHandlingService.handle(error) } } @Security('apiKey') @Post('/proofs/:proofRecordId/accept-presentation/:tenantId') @Example(ProofRecordExample) - public async acceptPresentation( - @Path('tenantId') tenantId: string, - @Path('proofRecordId') proofRecordId: string, - @Res() notFoundError: TsoaResponse<404, { reason: string }>, - @Res() internalServerError: TsoaResponse<500, { message: string }> - ) { + public async acceptPresentation(@Path('tenantId') tenantId: string, @Path('proofRecordId') proofRecordId: string) { let proof try { await this.agent.modules.tenants.withTenantAgent({ tenantId }, async (tenantAgent) => { proof = await tenantAgent.proofs.acceptPresentation({ proofRecordId }) }) - return proof } catch (error) { - if (error instanceof RecordNotFoundError) { - return notFoundError(404, { - reason: `proof with proofRecordId "${proofRecordId}" not found.`, - }) - } - - return internalServerError(500, { message: `something went wrong: ${error}` }) + throw ErrorHandlingService.handle(error) } } @Security('apiKey') @Get('/proofs/:proofRecordId/:tenantId') @Example(ProofRecordExample) - public async getProofById( - @Path('tenantId') tenantId: string, - @Path('proofRecordId') proofRecordId: RecordId, - @Res() notFoundError: TsoaResponse<404, { reason: string }>, - @Res() internalServerError: TsoaResponse<500, { message: string }> - ) { + public async getProofById(@Path('tenantId') tenantId: string, @Path('proofRecordId') proofRecordId: RecordId) { let proofRecord try { await this.agent.modules.tenants.withTenantAgent({ tenantId }, async (tenantAgent) => { @@ -1767,13 +1708,7 @@ export class MultiTenancyController extends Controller { }) return proofRecord } catch (error) { - if (error instanceof RecordNotFoundError) { - return notFoundError(404, { - reason: `proof with proofRecordId "${proofRecordId}" not found.`, - }) - } - - return internalServerError(500, { message: `something went wrong: ${error}` }) + throw ErrorHandlingService.handle(error) } }