Skip to content

Commit

Permalink
Show details and hints in error message
Browse files Browse the repository at this point in the history
  • Loading branch information
scotttrinh committed Sep 13, 2023
1 parent b3bb5a6 commit 1377d24
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
41 changes: 23 additions & 18 deletions packages/driver/src/errors/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,35 +75,40 @@ export function prettyPrintError(
attrs: Map<number, Uint8Array>,
query: string
) {
let errMessage = "\n";

const lineStart = tryParseInt(attrs.get(ErrorAttr.lineStart));
const lineEnd = tryParseInt(attrs.get(ErrorAttr.lineEnd));
const colStart = tryParseInt(attrs.get(ErrorAttr.utf16ColumnStart));
const colEnd = tryParseInt(attrs.get(ErrorAttr.utf16ColumnEnd));

if (
lineStart == null ||
lineEnd == null ||
colStart == null ||
colEnd == null
lineStart != null &&
lineEnd != null &&
colStart != null &&
colEnd != null
) {
return "";
}
const queryLines = query.split("\n");

const queryLines = query.split("\n");
const lineNoWidth = lineEnd.toString().length;

const lineNoWidth = lineEnd.toString().length;
let errMessage = "\n";
errMessage += "|".padStart(lineNoWidth + 3) + "\n";

errMessage += "|".padStart(lineNoWidth + 3) + "\n";
for (let i = lineStart; i < lineEnd + 1; i++) {
const line = queryLines[i - 1];
const start = i === lineStart ? colStart : 0;
const end = i === lineEnd ? colEnd : line.length;
errMessage += ` ${i.toString().padStart(lineNoWidth)} | ${line}\n`;
errMessage += `${"|".padStart(lineNoWidth + 3)} ${""
.padStart(end - start, "^")
.padStart(end)}\n`;
}
}

for (let i = lineStart; i < lineEnd + 1; i++) {
const line = queryLines[i - 1];
const start = i === lineStart ? colStart : 0;
const end = i === lineEnd ? colEnd : line.length;
errMessage += ` ${i.toString().padStart(lineNoWidth)} | ${line}\n`;
errMessage += `${"|".padStart(lineNoWidth + 3)} ${""
.padStart(end - start, "^")
.padStart(end)}\n`;
if (attrs.has(ErrorAttr.details)) {
errMessage += `Details: ${utf8Decoder.decode(
attrs.get(ErrorAttr.details)
)}\n`;
}
if (attrs.has(ErrorAttr.hint)) {
errMessage += `Hint: ${utf8Decoder.decode(attrs.get(ErrorAttr.hint))}\n`;
Expand Down
17 changes: 17 additions & 0 deletions packages/driver/test/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,20 @@ test("message is set with attrs and query", () => {
expect(error.message).toContain(String(lineNum));
expect(error.message).toContain("^".repeat(columnCount));
});

test("message is set with attrs and query without position", () => {
const error = new edgedb.AccessError("test");
const attrs = new Map<number, Uint8Array>();

// hint
attrs.set(1, new Uint8Array(Buffer.from(String("hint text"), "utf-8")));
// details
attrs.set(2, new Uint8Array(Buffer.from(String("details text"), "utf-8")));

(error as any)._attrs = attrs;
(error as any)._query = "0123456789\n";
expect(error.message).toContain("test");
expect(error.message).toContain(String("hint text"));
expect(error.message).toContain(String("details text"));
expect(error.message).toBe("test\nDetails: details text\nHint: hint text\n");
});

0 comments on commit 1377d24

Please sign in to comment.