From 160e480b1db1dd83778590ac2dfaef2977665101 Mon Sep 17 00:00:00 2001 From: Julien Vincent Date: Sun, 22 Jan 2023 22:34:11 +0000 Subject: [PATCH] Print full evaluation error to output window --- src/evaluate.ts | 75 ++++++++++++++++--------------- src/nrepl/index.ts | 18 +++++++- src/results-output/results-doc.ts | 2 +- 3 files changed, 57 insertions(+), 38 deletions(-) diff --git a/src/evaluate.ts b/src/evaluate.ts index f1e9ccc8e..13fe8a272 100644 --- a/src/evaluate.ts +++ b/src/evaluate.ts @@ -11,7 +11,7 @@ import { DEBUG_ANALYTICS } from './debugger/calva-debug'; import * as namespace from './namespace'; import * as replHistory from './results-output/repl-history'; import { formatAsLineComments } from './results-output/util'; -import { getStateValue } from '../out/cljs-lib/cljs-lib'; +import { getStateValue, prettyPrint } from '../out/cljs-lib/cljs-lib'; import { getConfig } from './config'; import * as replSession from './nrepl/repl-session'; import * as getText from './util/get-text'; @@ -156,7 +156,7 @@ async function evaluateCodeUpdatingUI( if (err.length > 0) { const errMsg = formatAsLineComments(err.join('\n')); if (context.stacktrace) { - outputWindow.saveStacktrace(context.stacktrace); + outputWindow.saveStacktrace(context.stacktrace as any); outputWindow.appendLine(errMsg, (_, afterResultLocation) => { outputWindow.markLastStacktraceRange(afterResultLocation); }); @@ -166,43 +166,46 @@ async function evaluateCodeUpdatingUI( } } } catch (e) { + const cause = `"${context.stacktrace.message.replace(/\"/g, '\\"').trim()}"`; + const data = context.stacktrace.data ? `\n :data ${context.stacktrace.data}` : ''; + const stacktrace = context.stacktrace.stacktrace + .map((item) => `[${outputWindow.stackEntryString(item)}]`) + .join('\n'); + + const formattedError = `#${context.stacktrace.class.trim()} { + :type ${context.stacktrace.class} + :cause ${cause}${data} + :trace [${stacktrace}] +}`; + if (showErrorMessage) { - const outputWindowError = err.length - ? formatAsLineComments(err.join('\n')) - : formatAsLineComments(e); - outputWindow.appendLine(outputWindowError, async (resultLocation, afterResultLocation) => { - if (selection) { - const editorError = util.stripAnsi(err.length ? err.join('\n') : e); - const currentCursorPos = editor.selection.active; - if (options.comment) { - await addAsComment( - selection.start.character, - editorError, - selection, - editor, - editor.selection - ); - } - if (!outputWindow.isResultsDoc(editor.document)) { - annotations.decorateSelection( - editorError, - selection, - editor, - currentCursorPos, - resultLocation, - annotations.AnnotationStatus.ERROR - ); - if (!options.comment) { - annotations.decorateResults(editorError, true, selection, editor); - } - } + outputWindow.appendLine(prettyPrint(formattedError).value); + + if (selection) { + const editorError = util.stripAnsi(err.length ? err.join('\n') : e); + const currentCursorPos = editor.selection.active; + if (options.comment) { + await addAsComment( + selection.start.character, + editorError, + selection, + editor, + editor.selection + ); } - if (context.stacktrace && context.stacktrace.stacktrace) { - outputWindow.markLastStacktraceRange(afterResultLocation); + if (!outputWindow.isResultsDoc(editor.document)) { + annotations.decorateSelection( + editorError, + selection, + editor, + currentCursorPos, + null, + annotations.AnnotationStatus.ERROR + ); + if (!options.comment) { + annotations.decorateResults(editorError, true, selection, editor); + } } - }); - if (context.stacktrace && context.stacktrace.stacktrace) { - outputWindow.saveStacktrace(context.stacktrace.stacktrace); } } } diff --git a/src/nrepl/index.ts b/src/nrepl/index.ts index 1361249aa..45d112003 100644 --- a/src/nrepl/index.ts +++ b/src/nrepl/index.ts @@ -901,6 +901,20 @@ export class NReplSession { } } +type StackTraceItem = { + class: string; + file: string; + line: number; + method: string; + name: string; + type: string; +}; +type NReplEvaluationErrorStackTrace = { + class: string; + message: string; + stacktrace: StackTraceItem[]; +}; + /** * A running nREPL eval call. */ @@ -919,7 +933,7 @@ export class NReplEvaluation { private _exception: string; - private _stacktrace: any; + private _stacktrace: NReplEvaluationErrorStackTrace | undefined | any; private _msgs: any[] = []; @@ -1093,6 +1107,7 @@ export class NReplEvaluation { const cause = msg.causes[0]; const errorMessage = `${cause.class}: ${cause.message}`; this._stacktrace = { stacktrace: cause.stacktrace }; + console.log('HAS STATUS', msg); this.err(errorMessage); } if (msg.value !== undefined || msg['debug-value'] !== undefined) { @@ -1135,6 +1150,7 @@ export class NReplEvaluation { this.session .stacktrace() .then((stacktrace) => { + console.log('GOT THING', stacktrace); this._stacktrace = stacktrace; this.doReject(this.exception); }) diff --git a/src/results-output/results-doc.ts b/src/results-output/results-doc.ts index 996b917be..260f31d63 100644 --- a/src/results-output/results-doc.ts +++ b/src/results-output/results-doc.ts @@ -389,7 +389,7 @@ export function getStacktraceEntryForKey(key: string): OutputStacktraceEntry { return _stacktraceEntries[key]; } -function stackEntryString(entry: any): string { +export function stackEntryString(entry: any): string { const name = entry.var || entry.name; return `${name} (${entry.file}:${entry.line})`; }