Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: error handling for multi-tenancy controller #144

Merged
114 changes: 44 additions & 70 deletions src/controllers/multi-tenancy/MultiTenancyController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { BadRequestError, NotFoundError } from '../../errors'
import { BCOVRIN_REGISTER_URL, INDICIO_NYM_URL } from '../../utils/util'
import { SchemaId, CredentialDefinitionId, RecordId, ProofRecordExample, ConnectionRecordExample } from '../examples'
import {
Expand Down Expand Up @@ -1777,39 +1779,29 @@ export class MultiTenancyController extends Controller {

@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) => {
if (!didOptions.seed) {
throw Error('Seed is required')
}
if (!didOptions.keyType) {
throw Error('keyType is required')
throw new BadRequestError('keyType is required')
}
if (!didOptions.domain) {
throw new BadRequestError('domain is required')
}
if (didOptions.keyType !== KeyType.Ed25519 && didOptions.keyType !== KeyType.Bls12381g2) {
throw Error('Only ed25519 and bls12381g2 key type supported')
pranalidhanavade marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -1843,24 +1835,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<KeyDidCreateOptions>({
method: 'key',
Expand All @@ -1874,9 +1860,7 @@ export class MultiTenancyController extends Controller {
})
return didCreateResponse
} catch (error) {
return internalServerError(500, {
message: `something went wrong: ${error}`,
})
throw ErrorHandlingService.handle(error)
}
}

Expand All @@ -1899,16 +1883,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)
}
}

/**
Expand All @@ -1924,13 +1912,12 @@ export class MultiTenancyController extends Controller {
@Path('connectionId') connectionId: RecordId,
@Path('tenantId') tenantId: string,
@Body()
config: {
config: //TODO type for config
{
question: string
pranalidhanavade marked this conversation as resolved.
Show resolved Hide resolved
validResponses: ValidResponse[]
detail?: string
},
@Res() notFoundError: TsoaResponse<404, { reason: string }>,
@Res() internalServerError: TsoaResponse<500, { message: string }>
}
) {
try {
const { question, validResponses, detail } = config
Expand All @@ -1943,13 +1930,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)
}
}

Expand All @@ -1965,9 +1948,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
Expand All @@ -1977,10 +1958,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)
}
}

Expand All @@ -1993,23 +1971,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
}
}