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: add extra error logging to auth response errors #6140

Merged
merged 1 commit into from
Jun 26, 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
5 changes: 5 additions & 0 deletions .changeset/tame-news-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

fix: add extra error logging to auth response errors
33 changes: 30 additions & 3 deletions packages/wrangler/src/user/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ import { generateAuthUrl } from "./generate-auth-url";
import { generateRandomState } from "./generate-random-state";
import type { ChooseAccountItem } from "./choose-account";
import type { ParsedUrlQuery } from "node:querystring";
import type { Response } from "undici";

export type ApiCredentials =
| {
Expand Down Expand Up @@ -728,7 +729,7 @@ async function exchangeRefreshTokenForAccessToken(): Promise<AccessContext> {
}
} else {
try {
const json = (await response.json()) as TokenResponse;
const json = (await getJSONFromResponse(response)) as TokenResponse;
if ("error" in json) {
throw json.error;
}
Expand Down Expand Up @@ -793,7 +794,9 @@ async function exchangeAuthCodeForAccessToken(): Promise<AccessContext> {

const response = await fetchAuthToken(params);
if (!response.ok) {
const { error } = (await response.json()) as { error: string };
const { error } = (await getJSONFromResponse(response)) as {
error: string;
};
// .catch((_) => ({ error: "invalid_json" }));
if (error === "invalid_grant") {
logger.log("Expired! Auth code or refresh token needs to be renewed.");
Expand All @@ -802,7 +805,7 @@ async function exchangeAuthCodeForAccessToken(): Promise<AccessContext> {
}
throw toErrorClass(error);
}
const json = (await response.json()) as TokenResponse;
const json = (await getJSONFromResponse(response)) as TokenResponse;
if ("error" in json) {
throw new Error(json.error);
}
Expand Down Expand Up @@ -1249,3 +1252,27 @@ async function fetchAuthToken(body: URLSearchParams) {
headers,
});
}

async function getJSONFromResponse(response: Response) {
const text = await response.text();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor, but if you passed in the text string to this function, it would reduce its responsibilities:

async function getJSONFromText<T>(text: string): T {
  try ...
}

...

const json = getJSONFromText<TokenResponse>(await response.text());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RIght, but then we would have to extract the text on each of the three calls, which seems a bit repetitive; but more importantly, the logs grab the status from the response so that would need to be passed in too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we might also want to use the status code as part of the analysis of what went wrong with the response (e.g. I think that the Bot Management failure would have a 403 status).

try {
return JSON.parse(text);
} catch (e) {
// Sometime we get an error response where the body is HTML
if (text.match(/<!DOCTYPE html>/)) {
logger.error(
"The body of the response was HTML rather than JSON. Check the debug logs to see the full body of the response."
);
if (text.match(/challenge-platform/)) {
logger.error(
"It looks like you might have hit a bot challenge page. This may be transient but if not, please contact Cloudflare to find out what can be done."
);
}
}
logger.debug("Full body of response\n\n", text);
throw new Error(
`Invalid JSON in response: status: ${response.status} ${response.statusText}`,
{ cause: e }
);
}
}
Loading