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

Unified error handling - All controllers #39

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@digicatapult/veritable-cloudagent",
"version": "0.4.10",
"version": "0.4.11",
"main": "build/index",
"types": "build/index",
"files": [
Expand Down
47 changes: 20 additions & 27 deletions src/controllers/connections/ConnectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import {
AriesFrameworkError,
RecordNotFoundError,
} from '@aries-framework/core'
import { Controller, Delete, Example, Get, Path, Post, Query, Res, Route, Tags, TsoaResponse } from 'tsoa'
import { Controller, Delete, Example, Get, Path, Post, Query, Route, Tags, Response } from 'tsoa'
import { injectable } from 'tsyringe'

import { ConnectionRecordExample, RecordId } from '../examples'
import { HttpResponse, NotFound } from '../../error'

@Tags('Connections')
@Route('/connections')
Expand Down Expand Up @@ -76,13 +77,11 @@ export class ConnectionController extends Controller {
*/
@Example<ConnectionRecordProps>(ConnectionRecordExample)
@Get('/:connectionId')
public async getConnectionById(
@Path('connectionId') connectionId: RecordId,
@Res() notFoundError: TsoaResponse<404, { reason: string }>
) {
@Response<NotFound['message']>(404)
public async getConnectionById(@Path('connectionId') connectionId: RecordId) {
const connection = await this.agent.connections.findById(connectionId)

if (!connection) return notFoundError(404, { reason: `connection with connection id "${connectionId}" not found.` })
if (!connection) throw new NotFound(`connection with connection id "${connectionId}" not found.`)

return connection.toJSON()
}
Expand All @@ -93,19 +92,17 @@ export class ConnectionController extends Controller {
* @param connectionId Connection identifier
*/
@Delete('/:connectionId')
public async deleteConnection(
@Path('connectionId') connectionId: RecordId,
@Res() notFoundError: TsoaResponse<404, { reason: string }>,
@Res() internalServerError: TsoaResponse<500, { message: string }>
) {
@Response<NotFound['message']>(404)
@Response<HttpResponse>(500)
public async deleteConnection(@Path('connectionId') connectionId: RecordId) {
try {
this.setStatus(204)
await this.agent.connections.deleteById(connectionId)
} catch (error) {
if (error instanceof RecordNotFoundError) {
return notFoundError(404, { reason: `connection with connection id "${connectionId}" not found.` })
throw new NotFound(`connection with connection id "${connectionId}" not found.`)
}
return internalServerError(500, { message: `something went wrong: ${error}` })
throw error
}
}

Expand All @@ -120,19 +117,17 @@ export class ConnectionController extends Controller {
*/
@Example<ConnectionRecordProps>(ConnectionRecordExample)
@Post('/:connectionId/accept-request')
public async acceptRequest(
@Path('connectionId') connectionId: RecordId,
@Res() notFoundError: TsoaResponse<404, { reason: string }>,
@Res() internalServerError: TsoaResponse<500, { message: string }>
) {
@Response<NotFound['message']>(404)
@Response<HttpResponse>(500)
public async acceptRequest(@Path('connectionId') connectionId: RecordId) {
try {
const connection = await this.agent.connections.acceptRequest(connectionId)
return connection.toJSON()
} catch (error) {
if (error instanceof AriesFrameworkError) {
return notFoundError(404, { reason: `connection with connection id "${connectionId}" not found.` })
throw new NotFound(`connection with connection id "${connectionId}" not found.`)
}
return internalServerError(500, { message: `something went wrong: ${error}` })
throw error
}
}

Expand All @@ -147,19 +142,17 @@ export class ConnectionController extends Controller {
*/
@Example<ConnectionRecordProps>(ConnectionRecordExample)
@Post('/:connectionId/accept-response')
public async acceptResponse(
@Path('connectionId') connectionId: RecordId,
@Res() notFoundError: TsoaResponse<404, { reason: string }>,
@Res() internalServerError: TsoaResponse<500, { message: string }>
) {
@Response<NotFound['message']>(404)
@Response<HttpResponse>(500)
public async acceptResponse(@Path('connectionId') connectionId: RecordId) {
try {
const connection = await this.agent.connections.acceptResponse(connectionId)
return connection.toJSON()
} catch (error) {
if (error instanceof RecordNotFoundError) {
return notFoundError(404, { reason: `connection with connection id "${connectionId}" not found.` })
throw new NotFound(`connection with connection id "${connectionId}" not found.`)
}
return internalServerError(500, { message: `something went wrong: ${error}` })
throw error
}
}
}
125 changes: 47 additions & 78 deletions src/controllers/credentials/CredentialController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import type { RestAgent } from '../../utils/agent'
import type { CredentialExchangeRecordProps } from '@aries-framework/core'

import { CredentialRepository, CredentialState, Agent, RecordNotFoundError } from '@aries-framework/core'
import { Body, Controller, Delete, Get, Path, Post, Res, Route, Tags, TsoaResponse, Example, Query } from 'tsoa'
import { Body, Controller, Delete, Get, Path, Post, Route, Tags, Example, Query, Response } from 'tsoa'
import { injectable } from 'tsyringe'

import { CredentialExchangeRecordExample, RecordId } from '../examples'
import { HttpResponse, NotFound } from '../../error'
import {
AcceptCredentialRequestOptions,
OfferCredentialOptions,
Expand Down Expand Up @@ -57,21 +58,17 @@ export class CredentialController extends Controller {
*/
@Example<CredentialExchangeRecordProps>(CredentialExchangeRecordExample)
@Get('/:credentialRecordId')
public async getCredentialById(
@Path('credentialRecordId') credentialRecordId: RecordId,
@Res() notFoundError: TsoaResponse<404, { reason: string }>,
@Res() internalServerError: TsoaResponse<500, { message: string }>
) {
@Response<NotFound['message']>(404)
@Response<HttpResponse>(500)
public async getCredentialById(@Path('credentialRecordId') credentialRecordId: RecordId) {
try {
const credential = await this.agent.credentials.getById(credentialRecordId)
return credential.toJSON()
} catch (error) {
if (error instanceof RecordNotFoundError) {
return notFoundError(404, {
reason: `credential with credential record id "${credentialRecordId}" not found.`,
})
throw new NotFound(`credential with credential record id "${credentialRecordId}" not found.`)
}
return internalServerError(500, { message: `something went wrong: ${error}` })
throw error
}
}

Expand All @@ -81,21 +78,17 @@ export class CredentialController extends Controller {
* @param credentialRecordId
*/
@Delete('/:credentialRecordId')
public async deleteCredential(
@Path('credentialRecordId') credentialRecordId: RecordId,
@Res() notFoundError: TsoaResponse<404, { reason: string }>,
@Res() internalServerError: TsoaResponse<500, { message: string }>
) {
@Response<NotFound['message']>(404)
@Response<HttpResponse>(500)
public async deleteCredential(@Path('credentialRecordId') credentialRecordId: RecordId) {
try {
this.setStatus(204)
await this.agent.credentials.deleteById(credentialRecordId)
} catch (error) {
if (error instanceof RecordNotFoundError) {
return notFoundError(404, {
reason: `credential with credential record id "${credentialRecordId}" not found.`,
})
throw new NotFound(`credential with credential record id "${credentialRecordId}" not found.`)
}
return internalServerError(500, { message: `something went wrong: ${error}` })
throw error
}
}

Expand All @@ -108,21 +101,17 @@ export class CredentialController extends Controller {
*/
@Example<CredentialExchangeRecordProps>(CredentialExchangeRecordExample)
@Post('/propose-credential')
public async proposeCredential(
@Body() options: ProposeCredentialOptions,
@Res() notFoundError: TsoaResponse<404, { reason: string }>,
@Res() internalServerError: TsoaResponse<500, { message: string }>
) {
@Response<NotFound['message']>(404)
@Response<HttpResponse>(500)
public async proposeCredential(@Body() options: ProposeCredentialOptions) {
try {
const credential = await this.agent.credentials.proposeCredential(options)
return credential.toJSON()
} catch (error) {
if (error instanceof RecordNotFoundError) {
return notFoundError(404, {
reason: `connection with connection record id "${options.connectionId}" not found.`,
})
throw new NotFound(`connection with connection record id "${options.connectionId}" not found.`)
}
return internalServerError(500, { message: `something went wrong: ${error}` })
throw error
}
}

Expand All @@ -136,10 +125,10 @@ export class CredentialController extends Controller {
*/
@Example<CredentialExchangeRecordProps>(CredentialExchangeRecordExample)
@Post('/:credentialRecordId/accept-proposal')
@Response<NotFound['message']>(404)
@Response<HttpResponse>(500)
public async acceptProposal(
@Path('credentialRecordId') credentialRecordId: RecordId,
@Res() notFoundError: TsoaResponse<404, { reason: string }>,
@Res() internalServerError: TsoaResponse<500, { message: string }>,
@Body() options?: AcceptCredentialProposalOptions
) {
try {
Expand All @@ -151,11 +140,9 @@ export class CredentialController extends Controller {
return credential.toJSON()
} catch (error) {
if (error instanceof RecordNotFoundError) {
return notFoundError(404, {
reason: `credential with credential record id "${credentialRecordId}" not found.`,
})
throw new NotFound(`credential with credential record id "${credentialRecordId}" not found.`)
}
return internalServerError(500, { message: `something went wrong: ${error}` })
throw error
}
}

Expand All @@ -168,18 +155,11 @@ export class CredentialController extends Controller {
*/
@Example<CredentialExchangeRecordProps>(CredentialExchangeRecordExample)
@Post('/create-offer')
public async createOffer(
@Body() options: CreateOfferOptions,
@Res() internalServerError: TsoaResponse<500, { message: string }>
) {
try {
const offer = await this.agent.credentials.createOffer(options)
return {
message: offer.message.toJSON(),
credentialRecord: offer.credentialRecord.toJSON(),
}
} catch (error) {
return internalServerError(500, { message: `something went wrong: ${error}` })
public async createOffer(@Body() options: CreateOfferOptions) {
const offer = await this.agent.credentials.createOffer(options)
return {
message: offer.message.toJSON(),
credentialRecord: offer.credentialRecord.toJSON(),
}
}

Expand All @@ -192,26 +172,23 @@ export class CredentialController extends Controller {
*/
@Example<CredentialExchangeRecordProps>(CredentialExchangeRecordExample)
@Post('/offer-credential')
public async offerCredential(
@Body() options: OfferCredentialOptions,
@Res() notFoundError: TsoaResponse<404, { reason: string }>,
@Res() internalServerError: TsoaResponse<500, { message: string }>
) {
@Response<NotFound['message']>(404)
@Response<HttpResponse>(500)
public async offerCredential(@Body() options: OfferCredentialOptions) {
try {
//check connection exists
const connection = await this.agent.connections.findById(options.connectionId)
if (!connection)
return notFoundError(404, { reason: `connection with connection id "${options.connectionId}" not found.` })
if (!connection) throw new NotFound(`connection with connection id "${options.connectionId}" not found.`)

const credential = await this.agent.credentials.offerCredential(options)
return credential.toJSON()
} catch (error) {
if (error instanceof RecordNotFoundError) {
return notFoundError(404, {
reason: `the credential definition id "${options.credentialFormats.anoncreds?.credentialDefinitionId}" not found.`,
})
throw new NotFound(
`the credential definition id "${options.credentialFormats.anoncreds?.credentialDefinitionId}" not found.`
)
}
return internalServerError(500, { message: `something went wrong: ${error}` })
throw error
}
}

Expand All @@ -225,10 +202,10 @@ export class CredentialController extends Controller {
*/
@Example<CredentialExchangeRecordProps>(CredentialExchangeRecordExample)
@Post('/:credentialRecordId/accept-offer')
@Response<NotFound['message']>(404)
@Response<HttpResponse>(500)
public async acceptOffer(
@Path('credentialRecordId') credentialRecordId: RecordId,
@Res() notFoundError: TsoaResponse<404, { reason: string }>,
@Res() internalServerError: TsoaResponse<500, { message: string }>,
@Body() options?: AcceptCredentialOfferOptions
) {
try {
Expand All @@ -239,11 +216,9 @@ export class CredentialController extends Controller {
return credential.toJSON()
} catch (error) {
if (error instanceof RecordNotFoundError) {
return notFoundError(404, {
reason: `credential with credential record id "${credentialRecordId}" not found.`,
})
throw new NotFound(`credential with credential record id "${credentialRecordId}" not found.`)
}
return internalServerError(500, { message: `something went wrong: ${error}` })
throw error
}
}

Expand All @@ -257,10 +232,10 @@ export class CredentialController extends Controller {
*/
@Example<CredentialExchangeRecordProps>(CredentialExchangeRecordExample)
@Post('/:credentialRecordId/accept-request')
@Response<NotFound['message']>(404)
@Response<HttpResponse>(500)
public async acceptRequest(
@Path('credentialRecordId') credentialRecordId: RecordId,
@Res() notFoundError: TsoaResponse<404, { reason: string }>,
@Res() internalServerError: TsoaResponse<500, { message: string }>,
@Body() options?: AcceptCredentialRequestOptions
) {
try {
Expand All @@ -271,11 +246,9 @@ export class CredentialController extends Controller {
return credential.toJSON()
} catch (error) {
if (error instanceof RecordNotFoundError) {
return notFoundError(404, {
reason: `credential with credential record id "${credentialRecordId}" not found.`,
})
throw new NotFound(`credential with credential record id "${credentialRecordId}" not found.`)
}
return internalServerError(500, { message: `something went wrong: ${error}` })
throw error
}
}

Expand All @@ -288,21 +261,17 @@ export class CredentialController extends Controller {
*/
@Example<CredentialExchangeRecordProps>(CredentialExchangeRecordExample)
@Post('/:credentialRecordId/accept-credential')
public async acceptCredential(
@Path('credentialRecordId') credentialRecordId: RecordId,
@Res() notFoundError: TsoaResponse<404, { reason: string }>,
@Res() internalServerError: TsoaResponse<500, { message: string }>
) {
@Response<NotFound['message']>(404)
@Response<HttpResponse>(500)
public async acceptCredential(@Path('credentialRecordId') credentialRecordId: RecordId) {
try {
const credential = await this.agent.credentials.acceptCredential({ credentialRecordId: credentialRecordId })
return credential.toJSON()
} catch (error) {
if (error instanceof RecordNotFoundError) {
return notFoundError(404, {
reason: `credential with credential record id "${credentialRecordId}" not found.`,
})
throw new NotFound(`credential with credential record id "${credentialRecordId}" not found.`)
}
return internalServerError(500, { message: `something went wrong: ${error}` })
throw error
}
}
}
Loading
Loading