Skip to content

Commit

Permalink
DCKA-2041: message provider, improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
maycon-mello committed Oct 9, 2023
1 parent 1efd56d commit 1848b59
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
14 changes: 14 additions & 0 deletions packages/core/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import axios from 'axios';
import assert from 'assert';

let sentryCaptureException: any = (error) => {
console.error(error);
};

function isURL(str) {
try {
// eslint-disable-next-line no-new
Expand Down Expand Up @@ -38,3 +42,13 @@ export function getJSON(jsonOrURL: string | any) {

throw new Error(`Invalid data ${jsonOrURL}`);
}

export function setSentryCaptureException(impl: any) {
sentryCaptureException = impl;
}

export function captureException(error) {
if (sentryCaptureException) {
sentryCaptureException(error);
}
}
14 changes: 12 additions & 2 deletions packages/core/src/message-provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IDIDProvider } from './did-provider';
import { captureException } from './helpers';
import { IWallet } from './types';
import { relayService as defaultRelayService } from '@docknetwork/wallet-sdk-wasm/src/services/relay-service';

Expand Down Expand Up @@ -41,6 +42,7 @@ export function createMessageProvider({
}
await relayService.sendMessage({ keyPairDoc, message, recipientDid });
} catch (error) {
captureException(error);
throw new Error(`Failed to send message: ${error.message}`);
}
},
Expand All @@ -50,10 +52,16 @@ export function createMessageProvider({
const messages = await wallet.getDocumentsByType('DIDCommMessage');
const keyPairDocs = await getKeyPairDocs(didProvider);
for (const { encryptedMessage } of messages) {
const decryptedMessage = await relayService.resolveDidcommMessage({ keyPairDocs, encryptedMessage });
wallet.eventManager.emit('didcomm-message-decrypted', decryptedMessage);
try {
const decryptedMessage = await relayService.resolveDidcommMessage({ keyPairDocs, encryptedMessage });
wallet.eventManager.emit('didcomm-message-decrypted', decryptedMessage);
} catch(err) {
captureException(err);
console.error(err);
}
}
} catch (error) {
captureException(error);
throw new Error(`Failed to process DIDComm messages: ${error.message}`);
}
},
Expand All @@ -68,6 +76,7 @@ export function createMessageProvider({

await wallet.removeDocument(messageId);
} catch (error) {
captureException(error);
throw new Error(`Failed to mark message as read: ${error.message}`);
}
},
Expand Down Expand Up @@ -95,6 +104,7 @@ export function createMessageProvider({

return encryptedMessages;
} catch (error) {
captureException(error);
throw new Error(`Failed to fetch messages: ${error.message}`);
}
},
Expand Down

0 comments on commit 1848b59

Please sign in to comment.