Skip to content

Commit 9f93d8c

Browse files
authored
Fix error handling in APIs (#875)
We never returned the parsed object type. This also fixes ApiException prototype.
1 parent 33ebabb commit 9f93d8c

Some content is hidden

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

57 files changed

+1177
-786
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,17 @@ export class {{ className }}ResponseProcessor {
182182
return body;
183183
{%- else %}
184184
const bodyText = ObjectSerializer.parse(await response.body.text(), contentType);
185+
let body: {{ responseType }};
185186
try {
186-
const body: {{ responseType }} = ObjectSerializer.deserialize(
187+
body = ObjectSerializer.deserialize(
187188
bodyText,
188189
"{{ responseType }}"
189190
) as {{ responseType }};
190-
throw new ApiException<{{ responseType }}>(response.httpStatusCode, body);
191191
} catch (error) {
192192
logger.info(`Got error deserializing error: ${error}`);
193193
throw new ApiException<{{ responseType }}>(response.httpStatusCode, bodyText);
194194
}
195+
throw new ApiException<{{ responseType }}>(response.httpStatusCode, body);
195196
{%- endif %}
196197
{%- else %}
197198
{%- if responseCodes[0].startswith("2") %}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@
1010
export class ApiException<T> extends Error {
1111
public constructor(public code: number, public body: T) {
1212
super("HTTP-Code: " + code + "\nMessage: " + JSON.stringify(body));
13+
Object.setPrototypeOf(this, ApiException.prototype);
14+
this.code = code;
15+
this.body = body;
1316
}
1417
}

features/step_definitions/request_steps.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ When("the request is sent", async function (this: World) {
132132
);
133133
}
134134
} catch (error) {
135-
this.response = error;
135+
if (error instanceof datadogApiClient.client.ApiException) {
136+
this.response = error.body;
137+
} else {
138+
this.response = error;
139+
}
136140
logger.debug(error);
137141
if (this.requestContext === undefined) {
138142
throw error;
@@ -193,7 +197,11 @@ When("the request with pagination is sent", async function (this: World) {
193197
}
194198
this.response = response;
195199
} catch (error) {
196-
this.response = error;
200+
if (error instanceof datadogApiClient.client.ApiException) {
201+
this.response = error.body;
202+
} else {
203+
this.response = error;
204+
}
197205
logger.debug(error);
198206
if (this.requestContext === undefined) {
199207
throw error;

packages/datadog-api-client-common/exception.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@
1010
export class ApiException<T> extends Error {
1111
public constructor(public code: number, public body: T) {
1212
super("HTTP-Code: " + code + "\nMessage: " + JSON.stringify(body));
13+
Object.setPrototypeOf(this, ApiException.prototype);
14+
this.code = code;
15+
this.body = body;
1316
}
1417
}

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

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -460,19 +460,20 @@ export class AWSIntegrationApiResponseProcessor {
460460
await response.body.text(),
461461
contentType
462462
);
463+
let body: APIErrorResponse;
463464
try {
464-
const body: APIErrorResponse = ObjectSerializer.deserialize(
465+
body = ObjectSerializer.deserialize(
465466
bodyText,
466467
"APIErrorResponse"
467468
) as APIErrorResponse;
468-
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
469469
} catch (error) {
470470
logger.info(`Got error deserializing error: ${error}`);
471471
throw new ApiException<APIErrorResponse>(
472472
response.httpStatusCode,
473473
bodyText
474474
);
475475
}
476+
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
476477
}
477478

478479
// Work around for missing responses in specification, e.g. for petstore.yaml
@@ -519,19 +520,20 @@ export class AWSIntegrationApiResponseProcessor {
519520
await response.body.text(),
520521
contentType
521522
);
523+
let body: APIErrorResponse;
522524
try {
523-
const body: APIErrorResponse = ObjectSerializer.deserialize(
525+
body = ObjectSerializer.deserialize(
524526
bodyText,
525527
"APIErrorResponse"
526528
) as APIErrorResponse;
527-
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
528529
} catch (error) {
529530
logger.info(`Got error deserializing error: ${error}`);
530531
throw new ApiException<APIErrorResponse>(
531532
response.httpStatusCode,
532533
bodyText
533534
);
534535
}
536+
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
535537
}
536538

537539
// Work around for missing responses in specification, e.g. for petstore.yaml
@@ -580,19 +582,20 @@ export class AWSIntegrationApiResponseProcessor {
580582
await response.body.text(),
581583
contentType
582584
);
585+
let body: APIErrorResponse;
583586
try {
584-
const body: APIErrorResponse = ObjectSerializer.deserialize(
587+
body = ObjectSerializer.deserialize(
585588
bodyText,
586589
"APIErrorResponse"
587590
) as APIErrorResponse;
588-
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
589591
} catch (error) {
590592
logger.info(`Got error deserializing error: ${error}`);
591593
throw new ApiException<APIErrorResponse>(
592594
response.httpStatusCode,
593595
bodyText
594596
);
595597
}
598+
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
596599
}
597600

598601
// Work around for missing responses in specification, e.g. for petstore.yaml
@@ -640,19 +643,20 @@ export class AWSIntegrationApiResponseProcessor {
640643
await response.body.text(),
641644
contentType
642645
);
646+
let body: APIErrorResponse;
643647
try {
644-
const body: APIErrorResponse = ObjectSerializer.deserialize(
648+
body = ObjectSerializer.deserialize(
645649
bodyText,
646650
"APIErrorResponse"
647651
) as APIErrorResponse;
648-
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
649652
} catch (error) {
650653
logger.info(`Got error deserializing error: ${error}`);
651654
throw new ApiException<APIErrorResponse>(
652655
response.httpStatusCode,
653656
bodyText
654657
);
655658
}
659+
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
656660
}
657661

658662
// Work around for missing responses in specification, e.g. for petstore.yaml
@@ -699,19 +703,20 @@ export class AWSIntegrationApiResponseProcessor {
699703
await response.body.text(),
700704
contentType
701705
);
706+
let body: APIErrorResponse;
702707
try {
703-
const body: APIErrorResponse = ObjectSerializer.deserialize(
708+
body = ObjectSerializer.deserialize(
704709
bodyText,
705710
"APIErrorResponse"
706711
) as APIErrorResponse;
707-
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
708712
} catch (error) {
709713
logger.info(`Got error deserializing error: ${error}`);
710714
throw new ApiException<APIErrorResponse>(
711715
response.httpStatusCode,
712716
bodyText
713717
);
714718
}
719+
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
715720
}
716721

717722
// Work around for missing responses in specification, e.g. for petstore.yaml
@@ -756,19 +761,20 @@ export class AWSIntegrationApiResponseProcessor {
756761
await response.body.text(),
757762
contentType
758763
);
764+
let body: APIErrorResponse;
759765
try {
760-
const body: APIErrorResponse = ObjectSerializer.deserialize(
766+
body = ObjectSerializer.deserialize(
761767
bodyText,
762768
"APIErrorResponse"
763769
) as APIErrorResponse;
764-
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
765770
} catch (error) {
766771
logger.info(`Got error deserializing error: ${error}`);
767772
throw new ApiException<APIErrorResponse>(
768773
response.httpStatusCode,
769774
bodyText
770775
);
771776
}
777+
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
772778
}
773779

774780
// Work around for missing responses in specification, e.g. for petstore.yaml
@@ -817,19 +823,20 @@ export class AWSIntegrationApiResponseProcessor {
817823
await response.body.text(),
818824
contentType
819825
);
826+
let body: APIErrorResponse;
820827
try {
821-
const body: APIErrorResponse = ObjectSerializer.deserialize(
828+
body = ObjectSerializer.deserialize(
822829
bodyText,
823830
"APIErrorResponse"
824831
) as APIErrorResponse;
825-
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
826832
} catch (error) {
827833
logger.info(`Got error deserializing error: ${error}`);
828834
throw new ApiException<APIErrorResponse>(
829835
response.httpStatusCode,
830836
bodyText
831837
);
832838
}
839+
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
833840
}
834841

835842
// Work around for missing responses in specification, e.g. for petstore.yaml
@@ -878,19 +885,20 @@ export class AWSIntegrationApiResponseProcessor {
878885
await response.body.text(),
879886
contentType
880887
);
888+
let body: APIErrorResponse;
881889
try {
882-
const body: APIErrorResponse = ObjectSerializer.deserialize(
890+
body = ObjectSerializer.deserialize(
883891
bodyText,
884892
"APIErrorResponse"
885893
) as APIErrorResponse;
886-
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
887894
} catch (error) {
888895
logger.info(`Got error deserializing error: ${error}`);
889896
throw new ApiException<APIErrorResponse>(
890897
response.httpStatusCode,
891898
bodyText
892899
);
893900
}
901+
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
894902
}
895903

896904
// Work around for missing responses in specification, e.g. for petstore.yaml
@@ -938,19 +946,20 @@ export class AWSIntegrationApiResponseProcessor {
938946
await response.body.text(),
939947
contentType
940948
);
949+
let body: APIErrorResponse;
941950
try {
942-
const body: APIErrorResponse = ObjectSerializer.deserialize(
951+
body = ObjectSerializer.deserialize(
943952
bodyText,
944953
"APIErrorResponse"
945954
) as APIErrorResponse;
946-
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
947955
} catch (error) {
948956
logger.info(`Got error deserializing error: ${error}`);
949957
throw new ApiException<APIErrorResponse>(
950958
response.httpStatusCode,
951959
bodyText
952960
);
953961
}
962+
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
954963
}
955964

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

0 commit comments

Comments
 (0)