Analyzes SQL query errors and provides actionable suggestions.
Classifies different types of database errors and generates helpful suggestions to guide the AI in fixing the query.
Error Types
:column_not_found- Referenced column doesn't exist:table_not_found- Referenced table doesn't exist:syntax_error- SQL syntax is invalid:type_mismatch- Data type incompatibility:ambiguous_column- Column name is ambiguous in JOIN:permission_denied- Access denied to table/column:unknown- Other unclassified errors
Usage
error_context = ErrorDetector.analyze_error(
"column 'status' does not exist",
"SELECT status FROM users",
%{tables_analyzed: ["users"]}
)
error_context.error_type
# => :column_not_found
error_context.suggestions
# => ["Use get_table_schema() to see available columns", ...]
Summary
Functions
Analyze a query error and generate actionable suggestions.
Classify the type of error based on the error message.
Generate helpful suggestions for fixing the error.
Types
@type error_context() :: %{ error_type: error_type(), error_message: String.t(), failed_sql: String.t() | nil, suggestions: [String.t()] }
@type error_type() ::
:column_not_found
| :table_not_found
| :syntax_error
| :type_mismatch
| :ambiguous_column
| :permission_denied
| :unknown
Functions
@spec analyze_error(String.t(), String.t() | nil, map()) :: error_context()
Analyze a query error and generate actionable suggestions.
Parameters
error_message- The error message from the databasesql- The SQL query that failed (optional)schema_context- Context about tables analyzed (optional)
Returns
Map containing error type, message, failed SQL, and suggestions for fixing.
Examples
iex> result = ErrorDetector.analyze_error(
...> "column 'status' does not exist",
...> "SELECT status FROM users",
...> %{tables_analyzed: ["users"]}
...> )
iex> result.error_type
:column_not_found
iex> result.error_message
"column 'status' does not exist"
iex> result.failed_sql
"SELECT status FROM users"
iex> Enum.any?(result.suggestions, &String.contains?(&1, "get_table_schema"))
true
@spec classify_error(String.t()) :: error_type()
Classify the type of error based on the error message.
Examples
iex> ErrorDetector.classify_error("column 'foo' does not exist")
:column_not_found
iex> ErrorDetector.classify_error("relation 'bar' does not exist")
:table_not_found
iex> ErrorDetector.classify_error("syntax error at or near 'SELECT'")
:syntax_error
@spec suggest_fixes(error_type(), String.t(), String.t() | nil, map()) :: [String.t()]
Generate helpful suggestions for fixing the error.
Parameters
error_type- Classified error typeerror_message- Original error messagesql- Failed SQL query (optional)schema_context- Schema context map (optional)
Returns
List of actionable suggestion strings.