@@ -39,46 +39,78 @@ export function hasSqlDetails(error: unknown): error is SqlError {
3939 return error instanceof SqlError && error . sqlDetails !== undefined ;
4040}
4141
42- export function formatSqlError ( error : SqlError , query ?: string , options ?: {
43- showPosition ?: boolean ;
44- showSource ?: boolean ;
45- useColors ?: boolean ;
46- } ) : string {
47- const opts = { showPosition : true , showSource : true , useColors : false , ...options } ;
48- let output = `Error: ${ error . message } ` ;
49-
42+ export function formatSqlError (
43+ error : SqlError ,
44+ query : string ,
45+ options : {
46+ showPosition ?: boolean ;
47+ showQuery ?: boolean ;
48+ color ?: boolean ;
49+ maxQueryLength ?: number ;
50+ } = { }
51+ ) : string {
52+ const {
53+ showPosition = true ,
54+ showQuery = true ,
55+ color = false ,
56+ maxQueryLength
57+ } = options ;
58+
59+ const lines : string [ ] = [ ] ;
60+
61+ // ANSI color codes
62+ const red = color ? '\x1b[31m' : '' ;
63+ const yellow = color ? '\x1b[33m' : '' ;
64+ const reset = color ? '\x1b[0m' : '' ;
65+
66+ // Add error message
67+ lines . push ( `${ red } Error: ${ error . message } ${ reset } ` ) ;
68+
69+ // Add SQL details if available
5070 if ( error . sqlDetails ) {
51- const details = error . sqlDetails ;
52-
53- if ( opts . showPosition && details . cursorPosition !== undefined ) {
54- output += `\nPosition : ${ details . cursorPosition } `;
71+ const { cursorPosition , fileName , functionName , lineNumber } = error . sqlDetails ;
72+
73+ if ( cursorPosition !== undefined && cursorPosition >= 0 ) {
74+ lines . push ( `Position : ${ cursorPosition } `) ;
5575 }
56-
57- if ( opts . showSource && ( details . fileName || details . functionName || details . lineNumber ) ) {
58- output += '\nSource:' ;
59- if ( details . fileName ) output += ` file: ${ details . fileName } ,` ;
60- if ( details . functionName ) output += ` function: ${ details . functionName } ,` ;
61- if ( details . lineNumber ) output += ` line: ${ details . lineNumber } ` ;
76+
77+ if ( fileName || functionName || lineNumber ) {
78+ const details = [ ] ;
79+ if ( fileName ) details . push ( `file: ${ fileName } ` ) ;
80+ if ( functionName ) details . push ( `function: ${ functionName } ` ) ;
81+ if ( lineNumber ) details . push ( `line: ${ lineNumber } ` ) ;
82+ lines . push ( `Source: ${ details . join ( ', ' ) } ` ) ;
6283 }
63-
64- if ( opts . showPosition && query && details . cursorPosition !== undefined && details . cursorPosition >= 0 ) {
65- const lines = query . split ( '\n' ) ;
66- let currentPos = 0 ;
67-
68- for ( let i = 0 ; i < lines . length ; i ++ ) {
69- const lineLength = lines [ i ] . length + 1 ; // +1 for newline
70- if ( currentPos + lineLength > details . cursorPosition ) {
71- const posInLine = details . cursorPosition - currentPos ;
72- output += `\n${ lines [ i ] } ` ;
73- output += '\n' + ' ' . repeat ( posInLine ) + '^' ;
74- break ;
75- }
76- currentPos += lineLength ;
84+
85+ // Show query with position marker
86+ if ( showQuery && showPosition && cursorPosition !== undefined && cursorPosition >= 0 ) {
87+ let displayQuery = query ;
88+ let adjustedPosition = cursorPosition ;
89+
90+ // Truncate if needed
91+ if ( maxQueryLength && query . length > maxQueryLength ) {
92+ const start = Math . max ( 0 , cursorPosition - Math . floor ( maxQueryLength / 2 ) ) ;
93+ const end = Math . min ( query . length , start + maxQueryLength ) ;
94+ displayQuery = ( start > 0 ? '...' : '' ) +
95+ query . substring ( start , end ) +
96+ ( end < query . length ? '...' : '' ) ;
97+ // Adjust cursor position for truncation
98+ adjustedPosition = cursorPosition - start + ( start > 0 ? 3 : 0 ) ;
7799 }
100+
101+ lines . push ( displayQuery ) ;
102+ lines . push ( ' ' . repeat ( adjustedPosition ) + `${ yellow } ^${ reset } ` ) ;
103+ }
104+ } else if ( showQuery ) {
105+ // No SQL details, just show the query if requested
106+ let displayQuery = query ;
107+ if ( maxQueryLength && query . length > maxQueryLength ) {
108+ displayQuery = query . substring ( 0 , maxQueryLength ) + '...' ;
78109 }
110+ lines . push ( `Query: ${ displayQuery } ` ) ;
79111 }
80-
81- return output ;
112+
113+ return lines . join ( '\n' ) ;
82114}
83115
84116// @ts -ignore
@@ -190,7 +222,7 @@ export const parse = awaitInit(async (query: string): Promise<ParseResult> => {
190222
191223 throw new SqlError ( message , {
192224 message,
193- cursorPosition : cursorpos ,
225+ cursorPosition : cursorpos > 0 ? cursorpos - 1 : 0 , // Convert to 0-based
194226 fileName : filename ,
195227 functionName : funcname ,
196228 lineNumber : lineno > 0 ? lineno : undefined
@@ -346,7 +378,7 @@ export function parseSync(query: string): ParseResult {
346378
347379 throw new SqlError ( message , {
348380 message,
349- cursorPosition : cursorpos ,
381+ cursorPosition : cursorpos > 0 ? cursorpos - 1 : 0 , // Convert to 0-based
350382 fileName : filename ,
351383 functionName : funcname ,
352384 lineNumber : lineno > 0 ? lineno : undefined
0 commit comments