Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Initialization settings:
| `disableGlobalErrorsHandling` | boolean | optional | Do not initialize global errors handling |
| `disableVueErrorHandler` | boolean | optional | Do not initialize Vue errors handling |
| `consoleTracking` | boolean | optional | Initialize console logs tracking |
| `trackOnlyMarkedFiles` | boolean | optional | If `true`, send only errors whose stack frames reference files marked for tracking (see [Tracking only marked files](#tracking-only-marked-files)) |
| `beforeSend` | function(event) => event | optional | This Method allows you to filter any data you don't want sending to Hawk |

Other available [initial settings](types/hawk-initial-settings.d.ts) are described at the type definition.
Expand Down Expand Up @@ -175,6 +176,33 @@ window.hawk = new HawkCatcher({
})
```

## Tracking only marked files

Sometimes you want Hawk to ignore errors coming from "foreign" code (third‑party bundles, other apps on the same domain, legacy scripts, etc.).
To do that, enable the `trackOnlyMarkedFiles` option:

```js
const hawk = new HawkCatcher({
token: 'INTEGRATION_TOKEN',
trackOnlyMarkedFiles: true,
});
```

With this flag turned on, Hawk will:

- **Inspect stack trace frames** for each error inside `formatAndSend()`.
- **Call an internal `checkTrackingMarker()`** for each file in the stack trace.
- **Drop the error** if none of the frames refer to a file that contains the tracking marker comment.

Typical usage is to add a special marker comment into the bundles you actually want to track, for example:

```js
// /*! HAWK:tracked */
// rest of your bundled app
```

Only files that contain this marker (or whatever marker your build inserts) will be treated as "ours" when `trackOnlyMarkedFiles` is enabled, everything else will be filtered out.

## Dismiss error

You can use the `beforeSend()` hook to prevent sending a particular event. Return `false` for that.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@hawk.so/javascript",
"type": "commonjs",
"version": "3.2.11",
"version": "3.2.12",
"description": "JavaScript errors tracking for Hawk.so",
"files": [
"dist"
Expand Down
84 changes: 84 additions & 0 deletions src/catcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Socket from './modules/socket';
import Sanitizer from './modules/sanitizer';
import log from './utils/log';
import StackParser from './modules/stackParser';
import fetchTimer from './modules/fetchTimer';
import type { CatcherMessage, HawkInitialSettings } from './types';
import { VueIntegration } from './integrations/vue';
import { id } from './utils/id';
Expand Down Expand Up @@ -103,6 +104,11 @@ export default class Catcher {
*/
private readonly consoleCatcher: ConsoleCatcher | null = null;

/**
* Track only errors from files marked with HAWK:tracked marker
*/
private readonly trackOnlyMarkedFiles: boolean;

/**
* Catcher constructor
*
Expand All @@ -129,6 +135,7 @@ export default class Catcher {
settings.consoleTracking !== null && settings.consoleTracking !== undefined
? settings.consoleTracking
: true;
this.trackOnlyMarkedFiles = settings.trackOnlyMarkedFiles || false;

if (!this.token) {
log(
Expand Down Expand Up @@ -343,6 +350,20 @@ export default class Catcher {
markErrorAsProcessed(error);
}

/**
* Check if we should filter by tracking marker
*/
if (this.trackOnlyMarkedFiles) {
const hasMarker = await this.checkTrackingMarker(error);

if (!hasMarker) {
/**
* Error is not from a marked file, skip it
*/
return;
}
}

const errorFormatted = await this.prepareErrorFormatted(error, context);

/**
Expand Down Expand Up @@ -478,6 +499,69 @@ export default class Catcher {
}
}

/**
* Check if error comes from a file marked with HAWK:tracked marker
*
* @param error - error to check
* @returns {boolean} true if at least one file in stack trace contains the marker, false otherwise
*/
private async checkTrackingMarker(error: Error | string): Promise<boolean> {
const notAnError = !(error instanceof Error);

/**
* If error is not an Error instance, we can't check stack trace
*/
if (notAnError) {
return false;
}

try {
const backtrace = await this.stackParser.parse(error as Error);

if (!backtrace || backtrace.length === 0) {
return false;
}

/**
* Check each file in the stack trace
*/
const marker = '/*! HAWK:tracked */';
const markerCheckPromises = backtrace.map(async (frame) => {
if (!frame.file) {
return false;
}

try {
/**
* Use fetchTimer to load file with timeout (same as StackParser does)
*/
const response = await fetchTimer(frame.file, 2000);
const fileContent = await response.text();

return fileContent.includes(marker);
} catch {
/**
* If we can't load the file, skip it
*/
return false;
}
});

const results = await Promise.all(markerCheckPromises);
/**
* Return true if at least one file contains the marker
*/

return results.some(hasMarker => hasMarker === true);
} catch {
/**
* If we can't parse the stack trace, allow the error to be sent
* (fail open - better to send too many than miss important errors)
*/
return true;
}
}

/**
* Collects additional information
*
Expand Down
9 changes: 8 additions & 1 deletion src/types/hawk-initial-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export interface HawkInitialSettings {

/**
* This Method allows you to filter any data you don't want sending to Hawk.
*
*
* Return `false` to prevent the event from being sent to Hawk.
*/
beforeSend?(event: HawkJavaScriptEvent): HawkJavaScriptEvent | false;
Expand All @@ -80,4 +80,11 @@ export interface HawkInitialSettings {
* Console log handler
*/
consoleTracking?: boolean;

/**
* Track only errors from files marked with HAWK:tracked marker.
* If enabled, only errors from files containing this marker will be sent.
* Default is false (all errors are tracked).
*/
trackOnlyMarkedFiles?: boolean;
}
Loading