Skip to content

Commit

Permalink
Merge pull request #103 from codex-team/feat-dismiss
Browse files Browse the repository at this point in the history
feat(before-send): ability to dismiss error
  • Loading branch information
neSpecc authored Sep 30, 2024
2 parents 1925506 + 11ffc43 commit b31a9ba
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ window.hawk = new HawkCatcher({
})
```
## Dismiss error
You can use the `beforeSend()` hook to prevent sending a particular event. Return `false` for that.
## Integrate to Vue application
Vue apps have their own error handler, so if you want to catcher errors thrown inside Vue components, you should set up a Vue integration.
Expand Down
4 changes: 2 additions & 2 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ <h2>Test console catcher</h2>
<br><br>
<button id="btn-console-test">Make</button>
</section>
<script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/vue@2"></script>
<section>
<h2>Test Vue integration: $root</h2>
<div id="vue-app-1">
Expand Down Expand Up @@ -227,7 +227,7 @@ <h2>Test Vue integration: &lt;test-component&gt;</h2>
<script src="sample-errors.js"></script>
<script>
window.hawk = new HawkCatcher({
token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwcm9qZWN0SWQiOiI1ZTVhNzVjNzNlM2E5ZDE2YjUzMzczOTMiLCJpYXQiOjE1ODI5ODY2OTV9.oK_LPRxcpRuZE8eSbmdObRX712YXO_Twpq7mgg7-9pw',
token: 'eyJpbnRlZ3JhdGlvbklkIjoiNWU5OTE1MzItZTdiYy00ZjA0LTliY2UtYmIzZmE5ZTUwMTg3Iiwic2VjcmV0IjoiMTBlMTA4MjQtZTcyNC00YWFkLTkwMDQtMzExYTU1OWMzZTIxIn0=',
collectorEndpoint: 'ws://localhost:3000/ws',
vue: window.Vue,
context: {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hawk.so/javascript",
"version": "3.0.2",
"version": "3.0.3",
"description": "JavaScript errors tracking for Hawk.so",
"main": "./dist/hawk.js",
"types": "./dist/index.d.ts",
Expand Down
25 changes: 21 additions & 4 deletions src/catcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
Json, EventData, EncodedIntegrationToken, DecodedIntegrationToken
} from '@hawk.so/types';
import { JavaScriptCatcherIntegrations } from './types/integrations';
import { EventRejectedError } from './errors';

/**
* Allow to use global VERSION, that will be overwritten by Webpack
Expand Down Expand Up @@ -65,8 +66,9 @@ export default class Catcher {

/**
* This Method allows developer to filter any data you don't want sending to Hawk
* If method returns false, event will not be sent
*/
private readonly beforeSend: (event: EventData<JavaScriptAddons>) => EventData<JavaScriptAddons>;
private readonly beforeSend: (event: EventData<JavaScriptAddons>) => EventData<JavaScriptAddons> | false;

/**
* Transport for dialog between Catcher and Collector
Expand Down Expand Up @@ -246,8 +248,15 @@ export default class Catcher {
}

this.sendErrorFormatted(errorFormatted);
} catch (formattingError) {
log('Internal error ლ(´ڡ`ლ)', 'error', formattingError);
} catch (e) {
if (e instanceof EventRejectedError) {
/**
* Event was rejected by user using the beforeSend method
*/
return;
}

log('Internal error ლ(´ڡ`ლ)', 'error', e);
}
}

Expand Down Expand Up @@ -286,7 +295,15 @@ export default class Catcher {
* Filter sensitive data
*/
if (typeof this.beforeSend === 'function') {
payload = this.beforeSend(payload);
const beforeSendResult = this.beforeSend(payload);

if (beforeSendResult === false) {
throw new EventRejectedError('Event rejected by beforeSend method.');

return;
} else {
payload = beforeSendResult;
}
}

return {
Expand Down
12 changes: 12 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Error triggered when event was rejected by beforeSend method
*/
export class EventRejectedError extends Error {
/**
* @param message - error message
*/
constructor(message: string) {
super(message);
this.name = 'EventRejectedError';
}
}

0 comments on commit b31a9ba

Please sign in to comment.