Skip to content

Add structured SQL error reporting with rich context#53

Merged
muk2 merged 2 commits intomainfrom
feature/issue-44-error-reporting
Feb 21, 2026
Merged

Add structured SQL error reporting with rich context#53
muk2 merged 2 commits intomainfrom
feature/issue-44-error-reporting

Conversation

@muk2
Copy link
Owner

@muk2 muk2 commented Feb 20, 2026

Summary

  • Replaces generic Option<String> error field with StructuredError type that extracts rich information from PostgreSQL DbError responses
  • Categorizes errors into Syntax, Semantic, Execution, Transaction, Connection types based on SQLSTATE codes
  • Computes line/column numbers from PostgreSQL's byte-position error offset
  • Renders color-coded, multi-line error display in results panel with category labels, SQLSTATE codes, detail, hints, and object context

Implementation Details

src/db/query.rs

  • Added ErrorCategory enum with Syntax, Semantic, Execution, Transaction, Connection, Unknown variants
  • Added StructuredError struct with fields: category, severity, code, message, detail, hint, position, line, col, schema, table, column, constraint, where_
  • Added from_pg_error() to extract rich info from tokio_postgres::Error via as_db_error() API
  • Added byte_offset_to_line_col() to convert PostgreSQL's 1-based byte position to (line, column)
  • Added categorize_sqlstate() mapping SQLSTATE class codes to error categories
  • Changed QueryResult.error from Option<String> to Option<StructuredError>
  • 5 new unit tests for structured error types, display, position conversion, and SQLSTATE categorization

src/ui/components.rs

  • Added draw_structured_error() function rendering rich error display with:
    • Color-coded category header (red for syntax/execution, yellow for semantic/transaction)
    • SQLSTATE code badge
    • Line/column position info highlighted in accent color
    • Detail and Hint sections with bold labels
    • Schema/table/column object context
    • Constraint name
    • PL/pgSQL call stack context
  • Results panel title now shows specific error category instead of generic "ERROR"

src/ui/app.rs

  • Status bar error messages now show category label (e.g., "Syntax Error: ...")

Error Category Mapping

SQLSTATE Class Category Example
42601 Syntax Error syntax_error
42P01, 42703 Semantic Error undefined_table, undefined_column
22, 23 Execution Error division_by_zero, constraint_violation
25, 40 Transaction Error transaction_aborted
08 Connection Error connection_exception

Test plan

  • cargo build compiles without warnings
  • cargo test - all 230 tests pass
  • Manual: Execute invalid SQL and verify structured error with line/column
  • Manual: Reference non-existent table and verify "Semantic Error" category
  • Manual: Trigger constraint violation and verify "Execution Error" with constraint name
  • Manual: Verify hints are displayed when PostgreSQL provides them

Closes #44

🤖 Generated with Claude Code

muk2 and others added 2 commits February 20, 2026 15:10
…hints

Replaces generic string errors with StructuredError type that extracts
rich information from PostgreSQL error responses: error category
(syntax/semantic/execution/transaction/connection), SQLSTATE code,
line and column numbers from byte position, detail, hint, schema/table/
column/constraint context, and PL/pgSQL call stack. The results panel
now renders errors with color-coded categories, position info, and
contextual hints instead of plain text.

Closes #44

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Run cargo fmt for consistent code formatting.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@muk2 muk2 merged commit 343f1cc into main Feb 21, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug / Feature: Proper SQL Syntax Error Reporting Instead of Generic "DB Error"

1 participant