Skip to content

Commit

Permalink
refactor: error handling of polygon controller (#143)
Browse files Browse the repository at this point in the history
Signed-off-by: pranalidhanavade <[email protected]>
  • Loading branch information
pranalidhanavade authored Jul 1, 2024
1 parent 18c7943 commit c759de7
Showing 1 changed file with 19 additions and 34 deletions.
53 changes: 19 additions & 34 deletions src/controllers/polygon/PolygonController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
Expand All @@ -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)
}
}

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

Expand All @@ -109,25 +110,21 @@ export class Polygon extends Controller {
estimateTransactionRequest: {
operation: any
transaction: any
},
@Res() internalServerError: TsoaResponse<500, { message: string }>,
@Res() badRequestError: TsoaResponse<400, { reason: string }>
}
): Promise<unknown> {
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 })
} else if (operation === DidOperation.Update) {
return this.agent.modules.polygon.estimateFeeForDidOperation({ operation })
}
} catch (error) {
return internalServerError(500, { message: `something went wrong: ${error}` })
throw ErrorHandlingService.handle(error)
}
}

Expand All @@ -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<unknown> {
public async getSchemaById(@Path('did') did: string, @Path('schemaId') schemaId: string): Promise<unknown> {
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)
}
}
}

0 comments on commit c759de7

Please sign in to comment.