Skip to content

Commit

Permalink
refactor: error handling for proof controller (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
GHkrishna authored Jun 25, 2024
1 parent 4a63ecc commit 6fed186
Showing 1 changed file with 40 additions and 80 deletions.
120 changes: 40 additions & 80 deletions src/controllers/proofs/ProofController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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')
Expand All @@ -38,11 +39,15 @@ export class ProofController extends Controller {
@Example<ProofExchangeRecordProps[]>([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)
}
}

/**
Expand All @@ -53,22 +58,13 @@ export class ProofController extends Controller {
*/
@Get('/:proofRecordId')
@Example<ProofExchangeRecordProps>(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)
}
}

Expand All @@ -81,11 +77,7 @@ export class ProofController extends Controller {
*/
@Post('/propose-proof')
@Example<ProofExchangeRecordProps>(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,
Expand All @@ -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)
}
}

Expand All @@ -118,32 +105,22 @@ export class ProofController extends Controller {
*/
@Post('/:proofRecordId/accept-proposal')
@Example<ProofExchangeRecordProps>(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<ProofExchangeRecordProps>(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,
Expand All @@ -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<ProofExchangeRecordProps>(ProofRecordExample)
public async createRequest(@Body() createRequestOptions: CreateProofRequestOobOptions) {
try {
let routing: Routing
if (createRequestOptions?.recipientKey) {
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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({
Expand All @@ -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)
}
}

Expand All @@ -267,41 +238,30 @@ export class ProofController extends Controller {
*/
@Post('/:proofRecordId/accept-presentation')
@Example<ProofExchangeRecordProps>(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<ProofExchangeRecordProps>(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<any> {
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)
}
}
}

0 comments on commit 6fed186

Please sign in to comment.