DEV Community

Ramu Narasinga
Ramu Narasinga

Posted on

Error handling anti-patterns rules Claude-Mem codebase.

In this article, we review error handling anti-patterns rules script in Claude-Mem codebase. We will look at:

  1. What is Claude-Mem?

  2. anti-pattern-czar.md

  3. Anti pattern script

What is Claude-Mem?

Claude-Mem is a Claude Code plugin that automatically captures everything Claude does during your coding sessions, compresses it with AI (using Claude’s agent-sdk), and injects relevant context back into future sessions.

Claude-Mem is your AI’s trusty note-taking sidekick. Never lose track ever again.

Learn more about Claude-Mem.

anti-pattern-czar.md

I was just going through the .claude folder in the claude-mem codebase and found this file — .claude/commands/anti-pattern-czar.md

Below are the table of contents:

Let’s understand the process.

## Process

1. **Run the detector:**
Enter fullscreen mode Exit fullscreen mode


bash
bun run scripts/anti-pattern-test/detect-error-handling-antipatterns.ts


2. **Analyze the results:**

    * Count CRITICAL, HIGH, MEDIUM, and APPROVED\_OVERRIDE issues

    * Prioritize CRITICAL issues on critical paths first

    * Group similar patterns together

3. **For each CRITICAL issue:**

    a. **Read the problematic code** using the Read tool

    b. **Explain the problem:**

    * Why is this dangerous?

    * What debugging nightmare could this cause?

    * What specific error is being swallowed?


    c. **Determine the right fix:**

    * **Option 1: Add proper logging** - If this is a real error that should be visible

    * **Option 2: Add \[APPROVED OVERRIDE\]** - If this is expected/documented behavior

    * **Option 3: Remove the try-catch entirely** - If the error should propagate

    * **Option 4: Add specific error type checking** - If only certain errors should be caught


    d. **Propose the fix** and ask for approval

    e. **Apply the fix** after approval

4. **Work through issues methodically:**

    * Fix one at a time

    * Re-run the detector after each batch of fixes

    * Track progress: "Fixed 3/28 critical issues"


Enter fullscreen mode Exit fullscreen mode


`javascript

javascript
bun run scripts/anti-pattern-test/detect-error-handling-antipatterns.ts
`

This is how I learnt that there is a script called detect-error-handling-antipatterns.ts

Once you run this script, you want to analyze the results as the next step in the process.

`javascript

  1. Analyze the results:
    • Count CRITICAL, HIGH, MEDIUM, and APPROVED_OVERRIDE issues
    • Prioritize CRITICAL issues on critical paths first
    • Group similar patterns together `

Then the next set of instructions are about how the critical issues should be resolved.

Anti pattern script

detect-error-handling-antipatterns.ts has about 514 LOC and has the following functions defined:

In the detectAntiPatterns function, I found regex being used to detect anti-patterns

`javascript
// HIGH: Logging only error.message instead of the full error object
// Patterns like: logger.error('X', 'Y', {}, error.message) or console.error(error.message)
const partialErrorLoggingPatterns = [
/logger.(error|warn|info|debug|failure)\s*([^)],\s(?:error|err|e).message\s*)/,
/logger.(error|warn|info|debug|failure)\s*([^)]{\s(?:error|err|e):\s*(?:error|err|e).message\s*}/,
/console.(error|warn|log)\s*(\s*(?:error|err|e).message\s*)/,
/console.(error|warn|log)\s*(\s*['"][^'"]+['"`]\s*,\s*(?:error|err|e).message\s*)/,
];

for (const pattern of partialErrorLoggingPatterns) {
  if (pattern.test(trimmed)) {
    if (hasOverride && overrideReason) {
      antiPatterns.push({
        file: relPath,
        line: i + 1,
Enter fullscreen mode Exit fullscreen mode

This is one example above.

Below is another example

Enter fullscreen mode Exit fullscreen mode


javascript
// CRITICAL: Error message string matching for type detection
// Patterns like: errorMessage.includes('connection') or error.message.includes('timeout')
const errorStringMatchPatterns = [
/error(?:Message|.message)\s*.includes\s*(\s*'"`['"]\s*\)/i,
/(?:err|e)\.message\s*\.includes\s*\(\s*['"
](\w+)['"]\s*\)/i,
/String\s*\(\s*(?:error|err|e)\s*\)\s*\.includes\s*\(\s*['"
](\w+)['"`]\s*)/i,
];




This reminds me of a concept called [AST](https://stackoverflow.com/questions/16127985/what-is-javascript-ast-how-to-play-with-it), I wonder if there’s any benefits in using AST over regex here.

### About me:

Hey, my name is [Ramu Narasinga](https://ramunarasinga.com/). I study codebase architecture in large open-source projects.

Email: ramu.narasinga@gmail.com

> *I spent 200+ hours analyzing Supabase, shadcn/ui, LobeChat. Found the patterns that separate AI slop from production code. Stop refactoring AI slop. Start with proven patterns. Check out production-grade projects at* [*thinkthroo.com*](http://thinkthroo.com/)

### References:

1. [https://github.com/thedotmack/claude-mem](https://github.com/thedotmack/claude-mem)

2. [https://claude-mem.ai/](https://claude-mem.ai/)

3. [claude-mem/scripts/anti-pattern-test/detect-error-handling-antipatterns.ts](https://github.com/thedotmack/claude-mem/blob/main/scripts/anti-pattern-test/detect-error-handling-antipatterns.ts)

4. [claude-mem/.claude/commands/anti-pattern-czar.md](https://github.com/thedotmack/claude-mem/blob/main/.claude/commands/anti-pattern-czar.md)

5. [questions/16127985/what-is-javascript-ast-how-to-play-with-it](https://stackoverflow.com/questions/16127985/what-is-javascript-ast-how-to-play-with-it)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)