|
| 1 | +/* Copyright 2024 Marimo. All rights reserved. */ |
| 2 | + |
| 3 | +import { describe, expect, it } from "vitest"; |
| 4 | +import { exportedForTesting } from "../languages/sql/validation-errors"; |
| 5 | + |
| 6 | +describe("Error Message Splitting", () => { |
| 7 | + it("should handle error message splitting correctly", () => { |
| 8 | + const { splitErrorMessage } = exportedForTesting; |
| 9 | + |
| 10 | + const result1 = splitErrorMessage("Syntax error: unexpected token"); |
| 11 | + expect(result1.errorType).toBe("Syntax error"); |
| 12 | + expect(result1.errorMessage).toBe("unexpected token"); |
| 13 | + |
| 14 | + const result2 = splitErrorMessage("Multiple: colons: in error"); |
| 15 | + expect(result2.errorType).toBe("Multiple"); |
| 16 | + expect(result2.errorMessage).toBe("colons: in error"); |
| 17 | + |
| 18 | + const result3 = splitErrorMessage("No colon error"); |
| 19 | + expect(result3.errorType).toBe("No colon error"); |
| 20 | + expect(result3.errorMessage).toBe(""); |
| 21 | + }); |
| 22 | +}); |
| 23 | + |
| 24 | +describe("DuckDB Error Handling", () => { |
| 25 | + it("should extract codeblock from error with LINE information", () => { |
| 26 | + const { handleDuckdbError } = exportedForTesting; |
| 27 | + |
| 28 | + const error = |
| 29 | + 'Binder Error: Referenced column "attacks" not found in FROM clause! Candidate bindings: "Attack", "Total" LINE 1:... from pokemon WHERE \'type_2\' = 32 and attack = 32 and not attacks = \'hi\' ^'; |
| 30 | + |
| 31 | + const result = handleDuckdbError(error); |
| 32 | + |
| 33 | + expect(result.errorType).toBe("Binder Error"); |
| 34 | + expect(result.errorMessage).toBe( |
| 35 | + 'Referenced column "attacks" not found in FROM clause! Candidate bindings: "Attack", "Total"', |
| 36 | + ); |
| 37 | + expect(result.codeblock).toBe( |
| 38 | + "LINE 1:... from pokemon WHERE 'type_2' = 32 and attack = 32 and not attacks = 'hi' ^", |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should handle error without LINE information", () => { |
| 43 | + const { handleDuckdbError } = exportedForTesting; |
| 44 | + |
| 45 | + const error = "Syntax Error: Invalid syntax near WHERE"; |
| 46 | + |
| 47 | + const result = handleDuckdbError(error); |
| 48 | + |
| 49 | + expect(result.errorType).toBe("Syntax Error"); |
| 50 | + expect(result.errorMessage).toBe("Invalid syntax near WHERE"); |
| 51 | + expect(result.codeblock).toBeUndefined(); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should handle error with LINE at the beginning", () => { |
| 55 | + const { handleDuckdbError } = exportedForTesting; |
| 56 | + |
| 57 | + const error = "LINE 1: SELECT * FROM table WHERE invalid_column = 1 ^"; |
| 58 | + |
| 59 | + const result = handleDuckdbError(error); |
| 60 | + |
| 61 | + expect(result.errorType).toBe("LINE 1"); |
| 62 | + expect(result.errorMessage).toBe( |
| 63 | + "SELECT * FROM table WHERE invalid_column = 1 ^", |
| 64 | + ); |
| 65 | + expect(result.codeblock).toBeUndefined(); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should handle error with multiple LINE occurrences", () => { |
| 69 | + const { handleDuckdbError } = exportedForTesting; |
| 70 | + |
| 71 | + const error = |
| 72 | + "Error: Something went wrong LINE 1: SELECT * FROM table WHERE invalid_column = 1 ^"; |
| 73 | + |
| 74 | + const result = handleDuckdbError(error); |
| 75 | + |
| 76 | + expect(result.errorType).toBe("Error"); |
| 77 | + expect(result.errorMessage).toBe("Something went wrong"); |
| 78 | + expect(result.codeblock).toBe( |
| 79 | + "LINE 1: SELECT * FROM table WHERE invalid_column = 1 ^", |
| 80 | + ); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should handle complex error with nested quotes", () => { |
| 84 | + const { handleDuckdbError } = exportedForTesting; |
| 85 | + |
| 86 | + const error = |
| 87 | + "Binder Error: Column \"name\" not found! LINE 1: SELECT * FROM users WHERE name = 'John' AND age > 25 ^"; |
| 88 | + |
| 89 | + const result = handleDuckdbError(error); |
| 90 | + |
| 91 | + expect(result.errorType).toBe("Binder Error"); |
| 92 | + expect(result.errorMessage).toBe('Column "name" not found!'); |
| 93 | + expect(result.codeblock).toBe( |
| 94 | + "LINE 1: SELECT * FROM users WHERE name = 'John' AND age > 25 ^", |
| 95 | + ); |
| 96 | + }); |
| 97 | + |
| 98 | + it("should handle error with LINE but no caret", () => { |
| 99 | + const { handleDuckdbError } = exportedForTesting; |
| 100 | + |
| 101 | + const error = "Error: Invalid query LINE 1: SELECT * FROM table"; |
| 102 | + |
| 103 | + const result = handleDuckdbError(error); |
| 104 | + |
| 105 | + expect(result.errorType).toBe("Error"); |
| 106 | + expect(result.errorMessage).toBe("Invalid query"); |
| 107 | + expect(result.codeblock).toBe("LINE 1: SELECT * FROM table"); |
| 108 | + }); |
| 109 | + |
| 110 | + it("should trim whitespace from codeblock", () => { |
| 111 | + const { handleDuckdbError } = exportedForTesting; |
| 112 | + |
| 113 | + const error = "Error: Something wrong LINE 1: SELECT * FROM table ^ "; |
| 114 | + |
| 115 | + const result = handleDuckdbError(error); |
| 116 | + |
| 117 | + expect(result.errorType).toBe("Error"); |
| 118 | + expect(result.errorMessage).toBe("Something wrong"); |
| 119 | + expect(result.codeblock).toBe("LINE 1: SELECT * FROM table ^"); |
| 120 | + }); |
| 121 | + |
| 122 | + it("should handle empty error message", () => { |
| 123 | + const { handleDuckdbError } = exportedForTesting; |
| 124 | + |
| 125 | + const error = ""; |
| 126 | + |
| 127 | + const result = handleDuckdbError(error); |
| 128 | + |
| 129 | + expect(result.errorType).toBe(""); |
| 130 | + expect(result.errorMessage).toBe(""); |
| 131 | + expect(result.codeblock).toBeUndefined(); |
| 132 | + }); |
| 133 | +}); |
0 commit comments