Skip to content

Commit

Permalink
refactor: unify integration
Browse files Browse the repository at this point in the history
  • Loading branch information
seibert-io committed May 28, 2020
1 parent ce9457a commit f36ba15
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 22 deletions.
77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# @madebyheyday/sentry-util

Provides basic sentry integration for Node and Browser based projects and includes Typescript type declarations.

## Usage

Set the following environment variables in Node or the browser (via webpack):

| Variable | Purpose |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| SENTRY_ENABLED | Used by default beforeSend() function if no custom beforeSend function is provided.<br>Set to true\|1 / false\|0 to enable/disable reporting errors to Sentry |
| SENTRY_DSN | DSN as found in your Sentry settings |
| SENTRY_ENVIRONMENT | Environment identifier to use when reporting errors.<br>Can be overriden in browser context by providing parameter `environment` to `initSentry()` in case your environment cannot or should not be hardcoded in to the application source |
| SENTRY_RELEASE | set to the release name to report to sentry when capturing errors. When not set, the Node implementation will use `${process.env.npm_package_name}@${process.env.npm_package_version` as a default. |

### Browsers

In your main.js

```
import Sentry, { initSentry } from '@madebyheyday/sentry-util/dist/browser';
initSentry('production');
// optionally provide a custom beforeSend function to filter captured errors
initSentry('production', (error: Error): Error | null => error);
// optionally use Sentry instance to allow setting extras, breadcrumbs, ...
Sentry.setExtra('userAuthenticated', false);
```

Anywhere you wish to capture errors

```
import Sentry, { captureAndLogError } from '@madebyheyday/sentry-util/dist/browser';
// optionally use Sentry instance to allow setting extras, breadcrumbs, ...
Sentry.addBreadcrumb({
level: Sentry.Severity.Debug,
message: `User logged in`
});
// use captureAndLogError() as an error handler for promises or try/catch blocks
anyPromise.catch(captureAndLogError);
try {
throw new Error('test')
} catch (error) {
captureAndLogError(error);
}
```

### Node

Set `process.env.SENTRY_ENVIRONMENT` to the desired environment specifier to use when reporting errors.

```
import Sentry, { initSentry, captureAndLogError } from '@madebyheyday/sentry-util/dist/node';
// initialize sentry
initSentry();
// optionally provide a custom beforeSend function to filter captured errors
initSentry((error: Error): Error | null => error);
// package exports Sentry instance to allow setting extras, breadcrumbs, ...
Sentry.setExtra('userAuthenticated', false);
// then use captureAndLogError() as an error handler for promises or try/catch blocks
anyPromise.catch(captureAndLogError);
try {
throw new Error('test')
} catch (error) {
captureAndLogError(error);
}
```
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
*/
import * as Sentry from '@sentry/browser';

// emit events only if sentry is enabled for the current environment:
const beforeSendDefault = (error: Error): Error | null => (process.env.SENTRY_ENABLED ? error : null);
export function initSentry(environment?: string, release?: string, beforeSend?: (error: Error) => Error | null): void {
// emit events only if sentry is enabled for the current environment:
const beforeSendDefault = (error: Error): Error | null =>
!process.env.SENTRY_ENABLED || ['0', 'false', ''].includes(process.env.SENTRY_ENABLED.toLowerCase()) ? null : error;

export function initSentry(environment: string, release?: string, beforeSend?: (error: Error) => Error | null): void {
Sentry.init({
dsn: process.env.SENTRY_DSN || '',
environment, // used to be process.env.SENTRY_ENVIRONMENT || ''
release,
environment: environment || process.env.SENTRY_ENVIRONMENT || '',
release: release || process.env.SENTRY_RELEASE,
beforeSend: beforeSend ? beforeSend : beforeSendDefault
});
}
Expand Down
31 changes: 15 additions & 16 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@
*/
import * as Sentry from '@sentry/node';

const emitErrors = !['false', '0', ''].includes(String(process.env.SENTRY_ENABLED).toLowerCase());
const useSentryReleases = ['1', 'true'].includes(String(process.env.SENTRY_USE_RELEASES).toLowerCase());

// emit events only if sentry is enabled for the current environment:
const beforeSend = (error: Error): Error | null => (emitErrors ? error : null);

const config: Sentry.NodeOptions = {
dsn: process.env.SENTRY_DSN || '',
environment: process.env.SENTRY_ENVIRONMENT || '',
beforeSend
};

if (useSentryReleases) config.release = `${process.env.npm_package_name}@${process.env.npm_package_version}`;

Sentry.init(config);

export function captureAndLogError(error: Error): void {
console.error(error);
Sentry.captureException(error);
}

export function initSentry(beforeSend?: (error: Error) => Error | null): void {
// emit events only if sentry is enabled for the current environment:
const beforeSendDefault = (error: Error): Error | null =>
['false', '0', ''].includes(String(process.env.SENTRY_ENABLED).toLowerCase()) ? null : error;

const config: Sentry.NodeOptions = {
dsn: process.env.SENTRY_DSN || '',
environment: process.env.SENTRY_ENVIRONMENT || '',
release: process.env.SENTRY_RELEASE || `${process.env.npm_package_name}@${process.env.npm_package_version}`,
beforeSend: beforeSend || beforeSendDefault
};

Sentry.init(config);
}

export default Sentry;

0 comments on commit f36ba15

Please sign in to comment.