diff --git a/packages/driver/src/errors/base.ts b/packages/driver/src/errors/base.ts index 834b81807..6e8afa3f5 100644 --- a/packages/driver/src/errors/base.ts +++ b/packages/driver/src/errors/base.ts @@ -75,35 +75,40 @@ export function prettyPrintError( attrs: Map, 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`; diff --git a/packages/driver/test/errors.test.ts b/packages/driver/test/errors.test.ts index 10f546e6f..6ad13c017 100644 --- a/packages/driver/test/errors.test.ts +++ b/packages/driver/test/errors.test.ts @@ -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(); + + // 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"); +});