Skip to content

Commit

Permalink
handled agent errors in the schema service
Browse files Browse the repository at this point in the history
Signed-off-by: Nishad <[email protected]>
  • Loading branch information
nishad-ayanworks committed Dec 20, 2023
1 parent bbc844e commit 47f347d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 93 deletions.
14 changes: 13 additions & 1 deletion apps/agent-service/src/agent-service.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
}

Expand Down
107 changes: 28 additions & 79 deletions apps/api-gateway/common/exception-handler.ts
Original file line number Diff line number Diff line change
@@ -1,100 +1,49 @@
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 {
private readonly logger = new Logger();
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);
}
}
6 changes: 6 additions & 0 deletions apps/api-gateway/common/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ export interface ResponseType {
data?: Record<string, unknown> | string;
error?: Record<string, unknown> | string;
}

export interface ExceptionResponse {
message: string | string[]
error: string
statusCode: number
}
23 changes: 13 additions & 10 deletions apps/ledger/src/schema/schema.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -218,7 +225,6 @@ export class SchemaService extends BaseService {
async _createSchema(payload: CreateSchemaAgentRedirection): Promise<{
response: string;
}> {
try {
const pattern = {
cmd: 'agent-create-schema'
};
Expand All @@ -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;
}


Expand Down
9 changes: 6 additions & 3 deletions libs/common/src/response-messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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'
}
};

0 comments on commit 47f347d

Please sign in to comment.