Skip to content

Commit bd612ac

Browse files
authored
Handle errors in deserialization errors (#836)
If the error schema doesn't match, it swallows the server exception which is very unhelpful. Catch potential errors and raise with the original body instead.
1 parent 494f9e9 commit bd612ac

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+6400
-1882
lines changed

.generator/src/generator/templates/api/api.j2

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ export class {{ className }}ResponseProcessor {
170170
{%- endfor -%}
171171
) {
172172
{%- if responseType %}
173+
{%- if responseCodes[0].startswith("2") %}
173174
{%- if responseSchema and responseSchema.get("format") == "binary" %}
174175
const body: {{ responseType }} = await response.getBodyAsFile() as {{ returnType }};
175176
{%- else %}
@@ -178,10 +179,19 @@ export class {{ className }}ResponseProcessor {
178179
"{{ responseType }}"
179180
) as {{ responseType }};
180181
{%- endif %}
181-
{%- if responseCodes[0].startswith("2") %}
182182
return body;
183183
{%- else %}
184-
throw new ApiException<{{ responseType }}>(response.httpStatusCode, body);
184+
const bodyText = ObjectSerializer.parse(await response.body.text(), contentType);
185+
try {
186+
const body: {{ responseType }} = ObjectSerializer.deserialize(
187+
bodyText,
188+
"{{ responseType }}"
189+
) as {{ responseType }};
190+
throw new ApiException<{{ responseType }}>(response.httpStatusCode, body);
191+
} catch (error) {
192+
logger.info(`Got error deserializing error: ${error}`);
193+
throw new ApiException<{{ responseType }}>(response.httpStatusCode, bodyText);
194+
}
185195
{%- endif %}
186196
{%- else %}
187197
{%- if responseCodes[0].startswith("2") %}

packages/datadog-api-client-v1/apis/AWSIntegrationApi.ts

Lines changed: 154 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
ResponseContext,
1414
} from "../../datadog-api-client-common/http/http";
1515

16+
import { logger } from "../../../logger";
1617
import { ObjectSerializer } from "../models/ObjectSerializer";
1718
import { ApiException } from "../../datadog-api-client-common/exception";
1819

@@ -455,11 +456,23 @@ export class AWSIntegrationApiResponseProcessor {
455456
response.httpStatusCode == 409 ||
456457
response.httpStatusCode == 429
457458
) {
458-
const body: APIErrorResponse = ObjectSerializer.deserialize(
459-
ObjectSerializer.parse(await response.body.text(), contentType),
460-
"APIErrorResponse"
461-
) as APIErrorResponse;
462-
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
459+
const bodyText = ObjectSerializer.parse(
460+
await response.body.text(),
461+
contentType
462+
);
463+
try {
464+
const body: APIErrorResponse = ObjectSerializer.deserialize(
465+
bodyText,
466+
"APIErrorResponse"
467+
) as APIErrorResponse;
468+
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
469+
} catch (error) {
470+
logger.info(`Got error deserializing error: ${error}`);
471+
throw new ApiException<APIErrorResponse>(
472+
response.httpStatusCode,
473+
bodyText
474+
);
475+
}
463476
}
464477

465478
// Work around for missing responses in specification, e.g. for petstore.yaml
@@ -502,11 +515,23 @@ export class AWSIntegrationApiResponseProcessor {
502515
response.httpStatusCode == 403 ||
503516
response.httpStatusCode == 429
504517
) {
505-
const body: APIErrorResponse = ObjectSerializer.deserialize(
506-
ObjectSerializer.parse(await response.body.text(), contentType),
507-
"APIErrorResponse"
508-
) as APIErrorResponse;
509-
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
518+
const bodyText = ObjectSerializer.parse(
519+
await response.body.text(),
520+
contentType
521+
);
522+
try {
523+
const body: APIErrorResponse = ObjectSerializer.deserialize(
524+
bodyText,
525+
"APIErrorResponse"
526+
) as APIErrorResponse;
527+
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
528+
} catch (error) {
529+
logger.info(`Got error deserializing error: ${error}`);
530+
throw new ApiException<APIErrorResponse>(
531+
response.httpStatusCode,
532+
bodyText
533+
);
534+
}
510535
}
511536

512537
// Work around for missing responses in specification, e.g. for petstore.yaml
@@ -551,11 +576,23 @@ export class AWSIntegrationApiResponseProcessor {
551576
response.httpStatusCode == 403 ||
552577
response.httpStatusCode == 429
553578
) {
554-
const body: APIErrorResponse = ObjectSerializer.deserialize(
555-
ObjectSerializer.parse(await response.body.text(), contentType),
556-
"APIErrorResponse"
557-
) as APIErrorResponse;
558-
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
579+
const bodyText = ObjectSerializer.parse(
580+
await response.body.text(),
581+
contentType
582+
);
583+
try {
584+
const body: APIErrorResponse = ObjectSerializer.deserialize(
585+
bodyText,
586+
"APIErrorResponse"
587+
) as APIErrorResponse;
588+
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
589+
} catch (error) {
590+
logger.info(`Got error deserializing error: ${error}`);
591+
throw new ApiException<APIErrorResponse>(
592+
response.httpStatusCode,
593+
bodyText
594+
);
595+
}
559596
}
560597

561598
// Work around for missing responses in specification, e.g. for petstore.yaml
@@ -599,11 +636,23 @@ export class AWSIntegrationApiResponseProcessor {
599636
response.httpStatusCode == 409 ||
600637
response.httpStatusCode == 429
601638
) {
602-
const body: APIErrorResponse = ObjectSerializer.deserialize(
603-
ObjectSerializer.parse(await response.body.text(), contentType),
604-
"APIErrorResponse"
605-
) as APIErrorResponse;
606-
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
639+
const bodyText = ObjectSerializer.parse(
640+
await response.body.text(),
641+
contentType
642+
);
643+
try {
644+
const body: APIErrorResponse = ObjectSerializer.deserialize(
645+
bodyText,
646+
"APIErrorResponse"
647+
) as APIErrorResponse;
648+
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
649+
} catch (error) {
650+
logger.info(`Got error deserializing error: ${error}`);
651+
throw new ApiException<APIErrorResponse>(
652+
response.httpStatusCode,
653+
bodyText
654+
);
655+
}
607656
}
608657

609658
// Work around for missing responses in specification, e.g. for petstore.yaml
@@ -646,11 +695,23 @@ export class AWSIntegrationApiResponseProcessor {
646695
response.httpStatusCode == 403 ||
647696
response.httpStatusCode == 429
648697
) {
649-
const body: APIErrorResponse = ObjectSerializer.deserialize(
650-
ObjectSerializer.parse(await response.body.text(), contentType),
651-
"APIErrorResponse"
652-
) as APIErrorResponse;
653-
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
698+
const bodyText = ObjectSerializer.parse(
699+
await response.body.text(),
700+
contentType
701+
);
702+
try {
703+
const body: APIErrorResponse = ObjectSerializer.deserialize(
704+
bodyText,
705+
"APIErrorResponse"
706+
) as APIErrorResponse;
707+
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
708+
} catch (error) {
709+
logger.info(`Got error deserializing error: ${error}`);
710+
throw new ApiException<APIErrorResponse>(
711+
response.httpStatusCode,
712+
bodyText
713+
);
714+
}
654715
}
655716

656717
// Work around for missing responses in specification, e.g. for petstore.yaml
@@ -691,11 +752,23 @@ export class AWSIntegrationApiResponseProcessor {
691752
return body;
692753
}
693754
if (response.httpStatusCode == 403 || response.httpStatusCode == 429) {
694-
const body: APIErrorResponse = ObjectSerializer.deserialize(
695-
ObjectSerializer.parse(await response.body.text(), contentType),
696-
"APIErrorResponse"
697-
) as APIErrorResponse;
698-
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
755+
const bodyText = ObjectSerializer.parse(
756+
await response.body.text(),
757+
contentType
758+
);
759+
try {
760+
const body: APIErrorResponse = ObjectSerializer.deserialize(
761+
bodyText,
762+
"APIErrorResponse"
763+
) as APIErrorResponse;
764+
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
765+
} catch (error) {
766+
logger.info(`Got error deserializing error: ${error}`);
767+
throw new ApiException<APIErrorResponse>(
768+
response.httpStatusCode,
769+
bodyText
770+
);
771+
}
699772
}
700773

701774
// Work around for missing responses in specification, e.g. for petstore.yaml
@@ -740,11 +813,23 @@ export class AWSIntegrationApiResponseProcessor {
740813
response.httpStatusCode == 403 ||
741814
response.httpStatusCode == 429
742815
) {
743-
const body: APIErrorResponse = ObjectSerializer.deserialize(
744-
ObjectSerializer.parse(await response.body.text(), contentType),
745-
"APIErrorResponse"
746-
) as APIErrorResponse;
747-
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
816+
const bodyText = ObjectSerializer.parse(
817+
await response.body.text(),
818+
contentType
819+
);
820+
try {
821+
const body: APIErrorResponse = ObjectSerializer.deserialize(
822+
bodyText,
823+
"APIErrorResponse"
824+
) as APIErrorResponse;
825+
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
826+
} catch (error) {
827+
logger.info(`Got error deserializing error: ${error}`);
828+
throw new ApiException<APIErrorResponse>(
829+
response.httpStatusCode,
830+
bodyText
831+
);
832+
}
748833
}
749834

750835
// Work around for missing responses in specification, e.g. for petstore.yaml
@@ -789,11 +874,23 @@ export class AWSIntegrationApiResponseProcessor {
789874
response.httpStatusCode == 403 ||
790875
response.httpStatusCode == 429
791876
) {
792-
const body: APIErrorResponse = ObjectSerializer.deserialize(
793-
ObjectSerializer.parse(await response.body.text(), contentType),
794-
"APIErrorResponse"
795-
) as APIErrorResponse;
796-
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
877+
const bodyText = ObjectSerializer.parse(
878+
await response.body.text(),
879+
contentType
880+
);
881+
try {
882+
const body: APIErrorResponse = ObjectSerializer.deserialize(
883+
bodyText,
884+
"APIErrorResponse"
885+
) as APIErrorResponse;
886+
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
887+
} catch (error) {
888+
logger.info(`Got error deserializing error: ${error}`);
889+
throw new ApiException<APIErrorResponse>(
890+
response.httpStatusCode,
891+
bodyText
892+
);
893+
}
797894
}
798895

799896
// Work around for missing responses in specification, e.g. for petstore.yaml
@@ -837,11 +934,23 @@ export class AWSIntegrationApiResponseProcessor {
837934
response.httpStatusCode == 409 ||
838935
response.httpStatusCode == 429
839936
) {
840-
const body: APIErrorResponse = ObjectSerializer.deserialize(
841-
ObjectSerializer.parse(await response.body.text(), contentType),
842-
"APIErrorResponse"
843-
) as APIErrorResponse;
844-
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
937+
const bodyText = ObjectSerializer.parse(
938+
await response.body.text(),
939+
contentType
940+
);
941+
try {
942+
const body: APIErrorResponse = ObjectSerializer.deserialize(
943+
bodyText,
944+
"APIErrorResponse"
945+
) as APIErrorResponse;
946+
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
947+
} catch (error) {
948+
logger.info(`Got error deserializing error: ${error}`);
949+
throw new ApiException<APIErrorResponse>(
950+
response.httpStatusCode,
951+
bodyText
952+
);
953+
}
845954
}
846955

847956
// Work around for missing responses in specification, e.g. for petstore.yaml

0 commit comments

Comments
 (0)