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

🐛 Handle non-object response and error #2860

Merged
merged 7 commits into from
Jul 24, 2024

Conversation

nulrich
Copy link
Contributor

@nulrich nulrich commented Jul 11, 2024

Motivation

We have a lot of telemetry errors concerning Fetch error that are not an Error object but a string or undefined.

Changes

  • Handling the non-object Fetch Error and Response
  • Supporting abort status from AbortController

Testing

  • Local
  • Staging
  • Unit
  • End to end

Copy link

cit-pr-commenter bot commented Jul 11, 2024

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum 161.66 KiB 161.73 KiB 71 B +0.04%
Logs 57.97 KiB 58.04 KiB 71 B +0.12%
Rum Slim 110.18 KiB 110.25 KiB 71 B +0.06%
Worker 25.21 KiB 25.21 KiB 0 B 0.00%
🚀 CPU Performance
Action Name Base Average Cpu Time (ms) Local Average Cpu Time (ms) 𝚫
addglobalcontext 0.002 0.003 0.001
addaction 0.041 0.070 0.029
adderror 0.039 0.075 0.036
addtiming 0.001 0.002 0.001
startview 1.074 4.312 3.238
startstopsessionreplayrecording 0.911 4.478 3.567
logmessage 0.020 0.092 0.071
🧠 Memory Performance
Action Name Base Consumption Memory (bytes) Local Consumption Memory (bytes) 𝚫 (bytes)
addglobalcontext 20.17 KiB 20.86 KiB 706 B
addaction 72.30 KiB 74.60 KiB 2.31 KiB
adderror 86.98 KiB 87.64 KiB 677 B
addtiming 18.06 KiB 19.48 KiB 1.43 KiB
startview 348.96 KiB 355.19 KiB 6.23 KiB
startstopsessionreplayrecording 16.98 KiB 13.80 KiB -3255 B
logmessage 69.84 KiB 70.96 KiB 1.12 KiB

🔗 RealWorld

@nulrich nulrich changed the title Handle non-object response and error 🐛 Handle non-object response and error Jul 12, 2024
@codecov-commenter
Copy link

codecov-commenter commented Jul 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.65%. Comparing base (4b853b4) to head (bdc7b51).
Report is 12 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2860      +/-   ##
==========================================
- Coverage   93.67%   93.65%   -0.02%     
==========================================
  Files         266      266              
  Lines        7586     7585       -1     
  Branches     1688     1688              
==========================================
- Hits         7106     7104       -2     
- Misses        480      481       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@nulrich nulrich marked this pull request as ready for review July 16, 2024 12:06
@nulrich nulrich requested a review from a team as a code owner July 16, 2024 12:06
Comment on lines 122 to 125
responsePromise.then(
monitor((response) => reportFetch(response)),
monitor((error) => reportFetch(error, true))
)
Copy link
Member

Choose a reason for hiding this comment

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

💭 thought: ‏Thank you for doing this! I don't think there is a good reason to have all those checks. I think we could remove them, this would allow a simpler approach:

Suggested change
responsePromise.then(
monitor((response) => reportFetch(response)),
monitor((error) => reportFetch(error, true))
)
function reportFetch(partialContext: Partial<FetchResolveContext>) {
const context = startContext as unknown as FetchResolveContext
context.state = 'resolve'
assign(context, partial)
observable.notify(context)
}
responsePromise.then(
monitor((response) => {
reportFetch({
response,
responsType: response.type,
status: response.status,
isAborted: false
})
}),
monitor((error: unknown) => {
reportFetch({
status: 0,
isAborted: response instanceof DOMException && response.code === DOMException.ABORT_ERR,
error
})
})
)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, and added support for AbortController.

})
}),
monitor((error: Error) => {
const isAborted =
Copy link
Collaborator

Choose a reason for hiding this comment

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

🥜 nitpick: ‏You could inline it

    reportFetch({
        status: 0,
        isAborted:
          context.init?.signal?.aborted || (error instanceof DOMException && error.code === DOMException.ABORT_ERR),
        error,
      })

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Collaborator

Choose a reason for hiding this comment

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

It could be nice to have a test with AbortSignal :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@amortemousque
Copy link
Collaborator

👏 praise: ‏Very nice simplification!!

@@ -14,7 +14,7 @@ export function mockFetch() {
}
}

window.fetch = (() => {
window.fetch = ((...[, init]) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

❓ question: ‏Just out of curiosity, do you know how this is transpiled in ES5? :)

Copy link
Member

Choose a reason for hiding this comment

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

https://www.typescriptlang.org/play/?target=0#code/O4SwdgJg9sB0BmBTALgYwBYAIC8mAUesRA2gDSbgjIC6AlDgHyYDeAvrUA

Though, this syntax is a bit surprising :D

Suggestion:

window.fetch = (_input, init) => {
  ..
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

It makes sense when you think about it, but I prefer benoit's suggestion (so I don't have to think about it 😄)

Or event (input, init) => { with args-after-used

@@ -14,7 +14,7 @@ export function mockFetch() {
}
}

window.fetch = (() => {
window.fetch = ((...[, init]) => {
Copy link
Member

Choose a reason for hiding this comment

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

https://www.typescriptlang.org/play/?target=0#code/O4SwdgJg9sB0BmBTALgYwBYAIC8mAUesRA2gDSbgjIC6AlDgHyYDeAvrUA

Though, this syntax is a bit surprising :D

Suggestion:

window.fetch = (_input, init) => {
  ..
}

@nulrich nulrich merged commit e524b92 into main Jul 24, 2024
20 checks passed
@nulrich nulrich deleted the nicolas.ulrich/fetch-observable-response branch July 24, 2024 11:32
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.

7 participants