1919const {
2020 MSGTYPE_ARIES_FWD ,
2121 MSGTYPE_GET_MSGS ,
22+ MSGTYPE_MSGS ,
2223 tryParseAuthcrypted,
2324 buildMsgVcxV2Msgs
2425} = require ( 'vcxagency-client' )
@@ -136,11 +137,21 @@ async function buildAgentConnectionAO (entityRecord, serviceWallets, serviceStor
136137 }
137138 logger . info ( `${ whoami } Handling message ${ JSON . stringify ( msgObject ) } ` )
138139 const resObject = await _handleAuthorizedAgentConnectionMsg ( msgObject )
139- logger . debug ( ` ${ whoami } Sending response: ${ JSON . stringify ( resObject ) } ` )
140+ _logResponseObject ( resObject )
140141 return { response : resObject , shouldEncrypt : true }
141142 }
142143 }
143144
145+ function _logResponseObject ( responseObject ) {
146+ const msgType = responseObject [ '@type' ]
147+ if ( msgType === MSGTYPE_MSGS ) {
148+ const msgCount = responseObject . msgs . length
149+ logger . info ( `${ whoami } Sending response of type ${ msgType } , retrieved ${ msgCount } messages.` )
150+ } else {
151+ logger . info ( `${ whoami } Sending response: ${ JSON . stringify ( responseObject ) } ` )
152+ }
153+ }
154+
144155 async function _handleAuthorizedAgentConnectionMsg ( msgObject ) {
145156 const msgType = msgObject [ '@type' ]
146157 if ( msgType === MSGTYPE_GET_MSGS ) {
@@ -150,29 +161,35 @@ async function buildAgentConnectionAO (entityRecord, serviceWallets, serviceStor
150161 }
151162 }
152163
153- async function trySendNotification ( msgUid , statusCode ) {
164+ async function trySendNotification ( msgUid ) {
154165 const webhookUrl = await serviceStorage . getAgentWebhook ( agentDid )
155- logger . info ( `Agent ${ agentDid } received aries message and resolved webhook ${ webhookUrl } ` )
156166 if ( webhookUrl ) {
157- const notificationId = uuid . v4 ( )
158- sendNotification ( webhookUrl , msgUid , statusCode , notificationId , userPairwiseDid )
159- . then ( ( ) => {
160- logger . info ( `Notification ${ notificationId } from agentConnectionDid ${ agentConnectionDid } sent to ${ webhookUrl } successfully.` )
161- } , reason => {
162- const respData = reason . response && reason . response . data
163- ? ` Response data ${ JSON . stringify ( reason . response . data ) } `
164- : ''
165- logger . warn ( `Notification ${ notificationId } from agentConnectionDid ${ agentConnectionDid } sent to ${ webhookUrl } encountered problem. Reason: ${ reason } . ${ respData } ` )
167+ sendNotification ( webhookUrl , msgUid , userPairwiseDid )
168+ . catch ( err => {
169+ if ( err . message . includes ( 'timeout' ) ) {
170+ logger . debug ( `${ whoami } Webhook url didn't respond quickly enough, err=${ err . stack } ` )
171+ // we don't log timeout errors, webhook integrators are expected to respond quickly
172+ } else {
173+ logger . warn ( `${ whoami } Error sending webhook notification, err=${ err . stack } ` )
174+ }
166175 } )
167176 }
168177 }
169178
170179 async function _handleAriesFwd ( msgObject ) {
171180 const msgUid = uuid . v4 ( )
172181 const statusCode = 'MS-103'
182+ logger . info ( `${ whoami } Received new Aries message msgUid=${ msgUid } .` )
173183 await serviceStorage . storeMessage ( agentDid , agentConnectionDid , msgUid , statusCode , msgObject . msg )
184+ logger . info ( `${ whoami } Stored message msgUid=${ msgUid } .` )
174185 serviceNewMessages . flagNewMessage ( agentDid )
175- trySendNotification ( msgUid , statusCode )
186+ . catch ( err => {
187+ logger . error ( `${ whoami } Failed to set new-message flag, agentDid=${ agentDid } , msgUid=${ msgUid } Error: ${ err . stack } ` )
188+ } )
189+ trySendNotification ( msgUid )
190+ . catch ( err => {
191+ logger . error ( `${ whoami } Failed trying to send webhook notification, agentDid=${ agentDid } , msgUid=${ msgUid } Error: ${ err . stack } ` )
192+ } )
176193 }
177194
178195 async function _handleGetMsgs ( msgObject ) {
0 commit comments