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

Refactor fetch.ts to improve error handling and formatting #628

Merged
merged 1 commit into from
Aug 19, 2024

Conversation

pelikhan
Copy link
Member

@pelikhan pelikhan commented Aug 19, 2024

This pull request refactors the fetch.ts file to improve error handling and formatting. The changes include handling specific error codes, checking for cancellation, and improving the formatting of the request body in the traceFetchPost function. These improvements enhance the overall reliability and readability of the code.

The GIT_DIFF shows two changes made in the fetch.ts file present in the packages/core/src/ directory:

  • 🧹Empty space was removed on line 38 and 74, making the code more concise and clean, following best coding practices.

  • 📝On line 74, the way the body of the fetch request is stringified for debugging purposes was modified. The changes effectively pretty-print the JSON body (adds indentation) and ensure newlines are consistently replaced, regardless of whether they're part of a Windows-style or Unix-style line-ending.

Please note, these alterations do not have an impact on the public API and would likely be considered internal or housekeeping changes.

generated by pr-describe

@@ -74,7 +74,7 @@ export function traceFetchPost(
${Object.entries(headers)
.map(([k, v]) => `-H "${k}: ${v}" \\`)
.join("\n")}
-d '${JSON.stringify(body).replace(/'/g, "'\\''").replace(/\r\n/g, "\n \\")}'
-d '${JSON.stringify(body, null, 2).replace(/'/g, "'\\''").replace(/\r?\n/g, "\n \\")}'

Choose a reason for hiding this comment

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

The use of JSON.stringify(body, null, 2) will format the JSON with 2 spaces of indentation. This might not be the desired behavior if the intention was to send the JSON data as compact as possible to reduce network usage. Consider reverting this change if compactness is a priority. 📡

generated by pr-review-commit JSON_stringify

@@ -74,7 +74,7 @@ export function traceFetchPost(
${Object.entries(headers)
.map(([k, v]) => `-H "${k}: ${v}" \\`)
.join("\n")}
-d '${JSON.stringify(body).replace(/'/g, "'\\''").replace(/\r\n/g, "\n \\")}'
-d '${JSON.stringify(body, null, 2).replace(/'/g, "'\\''").replace(/\r?\n/g, "\n \\")}'

Choose a reason for hiding this comment

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

The replacement of \r\n with \n \\ has been changed to replace \r?\n with \n \\. This change will also replace single \n characters with \n \\, which might not be the desired behavior if the intention was to only replace Windows-style line endings (\r\n). Please confirm if this change is intentional. 🖥️

generated by pr-review-commit replace

@@ -38,7 +38,7 @@ export async function createFetch(
if (code === "ECONNRESET" || code === "ENOTFOUND")
// fatal
return undefined

Choose a reason for hiding this comment

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

The removal of the whitespace on line 41 does not seem to have any functional impact, but it might affect code readability if the whitespace was intended to separate logical blocks of code. Please confirm if this change is intentional. 📖

generated by pr-review-commit whitespace

Copy link

The changes in the pull request are relatively minor and focused on code readability and format.

  1. The string for the body of the fetch request is now being formatted with proper indentation, which can improve the readability of the logs. The new code uses JSON.stringify(body, null, 2) to format the JSON body with 2 spaces indentation. This does not affect the functionality but improves the debuggability.

  2. Unnecessary whitespace has been removed.

Overall, there don't seem to be any functional concerns with these changes.

So, LGTM 🚀

generated by pr-review

@pelikhan pelikhan merged commit d70e43f into main Aug 19, 2024
10 checks passed
@pelikhan pelikhan deleted the curlnewline branch August 19, 2024 18:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant