Skip to content

Commit 0727407

Browse files
joeheymingjheyming
andauthored
fix(typescript-fetch): Handle cors errors. (#12960)
* fix(typescript-fetch): Handle cors errors. If there is a communication error, e.g. an OPTIONS request returns 404 not found, then the whole request is cancelled and there is no response object (it is undefined). I observed the following error: TypeError: Cannot read properties of undefined (reading 'status') Basically response was undefined. In order to circumvent this issue, we do a check to make sure response is "truthy", which works for objects. With these code changes, it will throw a ResponseError, which is what you would expect instead of a TypeError. * regenerate typescript-fetch stuff * retry code generation Co-authored-by: Joe Heyming <jheyming@Roblox.com>
1 parent 8e66294 commit 0727407

File tree

14 files changed

+78
-13
lines changed

14 files changed

+78
-13
lines changed

modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export class BaseAPI {
105105
protected async request(context: RequestOpts, initOverrides?: RequestInit | InitOverrideFunction): Promise<Response> {
106106
const { url, init } = await this.createFetchParams(context, initOverrides);
107107
const response = await this.fetchApi(url, init);
108-
if (response.status >= 200 && response.status < 300) {
108+
if (response && (response.status >= 200 && response.status < 300)) {
109109
return response;
110110
}
111111
throw new ResponseError(response, 'Response returned an error code');
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* tslint:disable */
2+
/* eslint-disable */
3+
/**
4+
* Example
5+
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
6+
*
7+
* The version of the OpenAPI document: 1.0.0
8+
*
9+
*
10+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
11+
* https://openapi-generator.tech
12+
* Do not edit the class manually.
13+
*/
14+
15+
import { exists, mapValues } from '../runtime';
16+
/**
17+
*
18+
* @export
19+
* @interface ClubOwner
20+
*/
21+
export interface ClubOwner {
22+
/**
23+
*
24+
* @type {string}
25+
* @memberof ClubOwner
26+
*/
27+
name?: string;
28+
}
29+
30+
/**
31+
* Check if a given object implements the ClubOwner interface.
32+
*/
33+
export function instanceOfClubOwner(value: object): boolean {
34+
let isInstance = true;
35+
36+
return isInstance;
37+
}
38+
39+
export function ClubOwnerFromJSON(json: any): ClubOwner {
40+
return ClubOwnerFromJSONTyped(json, false);
41+
}
42+
43+
export function ClubOwnerFromJSONTyped(json: any, ignoreDiscriminator: boolean): ClubOwner {
44+
if ((json === undefined) || (json === null)) {
45+
return json;
46+
}
47+
return {
48+
49+
'name': !exists(json, 'name') ? undefined : json['name'],
50+
};
51+
}
52+
53+
export function ClubOwnerToJSON(value?: ClubOwner | null): any {
54+
if (value === undefined) {
55+
return undefined;
56+
}
57+
if (value === null) {
58+
return null;
59+
}
60+
return {
61+
62+
'name': value.name,
63+
};
64+
}
65+

samples/client/petstore/typescript-fetch/builds/allOf-readonly/runtime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export class BaseAPI {
116116
protected async request(context: RequestOpts, initOverrides?: RequestInit | InitOverrideFunction): Promise<Response> {
117117
const { url, init } = await this.createFetchParams(context, initOverrides);
118118
const response = await this.fetchApi(url, init);
119-
if (response.status >= 200 && response.status < 300) {
119+
if (response && (response.status >= 200 && response.status < 300)) {
120120
return response;
121121
}
122122
throw new ResponseError(response, 'Response returned an error code');

samples/client/petstore/typescript-fetch/builds/default-v3.0/runtime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export class BaseAPI {
116116
protected async request(context: RequestOpts, initOverrides?: RequestInit | InitOverrideFunction): Promise<Response> {
117117
const { url, init } = await this.createFetchParams(context, initOverrides);
118118
const response = await this.fetchApi(url, init);
119-
if (response.status >= 200 && response.status < 300) {
119+
if (response && (response.status >= 200 && response.status < 300)) {
120120
return response;
121121
}
122122
throw new ResponseError(response, 'Response returned an error code');

samples/client/petstore/typescript-fetch/builds/default/runtime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export class BaseAPI {
116116
protected async request(context: RequestOpts, initOverrides?: RequestInit | InitOverrideFunction): Promise<Response> {
117117
const { url, init } = await this.createFetchParams(context, initOverrides);
118118
const response = await this.fetchApi(url, init);
119-
if (response.status >= 200 && response.status < 300) {
119+
if (response && (response.status >= 200 && response.status < 300)) {
120120
return response;
121121
}
122122
throw new ResponseError(response, 'Response returned an error code');

samples/client/petstore/typescript-fetch/builds/enum/runtime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export class BaseAPI {
116116
protected async request(context: RequestOpts, initOverrides?: RequestInit | InitOverrideFunction): Promise<Response> {
117117
const { url, init } = await this.createFetchParams(context, initOverrides);
118118
const response = await this.fetchApi(url, init);
119-
if (response.status >= 200 && response.status < 300) {
119+
if (response && (response.status >= 200 && response.status < 300)) {
120120
return response;
121121
}
122122
throw new ResponseError(response, 'Response returned an error code');

samples/client/petstore/typescript-fetch/builds/es6-target/src/runtime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export class BaseAPI {
116116
protected async request(context: RequestOpts, initOverrides?: RequestInit | InitOverrideFunction): Promise<Response> {
117117
const { url, init } = await this.createFetchParams(context, initOverrides);
118118
const response = await this.fetchApi(url, init);
119-
if (response.status >= 200 && response.status < 300) {
119+
if (response && (response.status >= 200 && response.status < 300)) {
120120
return response;
121121
}
122122
throw new ResponseError(response, 'Response returned an error code');

samples/client/petstore/typescript-fetch/builds/multiple-parameters/runtime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export class BaseAPI {
116116
protected async request(context: RequestOpts, initOverrides?: RequestInit | InitOverrideFunction): Promise<Response> {
117117
const { url, init } = await this.createFetchParams(context, initOverrides);
118118
const response = await this.fetchApi(url, init);
119-
if (response.status >= 200 && response.status < 300) {
119+
if (response && (response.status >= 200 && response.status < 300)) {
120120
return response;
121121
}
122122
throw new ResponseError(response, 'Response returned an error code');

samples/client/petstore/typescript-fetch/builds/prefix-parameter-interfaces/src/runtime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export class BaseAPI {
116116
protected async request(context: RequestOpts, initOverrides?: RequestInit | InitOverrideFunction): Promise<Response> {
117117
const { url, init } = await this.createFetchParams(context, initOverrides);
118118
const response = await this.fetchApi(url, init);
119-
if (response.status >= 200 && response.status < 300) {
119+
if (response && (response.status >= 200 && response.status < 300)) {
120120
return response;
121121
}
122122
throw new ResponseError(response, 'Response returned an error code');

samples/client/petstore/typescript-fetch/builds/sagas-and-records/src/runtime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export class BaseAPI {
116116
protected async request(context: RequestOpts, initOverrides?: RequestInit | InitOverrideFunction): Promise<Response> {
117117
const { url, init } = await this.createFetchParams(context, initOverrides);
118118
const response = await this.fetchApi(url, init);
119-
if (response.status >= 200 && response.status < 300) {
119+
if (response && (response.status >= 200 && response.status < 300)) {
120120
return response;
121121
}
122122
throw new ResponseError(response, 'Response returned an error code');

0 commit comments

Comments
 (0)