diff --git a/src/controllers/http-api/v0/local-store-http-api-controller-v0.js b/src/controllers/http-api/v0/local-store-http-api-controller-v0.js index 06b6e0385a..1941ecc5a1 100644 --- a/src/controllers/http-api/v0/local-store-http-api-controller-v0.js +++ b/src/controllers/http-api/v0/local-store-http-api-controller-v0.js @@ -1,5 +1,5 @@ import BaseController from '../base-http-api-controller.js'; -import { OPERATION_ID_STATUS } from '../../../constants/constants.js'; +import { ERROR_TYPE, OPERATION_ID_STATUS } from '../../../constants/constants.js'; class LocalStoreController extends BaseController { constructor(ctx) { @@ -65,7 +65,18 @@ class LocalStoreController extends BaseController { )}. Operation id: ${operationId}`, ); - await this.operationIdService.cacheOperationIdData(operationId, cachedAssertions); + try { + await this.operationIdService.cacheOperationIdData(operationId, cachedAssertions); + } catch (err) { + this.operationIdService.updateOperationIdStatus( + operationId, + null, + OPERATION_ID_STATUS.FAILED, + err.message, + ERROR_TYPE.LOCAL_STORE.LOCAL_STORE_ERROR, + ); + this.logger.warn(`Error caching operationId: ${operationId} data, ${err}`); + } const commandSequence = ['validateAssetCommand', 'localStoreCommand']; diff --git a/src/controllers/rpc/publish-rpc-controller.js b/src/controllers/rpc/publish-rpc-controller.js index 84010abb3c..1d56324383 100644 --- a/src/controllers/rpc/publish-rpc-controller.js +++ b/src/controllers/rpc/publish-rpc-controller.js @@ -37,11 +37,11 @@ class PublishController extends BaseController { command.data.keyword = message.data.keyword; command.data.agreementId = dataSource.agreementId; command.data.agreementData = dataSource.agreementData; + break; default: throw Error('unknown message type'); } - command.data = { ...command.data, remotePeerId, diff --git a/src/modules/network/implementation/libp2p-service.js b/src/modules/network/implementation/libp2p-service.js index 385d9be40e..359951706a 100644 --- a/src/modules/network/implementation/libp2p-service.js +++ b/src/modules/network/implementation/libp2p-service.js @@ -196,7 +196,6 @@ class Libp2pService { handleMessage(protocol, handler) { this.logger.info(`Enabling network protocol: ${protocol}`); - this.node.handle(protocol, async (handlerProps) => { const { stream } = handlerProps; const peerIdString = handlerProps.connection.remotePeer.toB58String(); @@ -205,7 +204,6 @@ class Libp2pService { this.isRequestValid.bind(this), peerIdString, ); - this.updateSessionStream( message.header.operationId, message.header.keywordUuid, @@ -214,28 +212,41 @@ class Libp2pService { ); if (!valid) { - await this.sendMessageResponse( - protocol, - peerIdString, - NETWORK_MESSAGE_TYPES.RESPONSES.NACK, - message.header.operationId, - message.header.keywordUuid, - { errorMessage: 'Invalid request message' }, - ); + try { + await this.sendMessageResponse( + protocol, + peerIdString, + NETWORK_MESSAGE_TYPES.RESPONSES.NACK, + message.header.operationId, + message.header.keywordUuid, + { errorMessage: 'Invalid request message' }, + ); + } catch (error) { + this.logger.warn( + `Request message is not valid with operationId: ${message.header.operationId}, peerId: ${peerIdString}, keyword: ${message.header.keywordUuid}, Error: ${error}`, + ); + } + this.removeCachedSession( message.header.operationId, message.header.keywordUuid, peerIdString, ); } else if (busy) { - await this.sendMessageResponse( - protocol, - peerIdString, - NETWORK_MESSAGE_TYPES.RESPONSES.BUSY, - message.header.operationId, - message.header.keywordUuid, - {}, - ); + try { + await this.sendMessageResponse( + protocol, + peerIdString, + NETWORK_MESSAGE_TYPES.RESPONSES.BUSY, + message.header.operationId, + message.header.keywordUuid, + {}, + ); + } catch (error) { + this.logger.warn( + `Peer is busy, operationId: ${message.header.operationId}, peerId: ${peerIdString}, keyword: ${message.header.keywordUuid}, Error: ${error}`, + ); + } this.removeCachedSession( message.header.operationId, message.header.keywordUuid, @@ -243,9 +254,23 @@ class Libp2pService { ); } else { this.logger.debug( - `Receiving message from ${peerIdString} to ${this.config.id}: protocol: ${protocol}, messageType: ${message.header.messageType};`, + `Receiving message from peerId: ${peerIdString} to ${this.config.id} protocol: ${protocol} with operationId: ${message.header.operationId} and keyword: ${message.header.keywordUuid}, messageType: ${message.header.messageType};`, ); - await handler(message, peerIdString); + try { + await handler(message, peerIdString); + } catch (error) { + await this.sendMessageResponse( + protocol, + peerIdString, + NETWORK_MESSAGE_TYPES.RESPONSES.NACK, + message.header.operationId, + message.header.keywordUuid, + { errorMessage: 'Unable to handle request' }, + ); + this.logger.error( + `Error handling message from peerId: ${peerIdString} to ${this.config.id} protocol : ${protocol} with operationId: ${message.header.operationId} and keyword: ${message.header.keywordUuid}, Error: ${error} `, + ); + } } }); } @@ -633,7 +658,13 @@ class Libp2pService { removeCachedSession(operationId, keywordUuid, peerIdString) { if (this.sessions[peerIdString]?.[operationId]?.[keywordUuid]?.stream) { - this.sessions[peerIdString][operationId][keywordUuid].stream.close(); + try { + this.sessions[peerIdString][operationId][keywordUuid].stream.close(); + } catch (error) { + this.logger.error( + `Error closing session stream. OperationId: ${operationId}, peerId: ${peerIdString} Error: ${error.message}`, + ); + } delete this.sessions[peerIdString][operationId]; this.logger.trace( `Removed session for remotePeerId: ${peerIdString}, operationId: ${operationId}.`, diff --git a/src/service/operation-id-service.js b/src/service/operation-id-service.js index 8331c067ea..cc29e862d0 100644 --- a/src/service/operation-id-service.js +++ b/src/service/operation-id-service.js @@ -115,11 +115,10 @@ class OperationIdService { } async getCachedOperationIdData(operationId) { - if (this.memoryCachedHandlersData[operationId]) { + if (this.memoryCachedHandlersData[operationId]?.data) { this.logger.debug(`Reading operation id: ${operationId} cached data from memory`); return this.memoryCachedHandlersData[operationId].data; } - this.logger.debug(`Reading operation id: ${operationId} cached data from file`); const documentPath = this.fileService.getOperationIdDocumentPath(operationId); let data;