Skip to content

Commit 5d733b9

Browse files
authored
[javascript] Fix thrown error message when API status code is unknown (#1160)
## Motivation The response body was not read correctly, causing the SDK to throw errors like `Message: "Unknown API Status Code! Body: "[object Object]"`. This happens when the API returns an unexpected status code (like 500) or when the content type is not defined. ## Solution Using `await response.body.text()` should return the actual response body.
1 parent 364ba3e commit 5d733b9

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

javascript/templates/api/api.mustache

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ export class {{classname}}ResponseProcessor {
165165
*/
166166
public async {{nickname}}(response: ResponseContext): Promise<{{#returnType}}{{{returnType}}}{{/returnType}} {{^returnType}}void{{/returnType}}> {
167167
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
168+
169+
if (contentType === undefined) {
170+
throw new Error("Cannot parse content. No Content-Type defined. Body: " + await response.body.text());
171+
}
172+
168173
{{#responses}}
169174
if (isCodeInRange("{{code}}", response.httpStatusCode)) {
170175
{{#dataType}}
@@ -214,8 +219,8 @@ export class {{classname}}ResponseProcessor {
214219
{{/returnType}}
215220
}
216221

217-
let body = response.body || "";
218-
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
222+
let body = await response.body.text();
223+
throw new ApiException<string>(response.httpStatusCode, body);
219224
}
220225

221226
{{/operation}}

0 commit comments

Comments
 (0)