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

feat(store): implement NGXS unhandled error handler #2137

Merged
merged 20 commits into from
Mar 23, 2024
Merged

Conversation

arturovt
Copy link
Member

No description provided.

Copy link

nx-cloud bot commented Mar 21, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 19c4b4f. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 4 targets

Sent with 💌 from NxCloud.

Copy link

bundlemon bot commented Mar 21, 2024

BundleMon (Integration Projects)

Files updated (2)
Status Path Size Limits
Main bundles(Gzip)
hello-world-ng16/dist-integration/main.(hash)
.js
67.03KB (+89B +0.13%) +1%
Main bundles(Gzip)
hello-world-ng17/dist-integration/main.(hash)
.js
68.49KB (+63B +0.09%) +1%

Total files change +152B +0.11%

Final result: ✅

View report in BundleMon website ➡️


Current branch size history | Target branch size history

Copy link
Member

@markwhitfeld markwhitfeld left a comment

Choose a reason for hiding this comment

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

Awesome work here!
A few comments.
And just wondering if the original issue is represented in a test?

import { leaveNgxs } from '../operators/leave-ngxs';
import { executeUnhandledCallback } from './unhandled-rxjs-error-callback';

function fallbackErrorHandler<T>(ngZone: NgZone) {
Copy link
Member

Choose a reason for hiding this comment

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

A very important aspect of this operator is the fact that it subscribes. We could call it fallbackSubscriber. WDYT? It should also move to another file.

return new Observable<T>(subscriber => {
realSubscriber = true;
// Now that there is a real subscriber, we can unsubscribe our pro-active subscription
subscription.unsubscribe();
Copy link
Member

@markwhitfeld markwhitfeld Mar 22, 2024

Choose a reason for hiding this comment

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

This might be called multiple times if there are multiple real subscribers. I think it is just a no-op, but haven't tested it.
We could set it to null after unsubscribing (and change this line to use ?.).
Having a falsy subscription could also be the indicator of whether or not we have a real subscriber.

// Since the error can be essentially anything, we must ensure that we only
// handle objects, as weak maps do not allow any other key type besides objects.
// The error can also be a string if thrown in the following manner: `throwError('My Error')`.
if (error !== null && typeof error === 'object') {
Copy link
Member

Choose a reason for hiding this comment

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

Do we need a strategy for handling non-object errors?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think so. Here's why:

  • the ecma 2022 standard specifies the Error class which is intended to represent errors
  • while other types are technically allowed in throw statements, such as throw some_string, it's widely regarded as bad practice. This is because it doesn't include stack trace information, making debugging more difficult and less informative


handleError(_error: any, _context: NgxsErrorContext): void {
// Retrieve lazily to avoid cyclic dependency exception.
this._errorHandler = this._errorHandler || this._injector.get(ErrorHandler);
Copy link
Member

Choose a reason for hiding this comment

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

Given the other changes to load this lazily in the StateFactory, we should move this back to the constructor.
This would also be closer to what a user may implement.

Comment on lines -830 to +831
'Action 1 - dispatch complete',
'action1 obs - unsubscribe',
'Action 1 - dispatch complete',
Copy link
Member

Choose a reason for hiding this comment

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

Is this change because we unsubscribe before subscribing the real subscriber.
Would it go back to what it was if we unsubscribed after adding the real subscriber (in the fallback operator) ?

Copy link
Member Author

Choose a reason for hiding this comment

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

This change is related to RxJS upgrade, seems like the sequence of some stuff has changed internally

Copy link

codeclimate bot commented Mar 23, 2024

Code Climate has analyzed commit 19c4b4f and detected 1 issue on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 1

The test coverage on the diff in this pull request is 91.7% (50% is the threshold).

This pull request will bring the total coverage in the repository to 95.2% (0.0% change).

View more on Code Climate.

@arturovt arturovt marked this pull request as ready for review March 23, 2024 23:30
@arturovt
Copy link
Member Author

And just wondering if the original issue is represented in a test?

The original issue was that the error handler was not being called on each error. We've decided not to support this anymore because it's strange to log errors even if they're handled. For instance, if someone is using Rollbar and providing their custom ErrorHandler which logs errors to Rollbar, even handled errors would be logged to Rollbar, which is completely undesirable.

@arturovt arturovt merged commit 3d36414 into master Mar 23, 2024
13 checks passed
@arturovt arturovt deleted the feat/unhandled branch March 23, 2024 23:46
@markwhitfeld markwhitfeld added this to the v.18.0.0 milestone Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants