From e84041815201076e2ca7249ac400d77c7823ab3f Mon Sep 17 00:00:00 2001 From: Konstantin Tsabolov <1006564+tsabolov@users.noreply.github.com> Date: Mon, 20 Nov 2023 16:26:48 +0100 Subject: [PATCH] chore: apply new ESLint config to proof-manager --- .../src/common/exception.handler.ts | 4 +- apps/proof-manager/src/common/response.ts | 4 +- .../controller/controller.ts | 2 +- .../services/service.spec.ts | 20 ++-- .../src/presentationProof/services/service.ts | 3 +- .../src/utils/exceptionsFilter.ts | 96 +++++++++---------- apps/proof-manager/src/utils/logger.spec.ts | 2 +- apps/proof-manager/test/app.e2e-spec.ts | 1 + 8 files changed, 63 insertions(+), 69 deletions(-) diff --git a/apps/proof-manager/src/common/exception.handler.ts b/apps/proof-manager/src/common/exception.handler.ts index dfa519e..d5960e3 100644 --- a/apps/proof-manager/src/common/exception.handler.ts +++ b/apps/proof-manager/src/common/exception.handler.ts @@ -12,7 +12,7 @@ import type ResponseType from './response.js'; export default class ExceptionHandler implements ExceptionFilter { constructor(private readonly httpAdapterHost: HttpAdapterHost) {} - catch(exception: any, host: ArgumentsHost): void { + catch(exception: unknown, host: ArgumentsHost): void { // In certain situations `httpAdapter` might not be available in the // constructor method, thus we should resolve it here. const { httpAdapter } = this.httpAdapterHost; @@ -25,7 +25,7 @@ export default class ExceptionHandler implements ExceptionFilter { exception.message.error || exception.message || 'Something went wrong!'; if (exception instanceof HttpException) { - const errorResponse: any = exception.getResponse(); + const errorResponse: string | object = exception.getResponse(); statusCode = exception.getStatus(); message = errorResponse.error || message; diff --git a/apps/proof-manager/src/common/response.ts b/apps/proof-manager/src/common/response.ts index e5b6296..45f4ccb 100644 --- a/apps/proof-manager/src/common/response.ts +++ b/apps/proof-manager/src/common/response.ts @@ -1,6 +1,6 @@ export default interface ResponseType { statusCode: number; message: string; - data?: any; - error?: any; + data?: unknown; + error?: unknown; } diff --git a/apps/proof-manager/src/presentationProof/controller/controller.ts b/apps/proof-manager/src/presentationProof/controller/controller.ts index 7806729..d06d1bc 100644 --- a/apps/proof-manager/src/presentationProof/controller/controller.ts +++ b/apps/proof-manager/src/presentationProof/controller/controller.ts @@ -285,7 +285,7 @@ export default class PresentationProofsController { data.requested_proof.revealed_attr_groups[revealedAttrGroupsKey] .sub_proof_index; - const presentationData: any = { + const presentationData: (typeof resData)['presentations'][number] = { schemaId: data.identifiers[subIndex].schema_id, credDefId: data.identifiers[subIndex].cred_def_id, revRegId: data.identifiers[subIndex].rev_reg_id, diff --git a/apps/proof-manager/src/presentationProof/services/service.spec.ts b/apps/proof-manager/src/presentationProof/services/service.spec.ts index 3220467..2d0cfd7 100644 --- a/apps/proof-manager/src/presentationProof/services/service.spec.ts +++ b/apps/proof-manager/src/presentationProof/services/service.spec.ts @@ -96,7 +96,7 @@ describe.skip('ConnectionsService', () => { threadId: '34881067-b9fc-49a4-814d-dee4668b4f90', }; jest - .spyOn(natsClient, 'getAgentByParticipantId') + .spyOn(natsClient, 'getConnectionById') .mockResolvedValueOnce(getAgentDetails); jest.spyOn(restClient, 'get').mockResolvedValueOnce(result); @@ -314,7 +314,7 @@ describe.skip('ConnectionsService', () => { .mockResolvedValueOnce(natsConnectionIdResponce); jest - .spyOn(natsClient, 'getAgentByParticipantId') + .spyOn(natsClient, 'getConnectionById') .mockResolvedValueOnce(getAgentDetails); jest.spyOn(restClient, 'post').mockResolvedValueOnce(result); @@ -331,12 +331,12 @@ describe.skip('ConnectionsService', () => { comment: 'Gaia-x Test', attributes: [ { - attribute_name: 'email', + attributeName: 'email', value: '', condition: '', }, { - attribute_name: 'issuerDID', + attributeName: 'issuerDID', value: '', condition: '', }, @@ -387,7 +387,7 @@ describe.skip('ConnectionsService', () => { }; jest - .spyOn(natsClient, 'getAgentByParticipantId') + .spyOn(natsClient, 'getConnectionById') .mockResolvedValueOnce(getAgentDetails); jest.spyOn(restClient, 'post').mockResolvedValueOnce(result); @@ -404,14 +404,10 @@ describe.skip('ConnectionsService', () => { const serviceDto: MembershipCredentialDto = { attributes: [ { - attribute_name: 'email', - value: '', - condition: '', + attributeName: 'email', }, { - attribute_name: 'issuerDID', - value: '', - condition: '', + attributeName: 'issuerDID', }, ], connectionId: '', @@ -458,7 +454,7 @@ describe.skip('ConnectionsService', () => { }; jest - .spyOn(natsClient, 'getAgentByParticipantId') + .spyOn(natsClient, 'getConnectionById') .mockResolvedValueOnce(getAgentDetails); jest.spyOn(restClient, 'post').mockResolvedValueOnce(result); diff --git a/apps/proof-manager/src/presentationProof/services/service.ts b/apps/proof-manager/src/presentationProof/services/service.ts index b020b7a..c191256 100644 --- a/apps/proof-manager/src/presentationProof/services/service.ts +++ b/apps/proof-manager/src/presentationProof/services/service.ts @@ -291,8 +291,7 @@ export default class PresentationProofsService { index < sendProofRequest.attributes.length; index += 1 ) { - const attributeElement: any = - sendProofRequest.attributes[index].attributeName; + const attributeElement = sendProofRequest.attributes[index].attributeName; const attributeReferent = `additionalProp${index + 1}`; const keys = Object.keys(requestedAttributes); if (keys.length > 0) { diff --git a/apps/proof-manager/src/utils/exceptionsFilter.ts b/apps/proof-manager/src/utils/exceptionsFilter.ts index 647308c..1556f3f 100644 --- a/apps/proof-manager/src/utils/exceptionsFilter.ts +++ b/apps/proof-manager/src/utils/exceptionsFilter.ts @@ -1,22 +1,27 @@ import { - ExceptionFilter, - Catch, ArgumentsHost, + Catch, + ExceptionFilter, HttpException, HttpStatus, Logger, } from '@nestjs/common'; import { HttpAdapterHost } from '@nestjs/core'; -import { Request } from 'express'; import { Prisma } from '@prisma/client'; - -const { PrismaClientKnownRequestError, PrismaClientValidationError } = Prisma; +import { Request } from 'express'; @Catch() export default class AllExceptionsFilter implements ExceptionFilter { constructor(private readonly httpAdapterHost: HttpAdapterHost) {} - catch(exception: any, host: ArgumentsHost): void { + catch( + exception: + | HttpException + | Prisma.PrismaClientKnownRequestError + | Prisma.PrismaClientUnknownRequestError + | Error, + host: ArgumentsHost, + ): void { const { httpAdapter } = this.httpAdapterHost; const ctx = host.switchToHttp(); @@ -24,54 +29,47 @@ export default class AllExceptionsFilter implements ExceptionFilter { let httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; let message = ''; - switch (exception.constructor) { - case HttpException: - httpStatus = (exception as HttpException).getStatus(); - message = exception?.message || 'Internal server error'; - break; - case PrismaClientKnownRequestError: - switch (exception.code) { - case 'P2002': // Unique constraint failed on the {constraint} - case 'P2000': // The provided value for the column is too long for the column's type. Column: {column_name} - case 'P2001': // The record searched for in the where condition ({model_name}.{argument_name} = {argument_value}) does not exist - case 'P2005': // The value {field_value} stored in the database for the field {field_name} is invalid for the field's type - case 'P2006': // The provided value {field_value} for {model_name} field {field_name} is not valid - case 'P2010': // Raw query failed. Code: {code}. Message: {message} - case 'P2011': // Null constraint violation on the {constraint} - case 'P2017': // The records for relation {relation_name} between the {parent_name} and {child_name} models are not connected. - case 'P2021': // The table {table} does not exist in the current database. - case 'P2022': // The column {column} does not exist in the current database. - httpStatus = HttpStatus.BAD_REQUEST; - message = exception?.message; - break; - case 'P2018': // The required connected records were not found. {details} - case 'P2025': // An operation failed because it depends on one or more records that were required but not found. {cause} - case 'P2015': // A related record could not be found. {details} - httpStatus = HttpStatus.NOT_FOUND; - message = exception?.message; - break; - default: - httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; - message = exception?.message || 'Internal server error'; - } - break; - case PrismaClientValidationError: - httpStatus = HttpStatus.BAD_REQUEST; - message = exception?.message; - break; - default: - httpStatus = - exception.response?.status || HttpStatus.INTERNAL_SERVER_ERROR; - message = - exception.response?.data?.message || - exception?.message || - 'Internal server error'; + + if (exception instanceof HttpException) { + httpStatus = exception.getStatus(); + message = exception.message || 'Internal server error'; + } else if (exception instanceof Prisma.PrismaClientKnownRequestError) { + switch (exception.code) { + case 'P2002': // Unique constraint failed on the {constraint} + case 'P2000': // The provided value for the column is too long for the column's type. Column: {column_name} + case 'P2001': // The record searched for in the where condition ({model_name}.{argument_name} = {argument_value}) does not exist + case 'P2005': // The value {field_value} stored in the database for the field {field_name} is invalid for the field's type + case 'P2006': // The provided value {field_value} for {model_name} field {field_name} is not valid + case 'P2010': // Raw query failed. Code: {code}. Message: {message} + case 'P2011': // Null constraint violation on the {constraint} + case 'P2017': // The records for relation {relation_name} between the {parent_name} and {child_name} models are not connected. + case 'P2021': // The table {table} does not exist in the current database. + case 'P2022': // The column {column} does not exist in the current database. + httpStatus = HttpStatus.BAD_REQUEST; + message = exception?.message; + break; + case 'P2018': // The required connected records were not found. {details} + case 'P2025': // An operation failed because it depends on one or more records that were required but not found. {cause} + case 'P2015': // A related record could not be found. {details} + httpStatus = HttpStatus.NOT_FOUND; + message = exception?.message; + break; + default: + httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; + message = exception?.message || 'Internal server error'; + } + } else if (exception instanceof Prisma.PrismaClientValidationError) { + httpStatus = HttpStatus.BAD_REQUEST; + message = exception?.message; + } else if (exception instanceof Error) { + httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; + message = exception.message || 'Internal server error'; } Logger.error( 'Exception Filter :', message, - (exception as any).stack, + (exception as Error).stack, `${request.method} ${request.url}`, ); diff --git a/apps/proof-manager/src/utils/logger.spec.ts b/apps/proof-manager/src/utils/logger.spec.ts index e6aed70..992dd1f 100644 --- a/apps/proof-manager/src/utils/logger.spec.ts +++ b/apps/proof-manager/src/utils/logger.spec.ts @@ -7,7 +7,7 @@ describe('Logger', () => { it('should create a directory if not exists', async () => { // jest.spyOn(fs, 'existsSync').mockImplementation(() => false); // jest.spyOn(fs, 'mkdirSync').mockImplementation(() => 'mocked'); - const logger = await import('./logger'); + const logger = await import('./logger.js'); expect(logger).toBeDefined(); // expect(fs.existsSync).toHaveBeenCalled(); // expect(fs.mkdirSync).toHaveBeenCalled(); diff --git a/apps/proof-manager/test/app.e2e-spec.ts b/apps/proof-manager/test/app.e2e-spec.ts index 3120aa7..45dd541 100644 --- a/apps/proof-manager/test/app.e2e-spec.ts +++ b/apps/proof-manager/test/app.e2e-spec.ts @@ -18,6 +18,7 @@ describe('AppController (e2e)', () => { afterEach(() => app.close()); + // eslint-disable-next-line jest/expect-expect it('/health (GET)', () => request(app.getHttpServer()).get('/health').expect(200)); }); -- GitLab