Skip to content

Commit

Permalink
ref(stacktrace-symbolication): Guard requests and parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Lms24 committed Nov 21, 2023
1 parent e96a5c6 commit 6890ef7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/smooth-pandas-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@spotlightjs/core': patch
---

ref(core): Guard calls to symbolication endpoint
5 changes: 5 additions & 0 deletions .changeset/warm-countries-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@spotlightjs/astro': patch
---

ref(astro): Guard requests and parsing in Vite Symbolication plugin
31 changes: 28 additions & 3 deletions packages/astro/src/vite/source-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,24 @@ export const sourceContextPlugin: () => Plugin = () => ({
});

req.on('end', async () => {
const stacktrace: SentryStackTrace = JSON.parse(requestBody);
const stacktrace = parseStackTrace(requestBody);

if (!stacktrace) {
res.writeHead(500);
res.end();
return;
}

for (const frame of stacktrace.frames ?? []) {
if (
isValidSentryStackFrame(frame) &&
// let's ignore dependencies for now with this naive check
!frame.filename.includes('/node_modules/')
) {
const generatedCodeResponse = await fetch(frame.filename);
const generatedCode = await generatedCodeResponse.text();
const generatedCode = await getGeneratedCodeFromServer(frame.filename);
if (!generatedCode) {
continue;
}

// Extract the inline source map from the minified code
const inlineSourceMapMatch = generatedCode.match(
Expand All @@ -71,6 +79,23 @@ export const sourceContextPlugin: () => Plugin = () => ({
},
});

async function getGeneratedCodeFromServer(filename: string): Promise<string | undefined> {
try {
const generatedCodeResponse = await fetch(filename);
return generatedCodeResponse.text();
} catch {
return undefined;
}
}

function parseStackTrace(requestBody: string): SentryStackTrace | undefined {
try {
return JSON.parse(requestBody) as SentryStackTrace;
} catch {
return undefined;
}
}

async function applySourceContextToFrame(sourceMapContent: string, frame: ValidSentryStackFrame) {
const consumer = await new SourceMap.SourceMapConsumer(sourceMapContent);

Expand Down
22 changes: 15 additions & 7 deletions packages/core/src/integrations/sentry/sentry-integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@ export class Spotlight implements Integration {
}

for (const exception of event.exception.values ?? []) {
console.log('fetching', exception);
const stackTraceWithContextResponse = await fetch('/spotlight/contextlines', {
method: 'PUT',
body: JSON.stringify(exception.stacktrace),
});
const stackTraceWithContext = await stackTraceWithContextResponse.json();
exception.stacktrace = stackTraceWithContext;
try {
const stackTraceWithContextResponse = await fetch('/spotlight/contextlines', {
method: 'PUT',
body: JSON.stringify(exception.stacktrace),
});

if (!stackTraceWithContextResponse.ok || stackTraceWithContextResponse.status !== 200) {
continue;
}

const stackTraceWithContext = await stackTraceWithContextResponse.json();
exception.stacktrace = stackTraceWithContext;
} catch {
// Something went wrong, for now we just ignore it.
}
}
return event;
});
Expand Down

0 comments on commit 6890ef7

Please sign in to comment.