Skip to content

Commit 19a028d

Browse files
authored
Merge pull request #141 from codex-team/fix-fetch
fix(stack-parser): handle file fetch error
2 parents 001c8ab + e3f245c commit 19a028d

File tree

5 files changed

+36
-11
lines changed

5 files changed

+36
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@hawk.so/javascript",
33
"type": "commonjs",
4-
"version": "3.2.11",
4+
"version": "3.2.12",
55
"description": "JavaScript errors tracking for Hawk.so",
66
"files": [
77
"dist"

src/addons/consoleCatcher.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ export class ConsoleCatcher {
173173
// First frame is used as fileLine - this is what DevTools shows as clickable link
174174
const fileLine = stackLines[userFrameIndex]?.trim() || '';
175175

176-
return { userStack, fileLine };
176+
return { userStack,
177+
fileLine };
177178
}
178179

179180
/**
@@ -248,6 +249,8 @@ export class ConsoleCatcher {
248249
* 3. Create a ConsoleLogEvent with metadata
249250
* 4. Store it in the buffer
250251
* 5. Forward the call to the native console (so output still appears in DevTools)
252+
*
253+
* @param {...any} args
251254
*/
252255
window.console[method] = (...args: unknown[]): void => {
253256
// Capture full stack trace and extract user code stack

src/modules/fetchTimer.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,18 @@ export default function fetchTimer(url: string, ms: number): Promise<any> {
2525
log('Request is too long, aborting...', 'log', url);
2626
}, ms);
2727

28-
return fetchPromise.then((response) => {
29-
clearTimeout(timeoutId);
28+
return fetchPromise
29+
.then((response) => {
30+
clearTimeout(timeoutId);
3031

31-
return response;
32-
});
32+
return response;
33+
})
34+
.catch((error) => {
35+
clearTimeout(timeoutId);
36+
37+
/**
38+
* Re-throw the error so it can be handled by the caller
39+
*/
40+
throw error;
41+
});
3342
}

src/modules/stackParser.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { StackFrame } from 'error-stack-parser';
22
import ErrorStackParser from 'error-stack-parser';
33
import type { BacktraceFrame, SourceCodeLine } from '@hawk.so/types';
4-
import log from '../utils/log';
54
import fetchTimer from './fetchTimer';
65

76
/**
@@ -26,7 +25,7 @@ export default class StackParser {
2625
const sourceCode = await this.extractSourceCode(frame);
2726
const file = frame.fileName !== null && frame.fileName !== undefined ? frame.fileName : '';
2827
const line = frame.lineNumber !== null && frame.lineNumber !== undefined ? frame.lineNumber : 0;
29-
28+
3029
return {
3130
file,
3231
line,
@@ -132,7 +131,18 @@ export default class StackParser {
132131
* Wait for maximum 2 sec to skip loading big files.
133132
*/
134133
this.sourceFilesCache[fileName] = fetchTimer(fileName, 2000)
135-
.then((response) => response.text());
134+
.then((response) => response.text())
135+
.catch((error) => {
136+
/**
137+
* Remove failed promise from cache to allow retry
138+
*/
139+
delete this.sourceFilesCache[fileName];
140+
141+
/**
142+
* Re-throw error so it can be caught by try-catch
143+
*/
144+
throw error;
145+
});
136146

137147
/**
138148
* Dealloc cache when it collects more that 10 files
@@ -146,7 +156,10 @@ export default class StackParser {
146156

147157
return await this.sourceFilesCache[fileName];
148158
} catch (error) {
149-
log('Can not load source file. Skipping...');
159+
/**
160+
* Ensure failed promise is removed from cache
161+
*/
162+
delete this.sourceFilesCache[fileName]; // log('Can not load source file. Skipping...');
150163

151164
return null;
152165
}

src/types/hawk-initial-settings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export interface HawkInitialSettings {
6464

6565
/**
6666
* This Method allows you to filter any data you don't want sending to Hawk.
67-
*
67+
*
6868
* Return `false` to prevent the event from being sent to Hawk.
6969
*/
7070
beforeSend?(event: HawkJavaScriptEvent): HawkJavaScriptEvent | false;

0 commit comments

Comments
 (0)