Skip to content

Commit

Permalink
Handle errors in deserialization errors (#836)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
therve authored Sep 14, 2022
1 parent 494f9e9 commit bd612ac
Show file tree
Hide file tree
Showing 54 changed files with 6,400 additions and 1,882 deletions.
14 changes: 12 additions & 2 deletions .generator/src/generator/templates/api/api.j2
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export class {{ className }}ResponseProcessor {
{%- endfor -%}
) {
{%- if responseType %}
{%- if responseCodes[0].startswith("2") %}
{%- if responseSchema and responseSchema.get("format") == "binary" %}
const body: {{ responseType }} = await response.getBodyAsFile() as {{ returnType }};
{%- else %}
Expand All @@ -178,10 +179,19 @@ export class {{ className }}ResponseProcessor {
"{{ responseType }}"
) as {{ responseType }};
{%- endif %}
{%- if responseCodes[0].startswith("2") %}
return body;
{%- else %}
throw new ApiException<{{ responseType }}>(response.httpStatusCode, body);
const bodyText = ObjectSerializer.parse(await response.body.text(), contentType);
try {
const body: {{ responseType }} = ObjectSerializer.deserialize(
bodyText,
"{{ responseType }}"
) as {{ responseType }};
throw new ApiException<{{ responseType }}>(response.httpStatusCode, body);
} catch (error) {
logger.info(`Got error deserializing error: ${error}`);
throw new ApiException<{{ responseType }}>(response.httpStatusCode, bodyText);
}
{%- endif %}
{%- else %}
{%- if responseCodes[0].startswith("2") %}
Expand Down
199 changes: 154 additions & 45 deletions packages/datadog-api-client-v1/apis/AWSIntegrationApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
ResponseContext,
} from "../../datadog-api-client-common/http/http";

import { logger } from "../../../logger";
import { ObjectSerializer } from "../models/ObjectSerializer";
import { ApiException } from "../../datadog-api-client-common/exception";

Expand Down Expand Up @@ -455,11 +456,23 @@ export class AWSIntegrationApiResponseProcessor {
response.httpStatusCode == 409 ||
response.httpStatusCode == 429
) {
const body: APIErrorResponse = ObjectSerializer.deserialize(
ObjectSerializer.parse(await response.body.text(), contentType),
"APIErrorResponse"
) as APIErrorResponse;
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
const bodyText = ObjectSerializer.parse(
await response.body.text(),
contentType
);
try {
const body: APIErrorResponse = ObjectSerializer.deserialize(
bodyText,
"APIErrorResponse"
) as APIErrorResponse;
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
} catch (error) {
logger.info(`Got error deserializing error: ${error}`);
throw new ApiException<APIErrorResponse>(
response.httpStatusCode,
bodyText
);
}
}

// Work around for missing responses in specification, e.g. for petstore.yaml
Expand Down Expand Up @@ -502,11 +515,23 @@ export class AWSIntegrationApiResponseProcessor {
response.httpStatusCode == 403 ||
response.httpStatusCode == 429
) {
const body: APIErrorResponse = ObjectSerializer.deserialize(
ObjectSerializer.parse(await response.body.text(), contentType),
"APIErrorResponse"
) as APIErrorResponse;
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
const bodyText = ObjectSerializer.parse(
await response.body.text(),
contentType
);
try {
const body: APIErrorResponse = ObjectSerializer.deserialize(
bodyText,
"APIErrorResponse"
) as APIErrorResponse;
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
} catch (error) {
logger.info(`Got error deserializing error: ${error}`);
throw new ApiException<APIErrorResponse>(
response.httpStatusCode,
bodyText
);
}
}

// Work around for missing responses in specification, e.g. for petstore.yaml
Expand Down Expand Up @@ -551,11 +576,23 @@ export class AWSIntegrationApiResponseProcessor {
response.httpStatusCode == 403 ||
response.httpStatusCode == 429
) {
const body: APIErrorResponse = ObjectSerializer.deserialize(
ObjectSerializer.parse(await response.body.text(), contentType),
"APIErrorResponse"
) as APIErrorResponse;
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
const bodyText = ObjectSerializer.parse(
await response.body.text(),
contentType
);
try {
const body: APIErrorResponse = ObjectSerializer.deserialize(
bodyText,
"APIErrorResponse"
) as APIErrorResponse;
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
} catch (error) {
logger.info(`Got error deserializing error: ${error}`);
throw new ApiException<APIErrorResponse>(
response.httpStatusCode,
bodyText
);
}
}

// Work around for missing responses in specification, e.g. for petstore.yaml
Expand Down Expand Up @@ -599,11 +636,23 @@ export class AWSIntegrationApiResponseProcessor {
response.httpStatusCode == 409 ||
response.httpStatusCode == 429
) {
const body: APIErrorResponse = ObjectSerializer.deserialize(
ObjectSerializer.parse(await response.body.text(), contentType),
"APIErrorResponse"
) as APIErrorResponse;
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
const bodyText = ObjectSerializer.parse(
await response.body.text(),
contentType
);
try {
const body: APIErrorResponse = ObjectSerializer.deserialize(
bodyText,
"APIErrorResponse"
) as APIErrorResponse;
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
} catch (error) {
logger.info(`Got error deserializing error: ${error}`);
throw new ApiException<APIErrorResponse>(
response.httpStatusCode,
bodyText
);
}
}

// Work around for missing responses in specification, e.g. for petstore.yaml
Expand Down Expand Up @@ -646,11 +695,23 @@ export class AWSIntegrationApiResponseProcessor {
response.httpStatusCode == 403 ||
response.httpStatusCode == 429
) {
const body: APIErrorResponse = ObjectSerializer.deserialize(
ObjectSerializer.parse(await response.body.text(), contentType),
"APIErrorResponse"
) as APIErrorResponse;
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
const bodyText = ObjectSerializer.parse(
await response.body.text(),
contentType
);
try {
const body: APIErrorResponse = ObjectSerializer.deserialize(
bodyText,
"APIErrorResponse"
) as APIErrorResponse;
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
} catch (error) {
logger.info(`Got error deserializing error: ${error}`);
throw new ApiException<APIErrorResponse>(
response.httpStatusCode,
bodyText
);
}
}

// Work around for missing responses in specification, e.g. for petstore.yaml
Expand Down Expand Up @@ -691,11 +752,23 @@ export class AWSIntegrationApiResponseProcessor {
return body;
}
if (response.httpStatusCode == 403 || response.httpStatusCode == 429) {
const body: APIErrorResponse = ObjectSerializer.deserialize(
ObjectSerializer.parse(await response.body.text(), contentType),
"APIErrorResponse"
) as APIErrorResponse;
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
const bodyText = ObjectSerializer.parse(
await response.body.text(),
contentType
);
try {
const body: APIErrorResponse = ObjectSerializer.deserialize(
bodyText,
"APIErrorResponse"
) as APIErrorResponse;
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
} catch (error) {
logger.info(`Got error deserializing error: ${error}`);
throw new ApiException<APIErrorResponse>(
response.httpStatusCode,
bodyText
);
}
}

// Work around for missing responses in specification, e.g. for petstore.yaml
Expand Down Expand Up @@ -740,11 +813,23 @@ export class AWSIntegrationApiResponseProcessor {
response.httpStatusCode == 403 ||
response.httpStatusCode == 429
) {
const body: APIErrorResponse = ObjectSerializer.deserialize(
ObjectSerializer.parse(await response.body.text(), contentType),
"APIErrorResponse"
) as APIErrorResponse;
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
const bodyText = ObjectSerializer.parse(
await response.body.text(),
contentType
);
try {
const body: APIErrorResponse = ObjectSerializer.deserialize(
bodyText,
"APIErrorResponse"
) as APIErrorResponse;
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
} catch (error) {
logger.info(`Got error deserializing error: ${error}`);
throw new ApiException<APIErrorResponse>(
response.httpStatusCode,
bodyText
);
}
}

// Work around for missing responses in specification, e.g. for petstore.yaml
Expand Down Expand Up @@ -789,11 +874,23 @@ export class AWSIntegrationApiResponseProcessor {
response.httpStatusCode == 403 ||
response.httpStatusCode == 429
) {
const body: APIErrorResponse = ObjectSerializer.deserialize(
ObjectSerializer.parse(await response.body.text(), contentType),
"APIErrorResponse"
) as APIErrorResponse;
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
const bodyText = ObjectSerializer.parse(
await response.body.text(),
contentType
);
try {
const body: APIErrorResponse = ObjectSerializer.deserialize(
bodyText,
"APIErrorResponse"
) as APIErrorResponse;
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
} catch (error) {
logger.info(`Got error deserializing error: ${error}`);
throw new ApiException<APIErrorResponse>(
response.httpStatusCode,
bodyText
);
}
}

// Work around for missing responses in specification, e.g. for petstore.yaml
Expand Down Expand Up @@ -837,11 +934,23 @@ export class AWSIntegrationApiResponseProcessor {
response.httpStatusCode == 409 ||
response.httpStatusCode == 429
) {
const body: APIErrorResponse = ObjectSerializer.deserialize(
ObjectSerializer.parse(await response.body.text(), contentType),
"APIErrorResponse"
) as APIErrorResponse;
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
const bodyText = ObjectSerializer.parse(
await response.body.text(),
contentType
);
try {
const body: APIErrorResponse = ObjectSerializer.deserialize(
bodyText,
"APIErrorResponse"
) as APIErrorResponse;
throw new ApiException<APIErrorResponse>(response.httpStatusCode, body);
} catch (error) {
logger.info(`Got error deserializing error: ${error}`);
throw new ApiException<APIErrorResponse>(
response.httpStatusCode,
bodyText
);
}
}

// Work around for missing responses in specification, e.g. for petstore.yaml
Expand Down
Loading

0 comments on commit bd612ac

Please sign in to comment.