diff --git a/apps/agent-service/src/agent-service.service.ts b/apps/agent-service/src/agent-service.service.ts index 8659486c1..6088a7cce 100644 --- a/apps/agent-service/src/agent-service.service.ts +++ b/apps/agent-service/src/agent-service.service.ts @@ -720,6 +720,12 @@ export class AgentServiceService { .then(async (schema) => { this.logger.debug(`API Response Data: ${JSON.stringify(schema)}`); return schema; + }) + .catch(error => { + throw new InternalServerErrorException( + ResponseMessages.agent.error.agentDown, + { cause: new Error(), description: ResponseMessages.errorMessages.serverError } + ); }); } else if (OrgAgentType.SHARED === payload.agentType) { @@ -735,12 +741,18 @@ export class AgentServiceService { .then(async (schema) => { this.logger.debug(`API Response Data: ${JSON.stringify(schema)}`); return schema; + }) + .catch(error => { + throw new InternalServerErrorException( + ResponseMessages.agent.error.agentDown, + { cause: new Error(), description: ResponseMessages.errorMessages.serverError } + ); }); } return schemaResponse; } catch (error) { this.logger.error(`Error in creating schema: ${error}`); - throw error; + throw new RpcException(error.response ? error.response : error); } } diff --git a/apps/api-gateway/common/exception-handler.ts b/apps/api-gateway/common/exception-handler.ts index afaf5f4ea..df5928a20 100644 --- a/apps/api-gateway/common/exception-handler.ts +++ b/apps/api-gateway/common/exception-handler.ts @@ -1,6 +1,7 @@ import { Catch, ArgumentsHost, HttpException, HttpStatus, Logger } from '@nestjs/common'; import { BaseExceptionFilter } from '@nestjs/core'; -import { isArray } from 'class-validator'; +import { ExceptionResponse } from './interface'; +import { ResponseMessages } from '@credebl/common/response-messages'; @Catch() export class CustomExceptionFilter extends BaseExceptionFilter { @@ -8,93 +9,41 @@ export class CustomExceptionFilter extends BaseExceptionFilter { catch(exception: HttpException, host: ArgumentsHost): void { const ctx = host.switchToHttp(); const response = ctx.getResponse(); - + + let errorResponse; let status = HttpStatus.INTERNAL_SERVER_ERROR; - if (exception instanceof HttpException) { - status = exception.getStatus(); - } - + this.logger.error(`exception ::: ${JSON.stringify(exception)}`); - if ("Cannot read properties of undefined (reading 'response')" === exception.message) { - exception.message = 'Oops! Something went wrong. Please try again'; - } - - let errorResponse; - if (isArray(exception)) { + if (!exception || '{}' === JSON.stringify(exception)) { errorResponse = { statusCode: status, - message: exception[0], - error: exception[0] - }; - } else if (exception && exception['statusCode'] === HttpStatus.INTERNAL_SERVER_ERROR) { - if (exception.message && exception.message['message']) { - errorResponse = { - statusCode: status, - message: exception.message['message'], - error: exception.message['message'] - }; - } else { - errorResponse = { - statusCode: status, - message: 'Oops! Something went wrong. Please try again', - error: 'Oops! Something went wrong. Please try again' - }; - } - } else if ( - exception && - exception['error'] && - exception['error'].message && - (exception['error'].statusCode || exception['error'].code) - ) { - const statusCode = exception['error'].statusCode || exception['error'].code || status; - errorResponse = { - statusCode, - message: exception['error'].message || 'Internal server error', - error: exception['error'].message || 'Internal server error' - }; - } else if (exception && exception['statusCode'] === undefined && status === HttpStatus.INTERNAL_SERVER_ERROR) { - errorResponse = { - statusCode: status, - message: 'Oops! Something went wrong. Please try again', - error: 'Oops! Something went wrong. Please try again' + message: 'Something went wrong!', + error: ResponseMessages.errorMessages.serverError }; + } + if (exception instanceof HttpException) { + status = exception.getStatus(); + } + + let exceptionResponse: ExceptionResponse; + + if (exception['response']) { + exceptionResponse = exception['response']; } else { - if (exception && exception['response'] && exception.message) { - if (Array.isArray(exception['response'].message)) { - errorResponse = { - statusCode: exception['statusCode'] ? exception['statusCode'] : status, - message: exception['response'].message ? exception['response'].message : 'Internal server error', - error: exception['response'].error - ? exception['response'].error - : exception['response'] - ? exception['response'] - : 'Internal server error' - }; - } else { - errorResponse = { - statusCode: exception['statusCode'] ? exception['statusCode'] : status, - message: exception['response'].message - ? exception['response'].message - : exception['response'] - ? exception['response'] - : 'Internal server error', - error: exception['response'].error - ? exception['response'].error - : exception['response'] - ? exception['response'] - : 'Internal server error' - }; - } - } else if (exception && exception.message) { - errorResponse = { - statusCode: exception['statusCode'] ? exception['statusCode'] : status, - message: exception.message || 'Internal server error', - error: exception.message || 'Internal server error' - }; - } + exceptionResponse = exception as unknown as ExceptionResponse; } + errorResponse = { + statusCode: exceptionResponse.statusCode ? exceptionResponse.statusCode : status, + message: exceptionResponse.message + ? exceptionResponse.message + : 'Something went wrong!', + error: exceptionResponse.error + ? exceptionResponse.error + : ResponseMessages.errorMessages.serverError + }; + response.status(errorResponse.statusCode).json(errorResponse); } } \ No newline at end of file diff --git a/apps/api-gateway/common/interface.ts b/apps/api-gateway/common/interface.ts index a64429f45..d3869d75f 100644 --- a/apps/api-gateway/common/interface.ts +++ b/apps/api-gateway/common/interface.ts @@ -4,3 +4,9 @@ export interface ResponseType { data?: Record | string; error?: Record | string; } + +export interface ExceptionResponse { + message: string | string[] + error: string + statusCode: number +} diff --git a/apps/ledger/src/schema/schema.service.ts b/apps/ledger/src/schema/schema.service.ts index 4d054f48c..ca848b0da 100644 --- a/apps/ledger/src/schema/schema.service.ts +++ b/apps/ledger/src/schema/schema.service.ts @@ -100,7 +100,14 @@ export class SchemaService extends BaseService { } schema.schemaName = schema.schemaName.trim(); - const { agentEndPoint, orgDid } = await this.schemaRepository.getAgentDetailsByOrgId(orgId); + const agentDetails = await this.schemaRepository.getAgentDetailsByOrgId(orgId); + if (!agentDetails) { + throw new NotFoundException( + ResponseMessages.schema.error.agentDetailsNotFound, + { cause: new Error(), description: ResponseMessages.errorMessages.notFound } + ); + } + const { agentEndPoint, orgDid } = agentDetails; const getAgentDetails = await this.schemaRepository.getAgentType(orgId); // eslint-disable-next-line yoda const did = schema.orgDid?.split(':').length >= 4 ? schema.orgDid : orgDid; @@ -218,7 +225,6 @@ export class SchemaService extends BaseService { async _createSchema(payload: CreateSchemaAgentRedirection): Promise<{ response: string; }> { - try { const pattern = { cmd: 'agent-create-schema' }; @@ -231,18 +237,15 @@ export class SchemaService extends BaseService { })) ).toPromise() .catch(error => { - this.logger.error(`Catch : ${JSON.stringify(error)}`); + this.logger.error(`Error in creating schema : ${JSON.stringify(error)}`); throw new HttpException( { - status: error.statusCode, - error: error.message + status: error.statusCode, + error: error.error, + message: error.message }, error.error); }); - return schemaResponse; - } catch (error) { - this.logger.error(`Error in creating schema : ${JSON.stringify(error)}`); - throw error; - } + return schemaResponse; } diff --git a/libs/common/src/response-messages/index.ts b/libs/common/src/response-messages/index.ts index 0d7c9b2ec..02a92385e 100644 --- a/libs/common/src/response-messages/index.ts +++ b/libs/common/src/response-messages/index.ts @@ -117,7 +117,8 @@ export const ResponseMessages = { notFound: 'Schema records not found', schemaIdNotFound: 'SchemaLedgerId not found', credentialDefinitionNotFound: 'No credential definition exist', - notStoredCredential: 'User credential not stored' + notStoredCredential: 'User credential not stored', + agentDetailsNotFound: 'Agent details not found' } }, credentialDefinition: { @@ -154,7 +155,8 @@ export const ResponseMessages = { notAbleToSpinUpAgent: 'Agent not able to spin-up', alreadySpinUp: 'Agent already spin-up', agentUrl: 'Agent url not exist', - agentNotExists: 'Agent not spinned up for this organization' + agentNotExists: 'Agent not spinned up for this organization', + agentDown: 'Agent is down or not spinned up' } }, connection: { @@ -300,6 +302,7 @@ export const ResponseMessages = { badRequest: 'Bad Request', conflict: 'Conflict', notAcceptable: 'Not Acceptable', - notFound: 'Not Found' + notFound: 'Not Found', + serverError: 'Internal Server error' } }; \ No newline at end of file