@@ -34,6 +34,7 @@ export type SocketSdkErrorType<T extends SocketSdkOperations> = Omit<
34
34
'error'
35
35
> & {
36
36
error : string
37
+ cause ?: unknown
37
38
}
38
39
39
40
export type SocketSdkResultType < T extends SocketSdkOperations > =
@@ -331,7 +332,7 @@ export class SocketSdk {
331
332
try {
332
333
res = await this . #createBatchPurlRequest( queryParams , componentsObj )
333
334
} catch ( e ) {
334
- return this . #handleApiError< 'batchPackageFetch' > ( e )
335
+ return await this . #handleApiError< 'batchPackageFetch' > ( e )
335
336
}
336
337
const rli = readline . createInterface ( {
337
338
input : res ,
@@ -343,9 +344,9 @@ export class SocketSdk {
343
344
}
344
345
}
345
346
346
- #handleApiError< T extends SocketSdkOperations > (
347
+ async #handleApiError< T extends SocketSdkOperations > (
347
348
error : unknown
348
- ) : SocketSdkErrorType < T > {
349
+ ) : Promise < SocketSdkErrorType < T > > {
349
350
if ( ! ( error instanceof ResponseError ) ) {
350
351
throw new Error ( 'Unexpected Socket API error' , {
351
352
cause : error
@@ -357,10 +358,34 @@ export class SocketSdk {
357
358
cause : error
358
359
} )
359
360
}
361
+
362
+ // The error payload may give a meaningful hint as to what went wrong.
363
+
364
+ const bodyStr = await new Promise ( ( resolve ) => {
365
+ const chunks : Buffer [ ] = [ ] ;
366
+ error . response . on ( 'data' , ( chunk :Buffer ) => chunks . push ( chunk ) ) ;
367
+ error . response . on ( 'end' , ( ) => resolve ( Buffer . concat ( chunks ) . toString ( 'utf8' ) ) ) ;
368
+ error . response . on ( 'error' , ( ) => resolve ( '(there was an error reading the body content)' ) ) ;
369
+ } ) ;
370
+
371
+ // Try to parse the body as JSON, fallback to treating it as plaintext
372
+
373
+ let body
374
+ try {
375
+ body = JSON . parse ( String ( bodyStr || '' ) ) as any
376
+ // A 400 should return an actionable message
377
+ if ( body ?. error ?. message ) {
378
+ body = body . error . message
379
+ }
380
+ } catch {
381
+ body = bodyStr
382
+ }
383
+
360
384
return {
361
385
success : false as const ,
362
386
status : statusCode ! ,
363
- error : error . message ?? ''
387
+ error : error . message ?? '' ,
388
+ cause : body
364
389
} as unknown as SocketSdkErrorType < T >
365
390
}
366
391
@@ -382,7 +407,7 @@ export class SocketSdk {
382
407
try {
383
408
res = await this . #createBatchPurlRequest( queryParams , componentsObj )
384
409
} catch ( e ) {
385
- return this . #handleApiError< 'batchPackageFetch' > ( e )
410
+ return await this . #handleApiError< 'batchPackageFetch' > ( e )
386
411
}
387
412
// Parse the newline delimited JSON response.
388
413
const rl = readline . createInterface ( {
@@ -502,7 +527,7 @@ export class SocketSdk {
502
527
)
503
528
return this . #handleApiSuccess< 'createDependenciesSnapshot' > ( data )
504
529
} catch ( e ) {
505
- return this . #handleApiError< 'createDependenciesSnapshot' > ( e )
530
+ return await this . #handleApiError< 'createDependenciesSnapshot' > ( e )
506
531
}
507
532
}
508
533
@@ -525,7 +550,7 @@ export class SocketSdk {
525
550
)
526
551
return this . #handleApiSuccess< 'CreateOrgFullScan' > ( data )
527
552
} catch ( e ) {
528
- return this . #handleApiError< 'CreateOrgFullScan' > ( e )
553
+ return await this . #handleApiError< 'CreateOrgFullScan' > ( e )
529
554
}
530
555
}
531
556
@@ -544,7 +569,7 @@ export class SocketSdk {
544
569
)
545
570
return this . #handleApiSuccess< 'createOrgRepo' > ( data )
546
571
} catch ( e ) {
547
- return this . #handleApiError< 'createOrgRepo' > ( e )
572
+ return await this . #handleApiError< 'createOrgRepo' > ( e )
548
573
}
549
574
}
550
575
@@ -572,7 +597,7 @@ export class SocketSdk {
572
597
)
573
598
return this . #handleApiSuccess< 'createReport' > ( data )
574
599
} catch ( e ) {
575
- return this . #handleApiError< 'createReport' > ( e )
600
+ return await this . #handleApiError< 'createReport' > ( e )
576
601
}
577
602
}
578
603
@@ -603,7 +628,7 @@ export class SocketSdk {
603
628
)
604
629
return this . #handleApiSuccess< 'deleteOrgFullScan' > ( data )
605
630
} catch ( e ) {
606
- return this . #handleApiError< 'deleteOrgFullScan' > ( e )
631
+ return await this . #handleApiError< 'deleteOrgFullScan' > ( e )
607
632
}
608
633
}
609
634
@@ -621,7 +646,7 @@ export class SocketSdk {
621
646
)
622
647
return this . #handleApiSuccess< 'deleteOrgRepo' > ( data )
623
648
} catch ( e ) {
624
- return this . #handleApiError< 'deleteOrgRepo' > ( e )
649
+ return await this . #handleApiError< 'deleteOrgRepo' > ( e )
625
650
}
626
651
}
627
652
@@ -639,7 +664,7 @@ export class SocketSdk {
639
664
)
640
665
return this . #handleApiSuccess< 'getAuditLogEvents' > ( data )
641
666
} catch ( e ) {
642
- return this . #handleApiError< 'getAuditLogEvents' > ( e )
667
+ return await this . #handleApiError< 'getAuditLogEvents' > ( e )
643
668
}
644
669
}
645
670
@@ -657,7 +682,7 @@ export class SocketSdk {
657
682
)
658
683
return this . #handleApiSuccess< 'getIssuesByNPMPackage' > ( data )
659
684
} catch ( e ) {
660
- return this . #handleApiError< 'getIssuesByNPMPackage' > ( e )
685
+ return await this . #handleApiError< 'getIssuesByNPMPackage' > ( e )
661
686
}
662
687
}
663
688
@@ -674,7 +699,7 @@ export class SocketSdk {
674
699
)
675
700
return this . #handleApiSuccess< 'getOrgAnalytics' > ( data )
676
701
} catch ( e ) {
677
- return this . #handleApiError< 'getOrgAnalytics' > ( e )
702
+ return await this . #handleApiError< 'getOrgAnalytics' > ( e )
678
703
}
679
704
}
680
705
@@ -685,7 +710,7 @@ export class SocketSdk {
685
710
)
686
711
return this . #handleApiSuccess< 'getOrganizations' > ( data )
687
712
} catch ( e ) {
688
- return this . #handleApiError< 'getOrganizations' > ( e )
713
+ return await this . #handleApiError< 'getOrganizations' > ( e )
689
714
}
690
715
}
691
716
@@ -712,7 +737,7 @@ export class SocketSdk {
712
737
}
713
738
return this . #handleApiSuccess< 'getOrgFullScan' > ( res )
714
739
} catch ( e ) {
715
- return this . #handleApiError< 'getOrgFullScan' > ( e )
740
+ return await this . #handleApiError< 'getOrgFullScan' > ( e )
716
741
}
717
742
}
718
743
@@ -730,7 +755,7 @@ export class SocketSdk {
730
755
)
731
756
return this . #handleApiSuccess< 'getOrgFullScanList' > ( data )
732
757
} catch ( e ) {
733
- return this . #handleApiError< 'getOrgFullScanList' > ( e )
758
+ return await this . #handleApiError< 'getOrgFullScanList' > ( e )
734
759
}
735
760
}
736
761
@@ -748,7 +773,7 @@ export class SocketSdk {
748
773
)
749
774
return this . #handleApiSuccess< 'getOrgFullScanMetadata' > ( data )
750
775
} catch ( e ) {
751
- return this . #handleApiError< 'getOrgFullScanMetadata' > ( e )
776
+ return await this . #handleApiError< 'getOrgFullScanMetadata' > ( e )
752
777
}
753
778
}
754
779
@@ -765,7 +790,7 @@ export class SocketSdk {
765
790
)
766
791
return this . #handleApiSuccess< 'getOrgLicensePolicy' > ( data )
767
792
} catch ( e ) {
768
- return this . #handleApiError< 'getOrgLicensePolicy' > ( e )
793
+ return await this . #handleApiError< 'getOrgLicensePolicy' > ( e )
769
794
}
770
795
}
771
796
@@ -786,7 +811,7 @@ export class SocketSdk {
786
811
)
787
812
return this . #handleApiSuccess< 'getOrgRepo' > ( data )
788
813
} catch ( e ) {
789
- return this . #handleApiError< 'getOrgRepo' > ( e )
814
+ return await this . #handleApiError< 'getOrgRepo' > ( e )
790
815
}
791
816
}
792
817
@@ -804,7 +829,7 @@ export class SocketSdk {
804
829
)
805
830
return this . #handleApiSuccess< 'getOrgRepoList' > ( data )
806
831
} catch ( e ) {
807
- return this . #handleApiError< 'getOrgRepoList' > ( e )
832
+ return await this . #handleApiError< 'getOrgRepoList' > ( e )
808
833
}
809
834
}
810
835
@@ -821,7 +846,7 @@ export class SocketSdk {
821
846
)
822
847
return this . #handleApiSuccess< 'getOrgSecurityPolicy' > ( data )
823
848
} catch ( e ) {
824
- return this . #handleApiError< 'getOrgSecurityPolicy' > ( e )
849
+ return await this . #handleApiError< 'getOrgSecurityPolicy' > ( e )
825
850
}
826
851
}
827
852
@@ -832,7 +857,7 @@ export class SocketSdk {
832
857
)
833
858
return this . #handleApiSuccess< 'getQuota' > ( data )
834
859
} catch ( e ) {
835
- return this . #handleApiError< 'getQuota' > ( e )
860
+ return await this . #handleApiError< 'getQuota' > ( e )
836
861
}
837
862
}
838
863
@@ -850,7 +875,7 @@ export class SocketSdk {
850
875
)
851
876
return this . #handleApiSuccess< 'getRepoAnalytics' > ( data )
852
877
} catch ( e ) {
853
- return this . #handleApiError< 'getRepoAnalytics' > ( e )
878
+ return await this . #handleApiError< 'getRepoAnalytics' > ( e )
854
879
}
855
880
}
856
881
@@ -865,7 +890,7 @@ export class SocketSdk {
865
890
)
866
891
return this . #handleApiSuccess< 'getReport' > ( data )
867
892
} catch ( e ) {
868
- return this . #handleApiError< 'getReport' > ( e )
893
+ return await this . #handleApiError< 'getReport' > ( e )
869
894
}
870
895
}
871
896
@@ -876,7 +901,7 @@ export class SocketSdk {
876
901
)
877
902
return this . #handleApiSuccess< 'getReportList' > ( data )
878
903
} catch ( e ) {
879
- return this . #handleApiError< 'getReportList' > ( e )
904
+ return await this . #handleApiError< 'getReportList' > ( e )
880
905
}
881
906
}
882
907
@@ -893,7 +918,7 @@ export class SocketSdk {
893
918
)
894
919
return this . #handleApiSuccess< 'getReportSupportedFiles' > ( data )
895
920
} catch ( e ) {
896
- return this . #handleApiError< 'getReportSupportedFiles' > ( e )
921
+ return await this . #handleApiError< 'getReportSupportedFiles' > ( e )
897
922
}
898
923
}
899
924
@@ -911,7 +936,7 @@ export class SocketSdk {
911
936
)
912
937
return this . #handleApiSuccess< 'getScoreByNPMPackage' > ( data )
913
938
} catch ( e ) {
914
- return this . #handleApiError< 'getScoreByNPMPackage' > ( e )
939
+ return await this . #handleApiError< 'getScoreByNPMPackage' > ( e )
915
940
}
916
941
}
917
942
@@ -929,7 +954,7 @@ export class SocketSdk {
929
954
)
930
955
return this . #handleApiSuccess< 'postSettings' > ( data )
931
956
} catch ( e ) {
932
- return this . #handleApiError< 'postSettings' > ( e )
957
+ return await this . #handleApiError< 'postSettings' > ( e )
933
958
}
934
959
}
935
960
@@ -947,7 +972,7 @@ export class SocketSdk {
947
972
)
948
973
return this . #handleApiSuccess< 'searchDependencies' > ( data )
949
974
} catch ( e ) {
950
- return this . #handleApiError< 'searchDependencies' > ( e )
975
+ return await this . #handleApiError< 'searchDependencies' > ( e )
951
976
}
952
977
}
953
978
@@ -967,7 +992,7 @@ export class SocketSdk {
967
992
)
968
993
return this . #handleApiSuccess< 'updateOrgRepo' > ( data )
969
994
} catch ( e ) {
970
- return this . #handleApiError< 'updateOrgRepo' > ( e )
995
+ return await this . #handleApiError< 'updateOrgRepo' > ( e )
971
996
}
972
997
}
973
998
}
0 commit comments