Skip to content

Attach error stack and log tail to Slack on crash#6

Open
slacki-ai wants to merge 2 commits intomainfrom
error-diagnostics-attachments
Open

Attach error stack and log tail to Slack on crash#6
slacki-ai wants to merge 2 commits intomainfrom
error-diagnostics-attachments

Conversation

@slacki-ai
Copy link
Copy Markdown
Collaborator

Summary

When ClaudeX catches an unhandled error (e.g. "Claude Code process exited with code 1"), the thread currently only receives a terse error text message. This PR makes crashes self-diagnosing by attaching two files to the same Slack thread immediately after that message:

  • error-stack.txt — the full JavaScript Error.stack trace (or the stringified error if it isn't an Error instance)
  • service-logs.txt — the last 10 000 lines of the active logs/service_*.log file

Changes

File What changed
src/util/log-reader.ts (new) getLatestLogFile() — locates the newest service_*.log; readLastNLines() — reads the tail of a file
src/slack/files.ts Added uploadContentAsFile() — writes a string to a UUID-named temp file, uploads via filesUploadV2, then cleans up
src/slack/events.ts Error handler calls both helpers after posting the error text; each upload is wrapped in its own try/catch so a failure to upload diagnostics never hides the original error

Test plan

  • Trigger a crash (e.g. kill the Claude Code subprocess manually) and confirm error-stack.txt appears in the Slack thread
  • Confirm service-logs.txt is also attached and contains recent log lines
  • Confirm the original :x: Sorry, something went wrong: … message still appears first
  • Confirm a failure to upload the diagnostic files does not suppress the error message

🤖 Generated with Claude Code

slacki-ai and others added 2 commits March 17, 2026 06:16
When ClaudeX catches an error (e.g. "Claude Code process exited with
code 1"), the error handler now posts two additional file attachments
to the same Slack thread:

- error-stack.txt — the full JavaScript Error stack trace
- service-logs.txt — the last 10 000 lines of the active service log

This makes crashes self-diagnosing directly in Slack without requiring
manual log inspection.

New file: src/util/log-reader.ts
  - getLatestLogFile() — finds the newest service_*.log in logs/
  - readLastNLines()   — reads the last N lines from a file

Modified: src/slack/files.ts
  - uploadContentAsFile() — writes a string to a tmp file and uploads
    it to Slack, then cleans up the tmp dir

Modified: src/slack/events.ts
  - Error handler now calls both helpers after posting the error text

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sets up Vitest (first test infrastructure in the repo) and adds 19
tests covering the two new utilities introduced in the previous commit:

src/util/log-reader.test.ts (12 tests)
  getLatestLogFile:
  - returns null for empty / missing logs directory
  - ignores files that don't match service_*.log
  - returns the single log file when only one exists
  - returns the most recently modified file among multiple candidates
  - handles three files and picks the newest

  readLastNLines:
  - returns error string for non-existent file
  - returns full content when file has fewer lines than requested
  - returns exactly the last N lines
  - handles empty file, n=1, n=0, and trailing-newline edge cases

src/slack/files.test.ts (7 tests)
  uploadContentAsFile:
  - calls filesUploadV2 with the correct channel, thread, and filename
  - verifies the temp file is cleaned up after a successful upload
  - verifies the temp file is cleaned up even when filesUploadV2 throws
  - uploads empty string without error
  - uploads a 10 000-line string (simulating a log tail) without error
  - preserves the exact filename argument

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

1 participant