Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: axios error transformation on network errors #126

Merged
merged 2 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [unreleased]

## [5.1.1] - 2024-06-12

### Fixes

- Fixed an issue in the Axios interceptor that caused it to throw when encountering a network error

## [5.1.0] - 2024-06-04

### Changes
Expand Down
57 changes: 57 additions & 0 deletions TestingApp/test/axios2.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,63 @@ describe("Axios AuthHttpRequest class tests", function() {
assert(idRefreshInCookies.length === 0);
});

it("should throw error if refresh fails with a network error", async function() {
await startST();
AuthHttpRequest.addAxiosInterceptors(axiosInstance);
const origFetch = global.fetch;
global.fetch = async (...input) => {
try {
if (input[0].endsWith("/auth/session/refresh")) {
input[0] = "http://localhost:1234/nope";
}
return await origFetch(...input);
} catch (err) {
throw err;
}
};
AuthHttpRequest.init({
apiDomain: BASE_URL,
tokenTransferMethod: "cookie"
});

let userId = "testing-supertokens-react-native";

let loginResponse = await axiosInstance.post(`${BASE_URL}/login`, JSON.stringify({ userId }), {
headers: {
Accept: "application/json",
"Content-Type": "application/json"
}
});
let userIdFromResponse = loginResponse.data;
assertEqual(userId, userIdFromResponse);

cookieJar.setCookieSync("sIdRefreshToken=asdf", `${BASE_URL}/`);

// This is to verify that the cookie is correctly set
let currentCookies = cookieJar.getCookiesSync(`${BASE_URL}/`);
let idRefreshInCookies = currentCookies.filter(i => i.key === "sIdRefreshToken");
let accessTokenInCookies = currentCookies.filter(i => i.key === "sAccessToken");

assert(idRefreshInCookies.length !== 0);
assert(accessTokenInCookies.length !== 0);

//check that the number of times the refreshAPI was called is 0
assert((await getNumberOfTimesRefreshCalled()) === 0);

try {
await axiosInstance("http://localhost:1234/asdf");
await axiosInstance({
url: `${BASE_URL}/`,
method: "GET",
headers: { "Cache-Control": "no-cache, private" }
});
} catch (err) {
assert(err.isAxiosError);
assert.strictEqual(err.code, "ECONNREFUSED");
assert.strictEqual(err.response, undefined);
}
});

it("test that interception happens based on the return value of shouldDoInterceptionBasedOnUrl override", async function() {
await startST();
AuthHttpRequest.addAxiosInterceptors(axiosInstance);
Expand Down
2 changes: 1 addition & 1 deletion lib/build/axiosError.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 30 additions & 24 deletions lib/build/axiosError.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/build/version.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/build/version.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 31 additions & 25 deletions lib/ts/axiosError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,37 +62,43 @@ function enhanceAxiosError(
return error;
}

export async function createAxiosErrorFromFetchResp(response: Response): Promise<AxiosError> {
export async function createAxiosErrorFromFetchResp(responseOrError: Response): Promise<AxiosError> {
const config = {
url: response.url,
headers: response.headers
url: responseOrError.url,
headers: responseOrError.headers
};
const contentType = response.headers.get("content-type");
let data;
if (!contentType || contentType.includes("application/json")) {
try {
data = await response.json();
} catch {
data = await response.text();
const isResponse = "status" in responseOrError;
let axiosResponse;
if (isResponse) {
let data;
const contentType = responseOrError.headers.get("content-type");
if (!contentType || contentType.includes("application/json")) {
try {
data = await responseOrError.json();
} catch {
data = await responseOrError.text();
}
} else if (contentType.includes("text/")) {
data = await responseOrError.text();
} else {
data = await responseOrError.blob();
}
} else if (contentType.includes("text/")) {
data = await response.text();
} else {
data = await response.blob();
}

const axiosResponse = {
data,
status: response.status,
statusText: response.statusText,
headers: response.headers,
config: config,
request: undefined
};
axiosResponse = {
data,
status: responseOrError.status,
statusText: responseOrError.statusText,
headers: responseOrError.headers,
config: config,
request: undefined
};
}
return enhanceAxiosError(
new Error("Request failed with status code " + response.status),
"status" in responseOrError
? new Error("Request failed with status code " + responseOrError.status)
: responseOrError,
config,
undefined,
(responseOrError as any).code,
undefined,
axiosResponse
);
Expand Down
2 changes: 1 addition & 1 deletion lib/ts/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
* License for the specific language governing permissions and limitations
* under the License.
*/
export const package_version = "5.1.0";
export const package_version = "5.1.1";

export const supported_fdi = ["1.16", "1.17", "1.18", "1.19", "2.0", "3.0"];
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "supertokens-react-native",
"version": "5.1.0",
"version": "5.1.1",
"description": "React Native SDK for SuperTokens",
"main": "index.js",
"scripts": {
Expand Down
Loading