diff --git a/package.json b/package.json index 66689441..b48a5b78 100644 --- a/package.json +++ b/package.json @@ -87,5 +87,8 @@ }, "engines": { "node": "20.12.2" + }, + "dependencies": { + "debug": "^4.3.7" } } \ No newline at end of file diff --git a/packages/encrypted-storage/src/encrypted-store-middleware.ts b/packages/encrypted-storage/src/encrypted-store-middleware.ts index 628c0310..e3d1b993 100644 --- a/packages/encrypted-storage/src/encrypted-store-middleware.ts +++ b/packages/encrypted-storage/src/encrypted-store-middleware.ts @@ -2,6 +2,9 @@ import { IAgent, IEncrypteAndStoreDataResult } from '@vckit/core-types'; import { NextFunction, Request, Response, Router } from 'express'; import interceptor from 'express-interceptor'; import { RequestWithAgent } from './encrypted-store-router.js'; +import Debug from 'debug'; + +const debug = Debug('vckit:encrypted-storage'); /** * @@ -14,7 +17,7 @@ export function encryptedStoreMiddleware(args: { const intercept = interceptor(function ( req: RequestWithAgent, - res: Response + res: Response, ) { return { isInterceptable: function () { @@ -32,7 +35,7 @@ export function encryptedStoreMiddleware(args: { ) { updatedBody = await encryptAndStoreData( req.agent, - JSON.parse(body) + JSON.parse(body), ); } @@ -50,12 +53,17 @@ export function encryptedStoreMiddleware(args: { } async function encryptAndStoreData(agent: IAgent, payload: object) { - const { id, key }: IEncrypteAndStoreDataResult = await agent.execute( - 'encryptAndStoreData', - { - data: payload, - } - ); - - return JSON.stringify({ id, key, credential: payload }); + try { + const { id, key }: IEncrypteAndStoreDataResult = await agent.execute( + 'encryptAndStoreData', + { + data: payload, + }, + ); + + return JSON.stringify({ id, key, credential: payload }); + } catch (error) { + debug(error); + return JSON.stringify({ credential: payload }); + } }