• Blog
  • Docs
  • Pricing
  • We’re hiring!
Log inSign up
dl4senses

dl4senses

x_thread_archiver

Public
Like
x_thread_archiver
Home
Code
9
src
5
.vtignore
AGENTS.md
DATABASE_ERROR_HANDLING.md
README.md
UI_IMPROVEMENTS.md
deno.json
H
main.ts
test_db.ts
Branches
1
Pull requests
Remixes
History
Environment variables
2
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Sign up now
Code
/
DATABASE_ERROR_HANDLING.md
Code
/
DATABASE_ERROR_HANDLING.md
Search
…
DATABASE_ERROR_HANDLING.md

Database Error Handling Improvements

Issue

When the Val Town application first loads, SQLite tables don't exist yet. This causes SQLITE_MISMATCH errors when trying to query non-existent tables, even though the code was catching and handling these errors.

Root Cause

The error was being caught and handled correctly (returning empty arrays/null), but the error was still being logged to the console, making it appear as if something was wrong when it was actually expected behavior.

Solution

Improved Error Handling Pattern

All database read operations now use this pattern:

try { const result = await sqlite.execute(query, params); // Process results } catch (error: any) { // Silently handle table not existing yet - this is expected on first load if (error?.code === 'SQLITE_MISMATCH' || error?.message?.includes('no such table')) { return []; // or null, depending on function } // Only log unexpected errors console.error('Unexpected error:', error); return []; // or null }

Functions Updated

  1. listThreads() (src/database.ts:178)

    • Silently returns empty array if tables don't exist
    • Only logs unexpected errors
  2. getThread() (src/database.ts:125)

    • Silently returns null if tables don't exist
    • Only logs unexpected errors
  3. threadExists() (src/database.ts:245)

    • Already silently returns false if tables don't exist
    • No changes needed

Expected Behavior

First Page Load:

  • Database initialization runs: initializeDatabase()
  • Creates tables if they don't exist
  • Frontend loads and calls ?list=true
  • listThreads() silently returns empty array
  • UI shows "πŸ“­ No archived threads yet"
  • No error messages in console

After First Archive:

  • Tables exist and contain data
  • All queries work normally
  • Thread list displays archived threads

Database Initialization

Tables are created on module import:

// main.ts line 9 await initializeDatabase();

This runs before the HTTP handler, ensuring tables exist for subsequent requests.

Error Categories

Silently Handled (Expected):

  • SQLITE_MISMATCH - Datatype mismatch (often means table doesn't exist)
  • no such table - Table hasn't been created yet

Logged (Unexpected):

  • Connection errors
  • Permission errors
  • Query syntax errors
  • Data integrity errors

Val Town Platform Notes

From Val Town skill documentation:

CRITICAL: When tables don't exist yet, queries will fail with SQLITE_MISMATCH errors

  • ALWAYS wrap SELECT queries in try-catch blocks and return empty arrays/false for graceful handling
  • Tables are created asynchronously on first load, but queries may run before initialization completes

This is a known Val Town SQLite behavior that requires defensive error handling.

Testing

To test the fix:

  1. Fresh deployment - No errors on first page load
  2. List empty threads - Returns empty array, no console errors
  3. Archive a thread - Works normally
  4. List with threads - Shows archived threads
  5. Get non-existent thread - Returns null gracefully

Related Files

  • src/database.ts - All database operations
  • main.ts - Database initialization
  • .claude/plugins/custom-skills/valtown-platform/references/platform.md - Platform gotchas
  • .claude/plugins/custom-skills/valtown-platform/references/libraries.md - SQLite best practices
FeaturesVersion controlCode intelligenceCLIMCP
Use cases
TeamsAI agentsSlackGTM
DocsShowcaseTemplatesNewestTrendingAPI examplesNPM packages
PricingNewsletterBlogAboutCareers
We’re hiring!
Brandhi@val.townStatus
X (Twitter)
Discord community
GitHub discussions
YouTube channel
Bluesky
Open Source Pledge
Terms of usePrivacy policyAbuse contact
Β© 2025 Val Town, Inc.