@@ -253,15 +253,17 @@ export class MongoDBOIDCPluginImpl implements MongoDBOIDCPlugin {
253
253
throw new MongoDBOIDCError (
254
254
`Stored OIDC data could not be deserialized: ${
255
255
( err as Error ) . message
256
- } `
256
+ } `,
257
+ { cause : err , codeName : 'DeserializeFormatMismatch' }
257
258
) ;
258
259
}
259
260
260
261
if ( original . oidcPluginStateVersion !== 1 ) {
261
262
throw new MongoDBOIDCError (
262
263
`Stored OIDC data could not be deserialized because of a version mismatch (got ${ JSON . stringify (
263
264
original . oidcPluginStateVersion
264
- ) } , expected 1)`
265
+ ) } , expected 1)`,
266
+ { codeName : 'DeserializeVersionMismatch' }
265
267
) ;
266
268
}
267
269
@@ -353,13 +355,16 @@ export class MongoDBOIDCPluginImpl implements MongoDBOIDCPlugin {
353
355
serverMetadata : IdPServerInfo & Pick < OIDCCallbackParams , 'username' >
354
356
) : UserOIDCAuthState {
355
357
if ( ! serverMetadata . issuer || typeof serverMetadata . issuer !== 'string' ) {
356
- throw new MongoDBOIDCError ( `'issuer' is missing` ) ;
358
+ throw new MongoDBOIDCError ( `'issuer' is missing` , {
359
+ codeName : 'MissingIssuer' ,
360
+ } ) ;
357
361
}
358
362
validateSecureHTTPUrl ( serverMetadata . issuer , 'issuer' ) ;
359
363
360
364
if ( ! serverMetadata . clientId ) {
361
365
throw new MongoDBOIDCError (
362
- 'No clientId passed in server OIDC metadata object'
366
+ 'No clientId passed in server OIDC metadata object' ,
367
+ { codeName : 'MissingClientId' }
363
368
) ;
364
369
}
365
370
@@ -502,6 +507,7 @@ export class MongoDBOIDCPluginImpl implements MongoDBOIDCPlugin {
502
507
) } : ${ messageFromError ( err ) } `,
503
508
{
504
509
cause : err ,
510
+ codeName : 'IssuerMetadataDiscoveryFailed' ,
505
511
}
506
512
) ;
507
513
}
@@ -538,7 +544,8 @@ export class MongoDBOIDCPluginImpl implements MongoDBOIDCPlugin {
538
544
new URL ( options . url ) ;
539
545
if ( ! / ^ [ a - z A - Z 0 - 9 % / : ; _ . , = @ - ] + $ / . test ( options . url ) ) {
540
546
throw new MongoDBOIDCError (
541
- `Unexpected format for internally generated URL: '${ options . url } '`
547
+ `Unexpected format for internally generated URL: '${ options . url } '` ,
548
+ { codeName : 'GeneratedUrlInvalidForOpenBrowserCommand' }
542
549
) ;
543
550
}
544
551
this . logger . emit ( 'mongodb-oidc-plugin:open-browser' , {
@@ -547,7 +554,8 @@ export class MongoDBOIDCPluginImpl implements MongoDBOIDCPlugin {
547
554
if ( this . options . openBrowser === false ) {
548
555
// We should never really get to this point
549
556
throw new MongoDBOIDCError (
550
- 'Cannot open browser if `openBrowser` is false'
557
+ 'Cannot open browser if `openBrowser` is false' ,
558
+ { codeName : 'OpenBrowserDisabled' }
551
559
) ;
552
560
}
553
561
if ( typeof this . options . openBrowser === 'function' ) {
@@ -567,7 +575,9 @@ export class MongoDBOIDCPluginImpl implements MongoDBOIDCPlugin {
567
575
child . unref ( ) ;
568
576
return child ;
569
577
}
570
- throw new MongoDBOIDCError ( 'Unknown format for `openBrowser`' ) ;
578
+ throw new MongoDBOIDCError ( 'Unknown format for `openBrowser`' , {
579
+ codeName : 'OpenBrowserOptionFormatUnknown' ,
580
+ } ) ;
571
581
}
572
582
573
583
private async notifyDeviceFlow (
@@ -580,7 +590,8 @@ export class MongoDBOIDCPluginImpl implements MongoDBOIDCPlugin {
580
590
if ( ! this . options . notifyDeviceFlow ) {
581
591
// Should never happen.
582
592
throw new MongoDBOIDCError (
583
- 'notifyDeviceFlow() requested but not provided'
593
+ 'notifyDeviceFlow() requested but not provided' ,
594
+ { codeName : 'DeviceFlowNotEnabled' }
584
595
) ;
585
596
}
586
597
this . logger . emit ( 'mongodb-oidc-plugin:notify-device-flow' ) ;
@@ -605,7 +616,8 @@ export class MongoDBOIDCPluginImpl implements MongoDBOIDCPlugin {
605
616
throw new MongoDBOIDCError (
606
617
`ID token expected, but not found. Expected claims: ${ JSON . stringify (
607
618
state . lastIdTokenClaims
608
- ) } `
619
+ ) } `,
620
+ { codeName : 'IDTokenClaimsMismatchTokenMissing' }
609
621
) ;
610
622
}
611
623
@@ -614,14 +626,17 @@ export class MongoDBOIDCPluginImpl implements MongoDBOIDCPlugin {
614
626
state . lastIdTokenClaims &&
615
627
state . lastIdTokenClaims . noIdToken
616
628
) {
617
- throw new MongoDBOIDCError ( `Unexpected ID token received.` ) ;
629
+ throw new MongoDBOIDCError ( `Unexpected ID token received.` , {
630
+ codeName : 'IDTokenClaimsMismatchTokenUnexpectedlyPresent' ,
631
+ } ) ;
618
632
}
619
633
620
634
if ( tokenSet . idToken ) {
621
635
const idTokenClaims = tokenSet . idTokenClaims ;
622
636
if ( ! idTokenClaims )
623
637
throw new MongoDBOIDCError (
624
- 'Internal error: id_token set but claims() unavailable'
638
+ 'Internal error: id_token set but claims() unavailable' ,
639
+ { codeName : 'IDTokenClaimsUnavailable' }
625
640
) ;
626
641
if ( state . lastIdTokenClaims && ! state . lastIdTokenClaims . noIdToken ) {
627
642
for ( const claim of [ 'aud' , 'sub' ] as const ) {
@@ -635,7 +650,8 @@ export class MongoDBOIDCPluginImpl implements MongoDBOIDCPlugin {
635
650
636
651
if ( knownClaim !== newClaim ) {
637
652
throw new MongoDBOIDCError (
638
- `Unexpected '${ claim } ' field in id token: Expected ${ knownClaim } , saw ${ newClaim } `
653
+ `Unexpected '${ claim } ' field in id token: Expected ${ knownClaim } , saw ${ newClaim } ` ,
654
+ { codeName : 'IDTokenClaimsMismatchClaimMismatch' }
639
655
) ;
640
656
}
641
657
}
@@ -807,15 +823,16 @@ export class MongoDBOIDCPluginImpl implements MongoDBOIDCPlugin {
807
823
`Opening browser failed with '${ messageFromError (
808
824
err
809
825
) } '${ extraErrorInfo ( ) } `,
810
- { cause : err }
826
+ { cause : err , codeName : 'BrowserOpenFailedSpawnError' }
811
827
)
812
828
)
813
829
) ;
814
830
browserHandle ?. once ( 'exit' , ( code ) => {
815
831
if ( code !== 0 )
816
832
reject (
817
833
new MongoDBOIDCError (
818
- `Opening browser failed with exit code ${ code } ${ extraErrorInfo ( ) } `
834
+ `Opening browser failed with exit code ${ code } ${ extraErrorInfo ( ) } ` ,
835
+ { codeName : 'BrowserOpenFailedNonZeroExit' }
819
836
)
820
837
) ;
821
838
} ) ;
@@ -831,7 +848,11 @@ export class MongoDBOIDCPluginImpl implements MongoDBOIDCPlugin {
831
848
. call (
832
849
null ,
833
850
( ) =>
834
- reject ( new MongoDBOIDCError ( 'Opening browser timed out' ) ) ,
851
+ reject (
852
+ new MongoDBOIDCError ( 'Opening browser timed out' , {
853
+ codeName : 'BrowserOpenTimeout' ,
854
+ } )
855
+ ) ,
835
856
this . options . openBrowserTimeout ?? kDefaultOpenBrowserTimeout
836
857
)
837
858
?. unref ?.( ) ;
@@ -988,9 +1009,13 @@ export class MongoDBOIDCPluginImpl implements MongoDBOIDCPlugin {
988
1009
}
989
1010
990
1011
if ( passIdTokenAsAccessToken && ! state . currentTokenSet ?. set ?. idToken ) {
991
- throw new MongoDBOIDCError ( 'Could not retrieve valid ID token' ) ;
1012
+ throw new MongoDBOIDCError ( 'Could not retrieve valid ID token' , {
1013
+ codeName : 'IDTokenMissingFromTokenSet' ,
1014
+ } ) ;
992
1015
} else if ( ! state . currentTokenSet ?. set ?. accessToken ) {
993
- throw new MongoDBOIDCError ( 'Could not retrieve valid access token' ) ;
1016
+ throw new MongoDBOIDCError ( 'Could not retrieve valid access token' , {
1017
+ codeName : 'AccessTokenMissingFromTokenSet' ,
1018
+ } ) ;
994
1019
}
995
1020
} catch ( err : unknown ) {
996
1021
this . logger . emit ( 'mongodb-oidc-plugin:auth-failed' , {
@@ -1058,18 +1083,22 @@ export class MongoDBOIDCPluginImpl implements MongoDBOIDCPlugin {
1058
1083
if ( params . version !== 1 ) {
1059
1084
throw new MongoDBOIDCError (
1060
1085
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1061
- `OIDC MongoDB driver protocol mismatch: unknown version ${ params . version } `
1086
+ `OIDC MongoDB driver protocol mismatch: unknown version ${ params . version } ` ,
1087
+ { codeName : 'ProtocolVersionMismatch' }
1062
1088
) ;
1063
1089
}
1064
1090
1065
1091
if ( this . destroyed ) {
1066
1092
throw new MongoDBOIDCError (
1067
- 'This OIDC plugin instance has been destroyed and is no longer active'
1093
+ 'This OIDC plugin instance has been destroyed and is no longer active' ,
1094
+ { codeName : 'PluginInstanceDestroyed' }
1068
1095
) ;
1069
1096
}
1070
1097
1071
1098
if ( ! params . idpInfo ) {
1072
- throw new MongoDBOIDCError ( 'No IdP information provided' ) ;
1099
+ throw new MongoDBOIDCError ( 'No IdP information provided' , {
1100
+ codeName : 'IdPInfoMissing' ,
1101
+ } ) ;
1073
1102
}
1074
1103
1075
1104
const state = this . getAuthState ( {
0 commit comments