Skip to content

feat(solidjs): Add withSentryErrorBoundary HOC #12421

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

Merged
merged 4 commits into from
Jun 10, 2024

Conversation

andreiborza
Copy link
Member

To automatically capture exceptions from inside a component tree and render a fallback component, wrap the native Solid
JS ErrorBoundary component with Sentry.withSentryErrorBoundary.

import * as Sentry from '@sentry/solidjs';
import { ErrorBoundary } from 'solid-js';

Sentry.init({
  dsn: '__PUBLIC_DSN__',
  tracesSampleRate: 1.0, //  Capture 100% of the transactions
});

const SentryErrorBoundary = Sentry.withSentryErrorBoundary(ErrorBoundary);

render(
  () => (
    <SentryErrorBoundary fallback={err => <div>Error: {err.message}</div>}>
      <ProblematicComponent />
    </SentryErrorBoundary>
  ),
  document.getElementById('root'),
);

Note: When using an ErrorBoundary in conjunction with Solid Router, the fallback component renders twice, see here.

@andreiborza andreiborza requested a review from AbhiPrasad June 7, 2024 22:03
@andreiborza andreiborza added the Package: solid Issues related to the Sentry Solid SDK label Jun 7, 2024
Copy link
Contributor

github-actions bot commented Jun 7, 2024

size-limit report 📦

Path Size
@sentry/browser 22 KB (added)
@sentry/browser (incl. Tracing) 33.19 KB (added)
@sentry/browser (incl. Tracing, Replay) 68.92 KB (added)
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 62.23 KB (added)
@sentry/browser (incl. Tracing, Replay with Canvas) 72.98 KB (added)
@sentry/browser (incl. Tracing, Replay, Feedback) 85.1 KB (added)
@sentry/browser (incl. Tracing, Replay, Feedback, metrics) 86.94 KB (added)
@sentry/browser (incl. metrics) 26.19 KB (added)
@sentry/browser (incl. Feedback) 38.17 KB (added)
@sentry/browser (incl. sendFeedback) 26.59 KB (added)
@sentry/browser (incl. FeedbackAsync) 31.15 KB (added)
@sentry/react 24.77 KB (added)
@sentry/react (incl. Tracing) 36.24 KB (added)
@sentry/vue 26.01 KB (added)
@sentry/vue (incl. Tracing) 35.04 KB (added)
@sentry/svelte 22.14 KB (added)
CDN Bundle 23.36 KB (added)
CDN Bundle (incl. Tracing) 34.87 KB (added)
CDN Bundle (incl. Tracing, Replay) 68.98 KB (added)
CDN Bundle (incl. Tracing, Replay, Feedback) 74.11 KB (added)
CDN Bundle - uncompressed 68.63 KB (added)
CDN Bundle (incl. Tracing) - uncompressed 103.22 KB (added)
CDN Bundle (incl. Tracing, Replay) - uncompressed 213.64 KB (added)
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 226.11 KB (added)
@sentry/nextjs (client) 35.59 KB (added)
@sentry/sveltekit (client) 33.83 KB (added)
@sentry/node 129.8 KB (added)
@sentry/node - without tracing 92.55 KB (added)
@sentry/aws-serverless 117.62 KB (added)

@andreiborza andreiborza requested review from mydea, lforst, Lms24 and s1gr1d June 10, 2024 07:15
import { captureException } from '@sentry/browser';
import type { Component, JSX } from 'solid-js';
import { mergeProps, splitProps, untrack } from 'solid-js';
import { createComponent } from 'solid-js/web';
Copy link
Member

Choose a reason for hiding this comment

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

Is solid-js/web always available to us? I don't see it being listed as peerDependency or dependency in the SDK's package.json

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 believe it's available through solid-js. At least I didn't have any issues developing locally. The solidjs templates (these are used to start projects ala create-react-app) also don't specifically list it as dep, e.g. here.

Copy link
Member

Choose a reason for hiding this comment

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

Ahh I see, thx for explaining! Let's leave it as is and if we get error reports (I have a feeling pnpm could complain 👀 ) we can add it later.

/**
* A higher-order component to wrap Solid's ErrorBoundary to capture exceptions.
*/
export function withSentryErrorBoundary(ErrorBoundary: Component<ErrorBoundaryProps>): Component<ErrorBoundaryProps> {
Copy link
Member

Choose a reason for hiding this comment

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

I have a naive question (sorry if I missed convos around this): Why do users need to pass in the ErrorBoundary component from solid here? Can we not import it like the other solid imports?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not naive at all. That's the approach I initially took, but it doesn't quite do the job correctly.

From local testing and similar experience with the router instrumentation, certain functions and functionality are app context specific. For the router, this meant that we couldn't use use hooks because they didn't have the user's router context. For the ErrorBoundary the reset function is contextual, so exporting a generic error boundary from this package would mean the reset function does nothing.

Hope this helps to understand it a bit better.

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense, thanks for explaining!

@andreiborza andreiborza requested a review from Lms24 June 10, 2024 08:56
@andreiborza andreiborza force-pushed the ab/solidjs-error-boundary branch from df01b63 to 322c45f Compare June 10, 2024 09:48
Copy link
Member

Choose a reason for hiding this comment

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

Any chance the yarn.lock file was created accidentally? I think we mainly use pnpm in the e2e tests but if you're using yarn it should work too.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, yea I used yarn locally. I had problems with pnpm and volta/yarn before, it also always adds itself as packageManager to the top level package.json which is a pain to remember/revert.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, I removed the yarn lock and error log :)

Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

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

Nice, thanks for addressing my feedback! Just spotted a suspicios yarn.lock file but other than that ready to go from my PoV!

@andreiborza andreiborza merged commit 54c26cd into develop Jun 10, 2024
110 checks passed
@andreiborza andreiborza deleted the ab/solidjs-error-boundary branch June 10, 2024 12:58
billyvg pushed a commit that referenced this pull request Jun 10, 2024
To automatically capture exceptions from inside a component tree and
render a fallback component, wrap the native Solid
JS `ErrorBoundary` component with `Sentry.withSentryErrorBoundary`.

```js
import * as Sentry from '@sentry/solidjs';
import { ErrorBoundary } from 'solid-js';

Sentry.init({
  dsn: '__PUBLIC_DSN__',
  tracesSampleRate: 1.0, //  Capture 100% of the transactions
});

const SentryErrorBoundary = Sentry.withSentryErrorBoundary(ErrorBoundary);

render(
  () => (
    <SentryErrorBoundary fallback={err => <div>Error: {err.message}</div>}>
      <ProblematicComponent />
    </SentryErrorBoundary>
  ),
  document.getElementById('root'),
);
```

**Note**: When using an `ErrorBoundary` in conjunction with Solid
Router, the fallback component renders twice, see
[here](solidjs/solid-router#440).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: solid Issues related to the Sentry Solid SDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants