diff --git a/src/controllers/proofs/ProofController.ts b/src/controllers/proofs/ProofController.ts index 101a5aa2..46d259fc 100644 --- a/src/controllers/proofs/ProofController.ts +++ b/src/controllers/proofs/ProofController.ts @@ -5,9 +5,10 @@ import type { Routing, } from '@credo-ts/core' -import { Agent, Key, KeyType, RecordNotFoundError } from '@credo-ts/core' +import { Agent, Key, KeyType } from '@credo-ts/core' import { injectable } from 'tsyringe' +import ErrorHandlingService from '../../errorHandlingService' import { ProofRecordExample, RecordId } from '../examples' import { AcceptProofProposal, @@ -16,7 +17,7 @@ import { RequestProofProposalOptions, } from '../types' -import { Body, Controller, Example, Get, Path, Post, Query, Res, Route, Tags, TsoaResponse, Security } from 'tsoa' +import { Body, Controller, Example, Get, Path, Post, Query, Route, Tags, Security } from 'tsoa' @Tags('Proofs') @Route('/proofs') @@ -38,11 +39,15 @@ export class ProofController extends Controller { @Example([ProofRecordExample]) @Get('/') public async getAllProofs(@Query('threadId') threadId?: string) { - let proofs = await this.agent.proofs.getAll() + try { + let proofs = await this.agent.proofs.getAll() - if (threadId) proofs = proofs.filter((p) => p.threadId === threadId) + if (threadId) proofs = proofs.filter((p) => p.threadId === threadId) - return proofs.map((proof) => proof.toJSON()) + return proofs.map((proof) => proof.toJSON()) + } catch (error) { + throw ErrorHandlingService.handle(error) + } } /** @@ -53,22 +58,13 @@ export class ProofController extends Controller { */ @Get('/:proofRecordId') @Example(ProofRecordExample) - public async getProofById( - @Path('proofRecordId') proofRecordId: RecordId, - @Res() notFoundError: TsoaResponse<404, { reason: string }>, - @Res() internalServerError: TsoaResponse<500, { message: string }> - ) { + public async getProofById(@Path('proofRecordId') proofRecordId: RecordId) { try { const proof = await this.agent.proofs.getById(proofRecordId) return proof.toJSON() } 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) } } @@ -81,11 +77,7 @@ export class ProofController extends Controller { */ @Post('/propose-proof') @Example(ProofRecordExample) - public async proposeProof( - @Body() requestProofProposalOptions: RequestProofProposalOptions, - @Res() notFoundError: TsoaResponse<404, { reason: string }>, - @Res() internalServerError: TsoaResponse<500, { message: string }> - ) { + public async proposeProof(@Body() requestProofProposalOptions: RequestProofProposalOptions) { try { const proof = await this.agent.proofs.proposeProof({ connectionId: requestProofProposalOptions.connectionId, @@ -99,12 +91,7 @@ export class ProofController extends Controller { return proof } catch (error) { - if (error instanceof RecordNotFoundError) { - return notFoundError(404, { - reason: `connection with connection id "${requestProofProposalOptions.connectionId}" not found.`, - }) - } - return internalServerError(500, { message: `something went wrong: ${error}` }) + throw ErrorHandlingService.handle(error) } } @@ -118,32 +105,22 @@ export class ProofController extends Controller { */ @Post('/:proofRecordId/accept-proposal') @Example(ProofRecordExample) - public async acceptProposal( - @Body() acceptProposal: AcceptProofProposal, - @Res() notFoundError: TsoaResponse<404, { reason: string }>, - @Res() internalServerError: TsoaResponse<500, { message: string }> - ) { + public async acceptProposal(@Body() acceptProposal: AcceptProofProposal) { try { const proof = await this.agent.proofs.acceptProposal(acceptProposal) return proof } catch (error) { - if (error instanceof RecordNotFoundError) { - return notFoundError(404, { - reason: `proof with proof record "${acceptProposal.proofRecordId}" not found.`, - }) - } - return internalServerError(500, { message: `something went wrong: ${error}` }) + throw ErrorHandlingService.handle(error) } } + /** + * Creates a presentation request bound to existing connection + */ @Post('/request-proof') @Example(ProofRecordExample) - public async requestProof( - @Body() requestProofOptions: RequestProofOptions, - @Res() notFoundError: TsoaResponse<404, { reason: string }>, - @Res() internalServerError: TsoaResponse<500, { message: string }> - ) { + public async requestProof(@Body() requestProofOptions: RequestProofOptions) { try { const requestProofPayload = { connectionId: requestProofOptions.connectionId, @@ -159,15 +136,16 @@ export class ProofController extends Controller { return proof } catch (error) { - return internalServerError(500, { message: `something went wrong: ${error}` }) + throw ErrorHandlingService.handle(error) } } + /** + * Creates a presentation request not bound to any proposal or existing connection + */ @Post('create-request-oob') - public async createRequest( - @Body() createRequestOptions: CreateProofRequestOobOptions, - @Res() internalServerError: TsoaResponse<500, { message: string }> - ) { + @Example(ProofRecordExample) + public async createRequest(@Body() createRequestOptions: CreateProofRequestOobOptions) { try { let routing: Routing if (createRequestOptions?.recipientKey) { @@ -209,7 +187,7 @@ export class ProofController extends Controller { recipientKey: createRequestOptions?.recipientKey ? {} : { recipientKey: routing.recipientKey.publicKeyBase58 }, } } catch (error) { - return internalServerError(500, { message: `something went wrong: ${error}` }) + throw ErrorHandlingService.handle(error) } } @@ -230,9 +208,7 @@ export class ProofController extends Controller { filterByPresentationPreview?: boolean filterByNonRevocationRequirements?: boolean comment?: string - }, - @Res() notFoundError: TsoaResponse<404, { reason: string }>, - @Res() internalServerError: TsoaResponse<500, { message: string }> + } ) { try { const requestedCredentials = await this.agent.proofs.selectCredentialsForRequest({ @@ -249,12 +225,7 @@ export class ProofController extends Controller { return proof.toJSON() } 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) } } @@ -267,41 +238,30 @@ export class ProofController extends Controller { */ @Post('/:proofRecordId/accept-presentation') @Example(ProofRecordExample) - public async acceptPresentation( - @Path('proofRecordId') proofRecordId: string, - @Res() notFoundError: TsoaResponse<404, { reason: string }>, - @Res() internalServerError: TsoaResponse<500, { message: string }> - ) { + public async acceptPresentation(@Path('proofRecordId') proofRecordId: string) { try { const proof = await this.agent.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) } } + /** + * Return proofRecord + * + * @param proofRecordId + * @returns ProofRecord + */ @Get('/:proofRecordId/form-data') @Example(ProofRecordExample) - public async proofFormData( - @Path('proofRecordId') proofRecordId: string, - @Res() notFoundError: TsoaResponse<404, { reason: string }>, - @Res() internalServerError: TsoaResponse<500, { message: string }> - ) { + // TODO: Add return type + public async proofFormData(@Path('proofRecordId') proofRecordId: string): Promise { try { const proof = await this.agent.proofs.getFormatData(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) } } }