Skip to content

Commit 48c5d22

Browse files
authored
Refactor error handling to remove duplication (#834)
1 parent 657eb63 commit 48c5d22

Some content is hidden

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

58 files changed

+3236
-9224
lines changed

.generator/src/generator/cli.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def cli(specs, output):
4242
env.filters["form_parameter"] = openapi.form_parameter
4343
env.filters["untitle_case"] = formatter.untitle_case
4444
env.filters["response_type"] = openapi.get_type_for_response
45+
env.filters["responses_by_types"] = openapi.responses_by_types
4546
env.filters["docstring"] = formatter.docstring
4647

4748
env.globals["get_references_for_model"] = openapi.get_references_for_model

.generator/src/generator/openapi.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,17 @@ def get_type_for_response(response):
339339
return type_to_typescript(content["schema"])
340340

341341

342+
def responses_by_types(operation):
343+
result = {}
344+
for response_code, response in operation["responses"].items():
345+
response_type = get_type_for_response(response)
346+
if response_type in result:
347+
result[response_type][1].append(response_code)
348+
else:
349+
result[response_type] = [response, [response_code]]
350+
return result.items()
351+
352+
342353
def return_type(operation):
343354
for response in operation.get("responses", {}).values():
344355
for content in response.get("content", {}).values():

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import FormData from "form-data";
1212
import { logger } from "../../../logger";
1313
import { ObjectSerializer } from "../models/ObjectSerializer";
1414
import { ApiException } from "../../datadog-api-client-common/exception";
15-
import { isCodeInRange } from "../../datadog-api-client-common/util";
1615

1716
{% for model in get_api_models(operations)|sort %}
1817
import { {{ model }} } from "../models/{{ model }}";
@@ -163,26 +162,29 @@ export class {{ className }}ResponseProcessor {
163162
*/
164163
public async {{ operationId }}(response: ResponseContext): Promise<{%- if returnType %}{{ returnType }} {%- else %}void{%-endif %}> {
165164
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
166-
{%- for responseCode, response in operation.responses.items() %}
165+
{%- for responseType, (response, responseCodes) in operation|responses_by_types %}
167166
{%- set responseSchema = response|parameter_schema %}
168-
{%- set reponseType = response|response_type %}
169-
if (isCodeInRange("{{ responseCode }}", response.httpStatusCode)) {
170-
{%- if reponseType %}
167+
if (
168+
{%- for responseCode in responseCodes -%}
169+
{%- if not loop.first -%} || {%- endif -%} response.httpStatusCode == {{ responseCode }}
170+
{%- endfor -%}
171+
) {
172+
{%- if responseType %}
171173
{%- if responseSchema and responseSchema.get("format") == "binary" %}
172-
const body: {{ reponseType }} = await response.getBodyAsFile() as {{ returnType }};
174+
const body: {{ responseType }} = await response.getBodyAsFile() as {{ returnType }};
173175
{%- else %}
174-
const body: {{ reponseType }} = ObjectSerializer.deserialize(
176+
const body: {{ responseType }} = ObjectSerializer.deserialize(
175177
ObjectSerializer.parse(await response.body.text(), contentType),
176-
"{{ reponseType }}", "{{ responseSchema.get("format", "") }}"
177-
) as {{ reponseType }};
178+
"{{ responseType }}"
179+
) as {{ responseType }};
178180
{%- endif %}
179-
{%- if responseCode.startswith("2") %}
181+
{%- if responseCodes[0].startswith("2") %}
180182
return body;
181183
{%- else %}
182-
throw new ApiException<{{ reponseType }}>({{ responseCode }}, body);
184+
throw new ApiException<{{ responseType }}>(response.httpStatusCode, body);
183185
{%- endif %}
184186
{%- else %}
185-
{%- if responseCode.startswith("2") %}
187+
{%- if responseCodes[0].startswith("2") %}
186188
return;
187189
{%- else %}
188190
throw new ApiException<string>(response.httpStatusCode, "{{ responseSchema.description }}");

.generator/src/generator/templates/util.j2

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,3 @@
1-
/**
2-
* Returns if a specific http code is in a given code range
3-
* where the code range is defined as a combination of digits
4-
* and "X" (the letter X) with a length of 3
5-
*
6-
* @param codeRange string with length 3 consisting of digits and "X" (the letter X)
7-
* @param code the http status code to be checked against the code range
8-
*/
9-
export function isCodeInRange(codeRange: string, code: number): boolean {
10-
// This is how the default value is encoded in OAG
11-
if (codeRange === "0") {
12-
return true;
13-
}
14-
if (codeRange == code.toString()) {
15-
return true;
16-
} else {
17-
const codeString = code.toString();
18-
if (codeString.length != codeRange.length) {
19-
return false;
20-
}
21-
for (let i = 0; i < codeString.length; i++) {
22-
if (codeRange.charAt(i) != "X" && codeRange.charAt(i) != codeString.charAt(i)) {
23-
return false;
24-
}
25-
}
26-
return true;
27-
}
28-
}
291

302
export class UnparsedObject {
313
unparsedObject:any;

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

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,3 @@
1-
/**
2-
* Returns if a specific http code is in a given code range
3-
* where the code range is defined as a combination of digits
4-
* and "X" (the letter X) with a length of 3
5-
*
6-
* @param codeRange string with length 3 consisting of digits and "X" (the letter X)
7-
* @param code the http status code to be checked against the code range
8-
*/
9-
export function isCodeInRange(codeRange: string, code: number): boolean {
10-
// This is how the default value is encoded in OAG
11-
if (codeRange === "0") {
12-
return true;
13-
}
14-
if (codeRange == code.toString()) {
15-
return true;
16-
} else {
17-
const codeString = code.toString();
18-
if (codeString.length != codeRange.length) {
19-
return false;
20-
}
21-
for (let i = 0; i < codeString.length; i++) {
22-
if (
23-
codeRange.charAt(i) != "X" &&
24-
codeRange.charAt(i) != codeString.charAt(i)
25-
) {
26-
return false;
27-
}
28-
}
29-
return true;
30-
}
31-
}
32-
331
export class UnparsedObject {
342
unparsedObject: any;
353
constructor(data: any) {

0 commit comments

Comments
 (0)