Skip to content

Commit

Permalink
Fixed an issue where <string> line numbers are embeded in the traceba…
Browse files Browse the repository at this point in the history
…ck (#39)
  • Loading branch information
bmingles committed Jul 16, 2024
1 parent d851f61 commit e2e8686
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
19 changes: 13 additions & 6 deletions src/services/DhService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,18 +263,25 @@ export abstract class DhService<TDH, TClient>

if (line != null) {
// If selectionOnly is true, the line number in the error will be
// relative to the selection
const offsetLine = selectionOnly
? line + editor.selection.start.line - 1
: line - 1;
// relative to the selection (Python line numbers are 1 based. vscode
// line numbers are zero based.)
const fileLine =
(selectionOnly ? line + editor.selection.start.line : line) - 1;

const lineLength = editor.document.lineAt(offsetLine).text.length;
// There seems to be an error for certain Python versions where line
// numbers are shown as -1. In such cases, we'll just mark the first
// token on the first line to at least flag the file as having an error.
const startLine = Math.max(0, fileLine);

// Zero length will flag a token instead of a line
const lineLength =
fileLine < 0 ? 0 : editor.document.lineAt(fileLine).text.length;

// Diagnostic representing the line of code that produced the server error
const diagnostic: vscode.Diagnostic = {
message: value == null ? error : `${value}\n${error}`,
severity: vscode.DiagnosticSeverity.Error,
range: new vscode.Range(offsetLine, 0, offsetLine, lineLength),
range: new vscode.Range(startLine, 0, startLine, lineLength),
source: 'deephaven',
};

Expand Down
14 changes: 13 additions & 1 deletion src/util/errorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function parseServerError(error: string): ParsedError {

const [key, value] = line.split(':');

if (key && value) {
if (key.length) {
// Once we hit the Traceback, accumulate remaining lines
if (key === 'Traceback (most recent call last)') {
errorDetails.traceback = [value, ...lines].join('\n');
Expand All @@ -42,5 +42,17 @@ export function parseServerError(error: string): ParsedError {
}
}

// If top-level error isn't associated with File: "<string>", look for the
// first match in the traceback.
if (errorDetails.file !== '<string>' && errorDetails.traceback != null) {
const fileStringRegEx = /\s+File "<string>", line (\d+), in <module>/;
const [, lineNumberStr] =
fileStringRegEx.exec(errorDetails.traceback) ?? [];

if (lineNumberStr != null) {
errorDetails.line = Number(lineNumberStr);
}
}

return errorDetails;
}

0 comments on commit e2e8686

Please sign in to comment.