Skip to content

Commit 3b8d247

Browse files
authored
Merge pull request #960 from LIT-Protocol/feature/jss-76-bug-request-id-should-be-included-inside-the-thrown-error
feat(network): surface request ids in node errors
2 parents 321c22b + 4d7ca85 commit 3b8d247

File tree

7 files changed

+548
-82
lines changed

7 files changed

+548
-82
lines changed

.changeset/big-tools-study.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@lit-protocol/lit-client': patch
3+
'@lit-protocol/networks': patch
4+
---
5+
6+
Node operations (pkpSign, decrypt, executeJs, session key signing) now emit request-aware errors, letting users share a requestID for log correlation.

packages/lit-client/src/lib/LitClient/createLitClient.ts

Lines changed: 144 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ import {
2121
PaymentManager,
2222
} from '@lit-protocol/networks';
2323

24-
import { DEV_PRIVATE_KEY } from '@lit-protocol/constants';
24+
import {
25+
DEV_PRIVATE_KEY,
26+
LitNodeClientBadConfigError,
27+
LitNodeClientNotReadyError,
28+
ParamsMissingError,
29+
UnsupportedMethodError,
30+
} from '@lit-protocol/constants';
2531
import {
2632
AuthContextSchema2,
2733
AuthData,
@@ -103,7 +109,15 @@ export const createLitClient = async ({
103109
case 'datil':
104110
return _createDatilLitClient();
105111
default:
106-
throw new Error(`Network module ${network.id} not supported`);
112+
throw new UnsupportedMethodError(
113+
{
114+
cause: new Error('Unsupported network module'),
115+
info: {
116+
networkId: network.id,
117+
},
118+
},
119+
`Network module ${network.id} not supported`
120+
);
107121
}
108122
};
109123

@@ -175,7 +189,13 @@ export const _createNagaLitClient = async (
175189

176190
// Initial check to ensure handshakeResult is available after setup
177191
if (!_stateManager.getCallbackResult()) {
178-
throw new Error(
192+
throw new LitNodeClientNotReadyError(
193+
{
194+
cause: new Error('Handshake result missing after initialization'),
195+
info: {
196+
operation: 'initialiseClient',
197+
},
198+
},
179199
'Initial handshake result is not available from state manager. LitClient cannot be initialized.'
180200
);
181201
}
@@ -196,7 +216,13 @@ export const _createNagaLitClient = async (
196216
const currentConnectionInfo = _stateManager.getLatestConnectionInfo();
197217

198218
if (!currentHandshakeResult || !currentConnectionInfo) {
199-
throw new Error(
219+
throw new LitNodeClientNotReadyError(
220+
{
221+
cause: new Error('Handshake result unavailable for pkpSign'),
222+
info: {
223+
operation: 'pkpSign',
224+
},
225+
},
200226
'Handshake result is not available from state manager at the time of pkpSign.'
201227
);
202228
}
@@ -276,7 +302,13 @@ export const _createNagaLitClient = async (
276302
const currentConnectionInfo = _stateManager.getLatestConnectionInfo();
277303

278304
if (!currentHandshakeResult || !currentConnectionInfo) {
279-
throw new Error(
305+
throw new LitNodeClientNotReadyError(
306+
{
307+
cause: new Error('Handshake result unavailable for signSessionKey'),
308+
info: {
309+
operation: 'signSessionKey',
310+
},
311+
},
280312
'Handshake result is not available from state manager at the time of pkpSign.'
281313
);
282314
}
@@ -307,7 +339,8 @@ export const _createNagaLitClient = async (
307339
return await networkModule.api.signSessionKey.handleResponse(
308340
result as any,
309341
params.requestBody.pkpPublicKey,
310-
jitContext
342+
jitContext,
343+
requestId
311344
);
312345
}
313346

@@ -322,7 +355,15 @@ export const _createNagaLitClient = async (
322355
const currentConnectionInfo = _stateManager.getLatestConnectionInfo();
323356

324357
if (!currentHandshakeResult || !currentConnectionInfo) {
325-
throw new Error(
358+
throw new LitNodeClientNotReadyError(
359+
{
360+
cause: new Error(
361+
'Handshake result unavailable for signCustomSessionKey'
362+
),
363+
info: {
364+
operation: 'signCustomSessionKey',
365+
},
366+
},
326367
'Handshake result is not available from state manager at the time of pkpSign.'
327368
);
328369
}
@@ -333,7 +374,15 @@ export const _createNagaLitClient = async (
333374
);
334375

335376
if (!currentHandshakeResult || !currentConnectionInfo) {
336-
throw new Error(
377+
throw new LitNodeClientNotReadyError(
378+
{
379+
cause: new Error(
380+
'Handshake result unavailable for signCustomSessionKey'
381+
),
382+
info: {
383+
operation: 'signCustomSessionKey',
384+
},
385+
},
337386
'Handshake result is not available from state manager at the time of pkpSign.'
338387
);
339388
}
@@ -375,7 +424,13 @@ export const _createNagaLitClient = async (
375424
const currentConnectionInfo = _stateManager.getLatestConnectionInfo();
376425

377426
if (!currentHandshakeResult || !currentConnectionInfo) {
378-
throw new Error(
427+
throw new LitNodeClientNotReadyError(
428+
{
429+
cause: new Error('Handshake result unavailable for executeJs'),
430+
info: {
431+
operation: 'executeJs',
432+
},
433+
},
379434
'Handshake result is not available from state manager at the time of executeJs.'
380435
);
381436
}
@@ -489,13 +544,27 @@ export const _createNagaLitClient = async (
489544
const currentHandshakeResult = _stateManager.getCallbackResult();
490545

491546
if (!currentHandshakeResult) {
492-
throw new Error(
547+
throw new LitNodeClientNotReadyError(
548+
{
549+
cause: new Error('Handshake result unavailable for encrypt'),
550+
info: {
551+
operation: 'encrypt',
552+
},
553+
},
493554
'Handshake result is not available from state manager at the time of encrypt.'
494555
);
495556
}
496557

497558
if (!currentHandshakeResult.coreNodeConfig?.subnetPubKey) {
498-
throw new Error('subnetPubKey cannot be null');
559+
throw new LitNodeClientBadConfigError(
560+
{
561+
cause: new Error('Missing subnetPubKey in handshake result'),
562+
info: {
563+
operation: 'encrypt',
564+
},
565+
},
566+
'subnetPubKey cannot be null'
567+
);
499568
}
500569

501570
// ========== Convert data to Uint8Array ==========
@@ -527,7 +596,15 @@ export const _createNagaLitClient = async (
527596

528597
// ========== Validate Params ==========
529598
if (!_validateEncryptionParams(params)) {
530-
throw new Error(
599+
throw new ParamsMissingError(
600+
{
601+
cause: new Error(
602+
'Required encryption access control parameters missing'
603+
),
604+
info: {
605+
operation: 'encrypt',
606+
},
607+
},
531608
'You must provide either accessControlConditions or evmContractConditions or solRpcConditions or unifiedAccessControlConditions'
532609
);
533610
}
@@ -540,7 +617,13 @@ export const _createNagaLitClient = async (
540617
await getHashedAccessControlConditions(params);
541618

542619
if (!hashOfConditions) {
543-
throw new Error(
620+
throw new ParamsMissingError(
621+
{
622+
cause: new Error('Failed to hash provided access control parameters'),
623+
info: {
624+
operation: 'encrypt',
625+
},
626+
},
544627
'You must provide either accessControlConditions or evmContractConditions or solRpcConditions or unifiedAccessControlConditions'
545628
);
546629
}
@@ -606,7 +689,13 @@ export const _createNagaLitClient = async (
606689
const currentConnectionInfo = _stateManager.getLatestConnectionInfo();
607690

608691
if (!currentHandshakeResult || !currentConnectionInfo) {
609-
throw new Error(
692+
throw new LitNodeClientNotReadyError(
693+
{
694+
cause: new Error('Handshake result unavailable for decrypt'),
695+
info: {
696+
operation: 'decrypt',
697+
},
698+
},
610699
'Handshake result is not available from state manager at the time of decrypt.'
611700
);
612701
}
@@ -617,18 +706,40 @@ export const _createNagaLitClient = async (
617706
);
618707

619708
if (!currentHandshakeResult || !currentConnectionInfo) {
620-
throw new Error(
709+
throw new LitNodeClientNotReadyError(
710+
{
711+
cause: new Error('Handshake result unavailable for decrypt'),
712+
info: {
713+
operation: 'decrypt',
714+
},
715+
},
621716
'Handshake result is not available from state manager at the time of decrypt.'
622717
);
623718
}
624719

625720
if (!currentHandshakeResult.coreNodeConfig?.subnetPubKey) {
626-
throw new Error('subnetPubKey cannot be null');
721+
throw new LitNodeClientBadConfigError(
722+
{
723+
cause: new Error('Missing subnetPubKey in handshake result'),
724+
info: {
725+
operation: 'decrypt',
726+
},
727+
},
728+
'subnetPubKey cannot be null'
729+
);
627730
}
628731

629732
// ========== Validate Params ==========
630733
if (!_validateEncryptionParams(params)) {
631-
throw new Error(
734+
throw new ParamsMissingError(
735+
{
736+
cause: new Error(
737+
'Required decryption access control parameters missing'
738+
),
739+
info: {
740+
operation: 'decrypt',
741+
},
742+
},
632743
'You must provide either accessControlConditions or evmContractConditions or solRpcConditions or unifiedAccessControlConditions'
633744
);
634745
}
@@ -641,7 +752,13 @@ export const _createNagaLitClient = async (
641752
await getHashedAccessControlConditions(params);
642753

643754
if (!hashOfConditions) {
644-
throw new Error(
755+
throw new ParamsMissingError(
756+
{
757+
cause: new Error('Failed to hash provided access control parameters'),
758+
info: {
759+
operation: 'decrypt',
760+
},
761+
},
645762
'You must provide either accessControlConditions or evmContractConditions or solRpcConditions or unifiedAccessControlConditions'
646763
);
647764
}
@@ -1015,7 +1132,15 @@ export const _createNagaLitClient = async (
10151132
type DatilNetworkModule = LitNetworkModule;
10161133

10171134
export const _createDatilLitClient = async () => {
1018-
throw new Error('Datil is not supported yet');
1135+
throw new UnsupportedMethodError(
1136+
{
1137+
cause: new Error('Datil network module is not implemented'),
1138+
info: {
1139+
networkId: 'datil',
1140+
},
1141+
},
1142+
'Datil is not supported yet'
1143+
);
10191144
};
10201145

10211146
export type LitClientType = Awaited<ReturnType<typeof createLitClient>>;

0 commit comments

Comments
 (0)