Skip to content

Commit e292dc9

Browse files
pvdzjdalton
authored andcommitted
Read error body and return it as .cause (#290)
1 parent 96bfa42 commit e292dc9

File tree

1 file changed

+56
-31
lines changed

1 file changed

+56
-31
lines changed

src/index.ts

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export type SocketSdkErrorType<T extends SocketSdkOperations> = Omit<
3434
'error'
3535
> & {
3636
error: string
37+
cause?: unknown
3738
}
3839

3940
export type SocketSdkResultType<T extends SocketSdkOperations> =
@@ -331,7 +332,7 @@ export class SocketSdk {
331332
try {
332333
res = await this.#createBatchPurlRequest(queryParams, componentsObj)
333334
} catch (e) {
334-
return this.#handleApiError<'batchPackageFetch'>(e)
335+
return await this.#handleApiError<'batchPackageFetch'>(e)
335336
}
336337
const rli = readline.createInterface({
337338
input: res,
@@ -343,9 +344,9 @@ export class SocketSdk {
343344
}
344345
}
345346

346-
#handleApiError<T extends SocketSdkOperations>(
347+
async #handleApiError<T extends SocketSdkOperations>(
347348
error: unknown
348-
): SocketSdkErrorType<T> {
349+
): Promise<SocketSdkErrorType<T>> {
349350
if (!(error instanceof ResponseError)) {
350351
throw new Error('Unexpected Socket API error', {
351352
cause: error
@@ -357,10 +358,34 @@ export class SocketSdk {
357358
cause: error
358359
})
359360
}
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+
360384
return {
361385
success: false as const,
362386
status: statusCode!,
363-
error: error.message ?? ''
387+
error: error.message ?? '',
388+
cause: body
364389
} as unknown as SocketSdkErrorType<T>
365390
}
366391

@@ -382,7 +407,7 @@ export class SocketSdk {
382407
try {
383408
res = await this.#createBatchPurlRequest(queryParams, componentsObj)
384409
} catch (e) {
385-
return this.#handleApiError<'batchPackageFetch'>(e)
410+
return await this.#handleApiError<'batchPackageFetch'>(e)
386411
}
387412
// Parse the newline delimited JSON response.
388413
const rl = readline.createInterface({
@@ -502,7 +527,7 @@ export class SocketSdk {
502527
)
503528
return this.#handleApiSuccess<'createDependenciesSnapshot'>(data)
504529
} catch (e) {
505-
return this.#handleApiError<'createDependenciesSnapshot'>(e)
530+
return await this.#handleApiError<'createDependenciesSnapshot'>(e)
506531
}
507532
}
508533

@@ -525,7 +550,7 @@ export class SocketSdk {
525550
)
526551
return this.#handleApiSuccess<'CreateOrgFullScan'>(data)
527552
} catch (e) {
528-
return this.#handleApiError<'CreateOrgFullScan'>(e)
553+
return await this.#handleApiError<'CreateOrgFullScan'>(e)
529554
}
530555
}
531556

@@ -544,7 +569,7 @@ export class SocketSdk {
544569
)
545570
return this.#handleApiSuccess<'createOrgRepo'>(data)
546571
} catch (e) {
547-
return this.#handleApiError<'createOrgRepo'>(e)
572+
return await this.#handleApiError<'createOrgRepo'>(e)
548573
}
549574
}
550575

@@ -572,7 +597,7 @@ export class SocketSdk {
572597
)
573598
return this.#handleApiSuccess<'createReport'>(data)
574599
} catch (e) {
575-
return this.#handleApiError<'createReport'>(e)
600+
return await this.#handleApiError<'createReport'>(e)
576601
}
577602
}
578603

@@ -603,7 +628,7 @@ export class SocketSdk {
603628
)
604629
return this.#handleApiSuccess<'deleteOrgFullScan'>(data)
605630
} catch (e) {
606-
return this.#handleApiError<'deleteOrgFullScan'>(e)
631+
return await this.#handleApiError<'deleteOrgFullScan'>(e)
607632
}
608633
}
609634

@@ -621,7 +646,7 @@ export class SocketSdk {
621646
)
622647
return this.#handleApiSuccess<'deleteOrgRepo'>(data)
623648
} catch (e) {
624-
return this.#handleApiError<'deleteOrgRepo'>(e)
649+
return await this.#handleApiError<'deleteOrgRepo'>(e)
625650
}
626651
}
627652

@@ -639,7 +664,7 @@ export class SocketSdk {
639664
)
640665
return this.#handleApiSuccess<'getAuditLogEvents'>(data)
641666
} catch (e) {
642-
return this.#handleApiError<'getAuditLogEvents'>(e)
667+
return await this.#handleApiError<'getAuditLogEvents'>(e)
643668
}
644669
}
645670

@@ -657,7 +682,7 @@ export class SocketSdk {
657682
)
658683
return this.#handleApiSuccess<'getIssuesByNPMPackage'>(data)
659684
} catch (e) {
660-
return this.#handleApiError<'getIssuesByNPMPackage'>(e)
685+
return await this.#handleApiError<'getIssuesByNPMPackage'>(e)
661686
}
662687
}
663688

@@ -674,7 +699,7 @@ export class SocketSdk {
674699
)
675700
return this.#handleApiSuccess<'getOrgAnalytics'>(data)
676701
} catch (e) {
677-
return this.#handleApiError<'getOrgAnalytics'>(e)
702+
return await this.#handleApiError<'getOrgAnalytics'>(e)
678703
}
679704
}
680705

@@ -685,7 +710,7 @@ export class SocketSdk {
685710
)
686711
return this.#handleApiSuccess<'getOrganizations'>(data)
687712
} catch (e) {
688-
return this.#handleApiError<'getOrganizations'>(e)
713+
return await this.#handleApiError<'getOrganizations'>(e)
689714
}
690715
}
691716

@@ -712,7 +737,7 @@ export class SocketSdk {
712737
}
713738
return this.#handleApiSuccess<'getOrgFullScan'>(res)
714739
} catch (e) {
715-
return this.#handleApiError<'getOrgFullScan'>(e)
740+
return await this.#handleApiError<'getOrgFullScan'>(e)
716741
}
717742
}
718743

@@ -730,7 +755,7 @@ export class SocketSdk {
730755
)
731756
return this.#handleApiSuccess<'getOrgFullScanList'>(data)
732757
} catch (e) {
733-
return this.#handleApiError<'getOrgFullScanList'>(e)
758+
return await this.#handleApiError<'getOrgFullScanList'>(e)
734759
}
735760
}
736761

@@ -748,7 +773,7 @@ export class SocketSdk {
748773
)
749774
return this.#handleApiSuccess<'getOrgFullScanMetadata'>(data)
750775
} catch (e) {
751-
return this.#handleApiError<'getOrgFullScanMetadata'>(e)
776+
return await this.#handleApiError<'getOrgFullScanMetadata'>(e)
752777
}
753778
}
754779

@@ -765,7 +790,7 @@ export class SocketSdk {
765790
)
766791
return this.#handleApiSuccess<'getOrgLicensePolicy'>(data)
767792
} catch (e) {
768-
return this.#handleApiError<'getOrgLicensePolicy'>(e)
793+
return await this.#handleApiError<'getOrgLicensePolicy'>(e)
769794
}
770795
}
771796

@@ -786,7 +811,7 @@ export class SocketSdk {
786811
)
787812
return this.#handleApiSuccess<'getOrgRepo'>(data)
788813
} catch (e) {
789-
return this.#handleApiError<'getOrgRepo'>(e)
814+
return await this.#handleApiError<'getOrgRepo'>(e)
790815
}
791816
}
792817

@@ -804,7 +829,7 @@ export class SocketSdk {
804829
)
805830
return this.#handleApiSuccess<'getOrgRepoList'>(data)
806831
} catch (e) {
807-
return this.#handleApiError<'getOrgRepoList'>(e)
832+
return await this.#handleApiError<'getOrgRepoList'>(e)
808833
}
809834
}
810835

@@ -821,7 +846,7 @@ export class SocketSdk {
821846
)
822847
return this.#handleApiSuccess<'getOrgSecurityPolicy'>(data)
823848
} catch (e) {
824-
return this.#handleApiError<'getOrgSecurityPolicy'>(e)
849+
return await this.#handleApiError<'getOrgSecurityPolicy'>(e)
825850
}
826851
}
827852

@@ -832,7 +857,7 @@ export class SocketSdk {
832857
)
833858
return this.#handleApiSuccess<'getQuota'>(data)
834859
} catch (e) {
835-
return this.#handleApiError<'getQuota'>(e)
860+
return await this.#handleApiError<'getQuota'>(e)
836861
}
837862
}
838863

@@ -850,7 +875,7 @@ export class SocketSdk {
850875
)
851876
return this.#handleApiSuccess<'getRepoAnalytics'>(data)
852877
} catch (e) {
853-
return this.#handleApiError<'getRepoAnalytics'>(e)
878+
return await this.#handleApiError<'getRepoAnalytics'>(e)
854879
}
855880
}
856881

@@ -865,7 +890,7 @@ export class SocketSdk {
865890
)
866891
return this.#handleApiSuccess<'getReport'>(data)
867892
} catch (e) {
868-
return this.#handleApiError<'getReport'>(e)
893+
return await this.#handleApiError<'getReport'>(e)
869894
}
870895
}
871896

@@ -876,7 +901,7 @@ export class SocketSdk {
876901
)
877902
return this.#handleApiSuccess<'getReportList'>(data)
878903
} catch (e) {
879-
return this.#handleApiError<'getReportList'>(e)
904+
return await this.#handleApiError<'getReportList'>(e)
880905
}
881906
}
882907

@@ -893,7 +918,7 @@ export class SocketSdk {
893918
)
894919
return this.#handleApiSuccess<'getReportSupportedFiles'>(data)
895920
} catch (e) {
896-
return this.#handleApiError<'getReportSupportedFiles'>(e)
921+
return await this.#handleApiError<'getReportSupportedFiles'>(e)
897922
}
898923
}
899924

@@ -911,7 +936,7 @@ export class SocketSdk {
911936
)
912937
return this.#handleApiSuccess<'getScoreByNPMPackage'>(data)
913938
} catch (e) {
914-
return this.#handleApiError<'getScoreByNPMPackage'>(e)
939+
return await this.#handleApiError<'getScoreByNPMPackage'>(e)
915940
}
916941
}
917942

@@ -929,7 +954,7 @@ export class SocketSdk {
929954
)
930955
return this.#handleApiSuccess<'postSettings'>(data)
931956
} catch (e) {
932-
return this.#handleApiError<'postSettings'>(e)
957+
return await this.#handleApiError<'postSettings'>(e)
933958
}
934959
}
935960

@@ -947,7 +972,7 @@ export class SocketSdk {
947972
)
948973
return this.#handleApiSuccess<'searchDependencies'>(data)
949974
} catch (e) {
950-
return this.#handleApiError<'searchDependencies'>(e)
975+
return await this.#handleApiError<'searchDependencies'>(e)
951976
}
952977
}
953978

@@ -967,7 +992,7 @@ export class SocketSdk {
967992
)
968993
return this.#handleApiSuccess<'updateOrgRepo'>(data)
969994
} catch (e) {
970-
return this.#handleApiError<'updateOrgRepo'>(e)
995+
return await this.#handleApiError<'updateOrgRepo'>(e)
971996
}
972997
}
973998
}

0 commit comments

Comments
 (0)