Error Codes¶
DecentDb uses specific error codes to indicate different types of failures.
Error Code Reference¶
ERR_IO (I/O Error)¶
File system operations failed.
Common Causes: - Disk full - Permission denied - File not found - Hardware failure
Example:
Resolution: - Check disk space: df -h - Check permissions: ls -la - Verify path exists
ERR_CORRUPTION (Database Corruption)¶
Database file structure is invalid.
Common Causes: - Unexpected shutdown during write - Storage hardware failure - Manual file modification - Bug in database engine
Example:
Resolution: - Restore from backup - Use PRAGMA integrity_check - Contact support if reproducible
ERR_CONSTRAINT (Constraint Violation)¶
Data violates schema constraints.
Common Causes: - Duplicate PRIMARY KEY - Violates UNIQUE constraint - Violates NOT NULL - Violates FOREIGN KEY
Example:
-- Table with UNIQUE constraint
CREATE TABLE users (id INT PRIMARY KEY, email TEXT UNIQUE);
INSERT INTO users VALUES (1, 'alice@example.com');
INSERT INTO users VALUES (2, 'alice@example.com');
-- Error: ERR_CONSTRAINT: UNIQUE constraint violation
Resolution: - Check for existing values - Handle duplicates in application - Use INSERT OR REPLACE (if supported)
ERR_TRANSACTION (Transaction Error)¶
Transaction-related failures.
Common Causes: - Deadlock (rare in DecentDb) - Lock timeout - Write conflict
Example:
# Two processes trying to write simultaneously
# Process 1: BEGIN; UPDATE ...
# Process 2: BEGIN; UPDATE ... (waits)
Resolution: - Retry the transaction - Keep transactions short - Use appropriate isolation level
ERR_SQL (SQL Error)¶
Invalid SQL syntax or semantic error.
Common Causes: - Syntax error in SQL - Table doesn't exist - Column doesn't exist - Type mismatch
Example:
decentdb exec --db=my.ddb --sql="SELECT * FROM nonexistent"
# Error: ERR_SQL: Table not found
decentdb exec --db=my.ddb --sql="SELEC * FROM users"
# Error: ERR_SQL: Syntax error
Resolution: - Verify table/column names - Check SQL syntax - Use DESCRIBE to see schema
ERR_INTERNAL (Internal Error)¶
Unexpected internal engine error.
Common Causes: - Bug in DecentDb - Memory allocation failure - Internal assertion failure
Example:
Resolution: - Report bug with reproduction steps - Check system resources - Restart application
Error Response Format¶
CLI errors are returned as JSON:
{
"ok": false,
"error": {
"code": "ERR_SQL",
"message": "Table not found",
"context": "users"
},
"rows": []
}
Fields: - code: Error category (see above) - message: Human-readable description - context: Additional context (table name, column name, etc.)
Nim API Error Handling¶
import decentdb/engine
let res = execSql(db, "SELECT * FROM users")
if not res.ok:
echo "Error code: ", res.err.code
echo "Message: ", res.err.message
echo "Context: ", res.err.context
case res.err.code
of ERR_SQL:
echo "Fix your SQL query"
of ERR_CONSTRAINT:
echo "Check your data"
of ERR_IO:
echo "Check file system"
else:
echo "Unexpected error"
Common Error Patterns¶
"Table not found"¶
Check: 1. Did you create the table? 2. Is the database path correct? 3. Are you using the right database file?
"UNIQUE constraint violation"¶
Check: 1. Does the value already exist? 2. Are you inserting duplicates?
"FOREIGN KEY constraint violation"¶
Check: 1. Does the referenced row exist? 2. Is the foreign key value correct?
"Missing parameter"¶
Check: 1. Did you provide all parameters? 2. Are parameter indices correct ($1, $2, etc.)?
"Type mismatch"¶
Check: 1. Are you passing the right type (int vs text)? 2. Is the column type what you expect?
Troubleshooting Guide¶
Database Won't Open¶
- Check file permissions
- Verify file isn't corrupted
- Ensure sufficient disk space
- Check if another process has locked it
Queries Are Slow¶
- Check if indexes exist:
list-indexes - Analyze query plan
- Consider adding indexes
- Check cache size
Write Failures¶
- Check disk space
- Verify write permissions
- Check if database is read-only
- Ensure single writer
Recovery Mode¶
If database is corrupted:
# Check integrity
decentdb exec --db=my.ddb --sql="PRAGMA integrity_check"
# Export and reimport if needed
decentdb export --db=my.ddb --table=users --file=users.csv
# Create new database and import
Getting Help¶
If you encounter an error:
- Check this error code reference
- Review the SQL Reference
- Check common issues on GitHub
- Report new issues with:
- Error code and message
- Steps to reproduce
- Expected vs actual behavior