@@ -21,8 +21,16 @@ import (
2121 "github.com/ajitpratap0/GoSQLX/pkg/models"
2222)
2323
24- // FormatErrorWithContext formats an error with SQL context and visual indicators
25- // This is a convenience function that wraps the Error.Error() method
24+ // FormatErrorWithContext formats an error with SQL context and visual indicators.
25+ // For *Error values it delegates to the structured Error.Error() method, which
26+ // includes code, location, SQL context highlighting, hint, and help URL. For all
27+ // other error types it falls back to a plain "Error: <message>" string.
28+ //
29+ // Parameters:
30+ // - err: The error to format (may be *Error or a generic error)
31+ // - sql: The SQL source; currently unused for *Error (context is already attached)
32+ //
33+ // Returns the formatted error string ready for display to end users.
2634func FormatErrorWithContext (err error , sql string ) string {
2735 // If it's already a structured error, just return its formatted string
2836 if structErr , ok := err .(* Error ); ok {
@@ -33,7 +41,19 @@ func FormatErrorWithContext(err error, sql string) string {
3341 return fmt .Sprintf ("Error: %v" , err )
3442}
3543
36- // FormatErrorWithContextAt formats an error at a specific location with SQL context
44+ // FormatErrorWithContextAt creates a structured error for the given code and location,
45+ // attaches the SQL context window, and auto-generates a hint. It then returns the
46+ // fully-formatted error string. This is useful for one-shot error formatting without
47+ // retaining the *Error value.
48+ //
49+ // Parameters:
50+ // - code: ErrorCode classifying the error category (e.g., ErrCodeExpectedToken)
51+ // - message: Human-readable description of the error
52+ // - location: Precise line/column where the error occurred
53+ // - sql: Full SQL source used to generate the context window
54+ // - highlightLen: Number of characters to highlight at the error column
55+ //
56+ // Returns the complete formatted error string including context highlighting.
3757func FormatErrorWithContextAt (code ErrorCode , message string , location models.Location , sql string , highlightLen int ) string {
3858 err := NewError (code , message , location )
3959 err = err .WithContext (sql , highlightLen )
@@ -46,8 +66,17 @@ func FormatErrorWithContextAt(code ErrorCode, message string, location models.Lo
4666 return err .Error ()
4767}
4868
49- // FormatMultiLineContext formats error context for multi-line SQL with extended context
50- // Shows up to 3 lines (1 before, error line, 1 after) with proper indentation
69+ // FormatMultiLineContext formats an SQL context window around a specific error location.
70+ // It shows up to three lines: one before the error, the error line itself, and one
71+ // after. A caret indicator (^) is rendered below the error column, with optional
72+ // multi-character highlighting when highlightLen > 1.
73+ //
74+ // Parameters:
75+ // - sql: The full SQL source string (may contain newlines)
76+ // - location: The line/column of the error (1-based)
77+ // - highlightLen: Number of characters to highlight; 1 renders a single caret
78+ //
79+ // Returns the formatted context block, or an empty string if location is invalid.
5180func FormatMultiLineContext (sql string , location models.Location , highlightLen int ) string {
5281 if sql == "" || location .Line <= 0 {
5382 return ""
@@ -107,8 +136,18 @@ func FormatMultiLineContext(sql string, location models.Location, highlightLen i
107136 return sb .String ()
108137}
109138
110- // FormatErrorSummary provides a brief summary of an error without full context
111- // Useful for logging or when SQL context is not needed
139+ // FormatErrorSummary provides a concise one-line error summary suitable for
140+ // structured logging and monitoring systems where a full context window would
141+ // be too verbose. For *Error values the output format is:
142+ //
143+ // [E2001] unexpected token: COMMA at line 5, column 20
144+ //
145+ // For other error types the output is "Error: <message>".
146+ //
147+ // Parameters:
148+ // - err: The error to summarise
149+ //
150+ // Returns the one-line summary string.
112151func FormatErrorSummary (err error ) string {
113152 if structErr , ok := err .(* Error ); ok {
114153 return fmt .Sprintf ("[%s] %s at line %d, column %d" ,
@@ -120,7 +159,20 @@ func FormatErrorSummary(err error) string {
120159 return fmt .Sprintf ("Error: %v" , err )
121160}
122161
123- // FormatErrorWithSuggestion formats an error with an intelligent suggestion
162+ // FormatErrorWithSuggestion creates and formats a structured error that includes a
163+ // manually provided hint. When suggestion is empty, the function falls back to
164+ // auto-generating a hint via GenerateHint. This is the preferred formatter when
165+ // the caller already knows the correct fix.
166+ //
167+ // Parameters:
168+ // - code: ErrorCode classifying the error category
169+ // - message: Human-readable description of the error
170+ // - location: Precise line/column where the error occurred
171+ // - sql: Full SQL source used to generate the context window
172+ // - highlightLen: Number of characters to highlight at the error column
173+ // - suggestion: Custom hint text; empty string triggers auto-generation
174+ //
175+ // Returns the complete formatted error string.
124176func FormatErrorWithSuggestion (code ErrorCode , message string , location models.Location , sql string , highlightLen int , suggestion string ) string {
125177 err := NewError (code , message , location )
126178 err = err .WithContext (sql , highlightLen )
@@ -137,7 +189,16 @@ func FormatErrorWithSuggestion(code ErrorCode, message string, location models.L
137189 return err .Error ()
138190}
139191
140- // FormatErrorList formats multiple errors in a readable list
192+ // FormatErrorList formats a slice of structured errors into a numbered list with
193+ // full context for each entry. The output begins with a count line and separates
194+ // each error with a blank line.
195+ //
196+ // Returns "No errors" when the slice is empty.
197+ //
198+ // Parameters:
199+ // - errors: Slice of *Error values to format
200+ //
201+ // Returns the multi-error report string.
141202func FormatErrorList (errors []* Error ) string {
142203 if len (errors ) == 0 {
143204 return "No errors"
@@ -155,7 +216,21 @@ func FormatErrorList(errors []*Error) string {
155216 return sb .String ()
156217}
157218
158- // FormatErrorWithExample formats an error with a corrected example
219+ // FormatErrorWithExample formats an error and appends a side-by-side "Wrong / Correct"
220+ // example to the hint. This is particularly useful for educational error messages
221+ // (e.g., in linters or IDEs) where showing the correct pattern helps users learn
222+ // the expected SQL syntax.
223+ //
224+ // Parameters:
225+ // - code: ErrorCode classifying the error category
226+ // - message: Human-readable description of the error
227+ // - location: Precise line/column where the error occurred
228+ // - sql: Full SQL source used to generate the context window
229+ // - highlightLen: Number of characters to highlight at the error column
230+ // - wrongExample: The erroneous SQL fragment (e.g., "SELECT * FORM users")
231+ // - correctExample: The corrected SQL fragment (e.g., "SELECT * FROM users")
232+ //
233+ // Returns the complete formatted error string including the before/after example.
159234func FormatErrorWithExample (code ErrorCode , message string , location models.Location , sql string , highlightLen int , wrongExample , correctExample string ) string {
160235 err := NewError (code , message , location )
161236 err = err .WithContext (sql , highlightLen )
@@ -167,7 +242,19 @@ func FormatErrorWithExample(code ErrorCode, message string, location models.Loca
167242 return err .Error ()
168243}
169244
170- // FormatContextWindow formats a larger context window (up to N lines before and after)
245+ // FormatContextWindow formats a configurable SQL context window of up to linesBefore
246+ // lines before and linesAfter lines after the error line. Prefer this over
247+ // FormatMultiLineContext when more surrounding context is needed (e.g., in IDE
248+ // hover messages or verbose diagnostic reports).
249+ //
250+ // Parameters:
251+ // - sql: The full SQL source string
252+ // - location: The line/column of the error (1-based)
253+ // - highlightLen: Number of characters to highlight at the error column
254+ // - linesBefore: Number of source lines to display before the error line
255+ // - linesAfter: Number of source lines to display after the error line
256+ //
257+ // Returns the formatted context block, or an empty string if location is invalid.
171258func FormatContextWindow (sql string , location models.Location , highlightLen int , linesBefore , linesAfter int ) string {
172259 if sql == "" || location .Line <= 0 {
173260 return ""
@@ -231,21 +318,40 @@ func FormatContextWindow(sql string, location models.Location, highlightLen int,
231318 return sb .String ()
232319}
233320
234- // IsStructuredError checks if an error is a structured GoSQLX error
321+ // IsStructuredError reports whether err is a GoSQLX *Error value.
322+ // Use this to distinguish GoSQLX structured errors from generic Go errors
323+ // before calling functions that require *Error (e.g., ExtractLocation).
324+ //
325+ // Example:
326+ //
327+ // if errors.IsStructuredError(err) {
328+ // loc, _ := errors.ExtractLocation(err)
329+ // // use loc for IDE diagnostics
330+ // }
235331func IsStructuredError (err error ) bool {
236332 _ , ok := err .(* Error )
237333 return ok
238334}
239335
240- // ExtractLocation extracts location information from an error
336+ // ExtractLocation extracts the line/column location from a GoSQLX *Error.
337+ // This is the preferred way to obtain location data for IDE integrations such as
338+ // LSP diagnostics, since it handles the type assertion safely.
339+ //
340+ // Returns the Location and true when err is a *Error; returns a zero Location
341+ // and false for all other error types.
241342func ExtractLocation (err error ) (models.Location , bool ) {
242343 if structErr , ok := err .(* Error ); ok {
243344 return structErr .Location , true
244345 }
245346 return models.Location {}, false
246347}
247348
248- // ExtractErrorCode extracts the error code from an error
349+ // ExtractErrorCode extracts the ErrorCode from a GoSQLX *Error.
350+ // Unlike GetCode, this function returns a boolean indicating whether the extraction
351+ // succeeded, making it suitable for use in type-switch–style handling.
352+ //
353+ // Returns the ErrorCode and true when err is a *Error; returns an empty string
354+ // and false for all other error types.
249355func ExtractErrorCode (err error ) (ErrorCode , bool ) {
250356 if structErr , ok := err .(* Error ); ok {
251357 return structErr .Code , true
0 commit comments