{"record":{"id":"b43950ee80a9128e","repo":"mastra-ai/mastra","slug":"pii-detection-failed-error-instanceof-error-e","errorCode":null,"errorMessage":"PII detection failed: ${error instanceof Error ? error.stack : 'Unknown error'}","messagePattern":"PII detection failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/processors/processors/pii-detector.ts","lineNumber":346,"sourceCode":"          } else if (this.strategy === 'redact') {\n            if (processedMessage) {\n              processedMessages.push(processedMessage);\n            } else {\n              processedMessages.push(message); // Fallback to original if redaction failed\n            }\n            continue;\n          }\n        }\n\n        processedMessages.push(message);\n      }\n\n      return processedMessages;\n    } catch (error) {\n      if (error instanceof TripWire) {\n        throw error; // Re-throw tripwire errors\n      }\n      throw new Error(`PII detection failed: ${error instanceof Error ? error.stack : 'Unknown error'}`);\n    }\n  }\n\n  /**\n   * Notify the consumer-supplied `onDetection` callback. Never throws.\n   */\n  private async emitDetection(input: string, detectionResult: PIIDetectionResult, flagged: boolean): Promise<void> {\n    if (!this.onDetection) return;\n    try {\n      await this.onDetection({\n        detectionResult,\n        input,\n        flagged,\n        strategyApplied: flagged ? this.strategy : 'none',\n      });\n    } catch (error) {\n      console.warn('[PIIDetector] onDetection callback failed:', error);\n    }","sourceCodeStart":328,"sourceCodeEnd":364,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/processors/processors/pii-detector.ts#L328-L364","documentation":"PIIDetectorProcessor.processInput wraps all detection work in a try/catch; TripWire errors (an intentional detection trip) are re-thrown as-is, but any other failure is re-wrapped in this generic Error carrying the original stack. It signals the processor itself failed (not that PII was found).","triggerScenarios":"The underlying detection engine throws: malformed regex patterns, detector model/API errors when using model-based detection, bad options passed to the processor, or unexpected message shapes (e.g. non-string content where text is expected).","commonSituations":"Misconfigured PII entity types or regexes in processor options; network/auth failures from a detection model; upstream library version changes altering message formats; bugs in custom detection callbacks (onDetection is safe, but other hooks may not be).","solutions":["Read the embedded stack in the message to find the root cause (it is the original error.stack)","Validate processor options (patterns, entity types) at construction","Verify any detection model/API configuration (keys, endpoints) if using model-based detection","If you need to distinguish intentional detections, catch TripWire separately before this generic error"],"exampleFix":"// before\nnew PIIDetectorProcessor({ patterns: { email: '(unclosed[') });\n// after\nnew PIIDetectorProcessor({ patterns: { email: '[^@\\\\s]+@[^@\\\\s]+\\\\.[a-z]+' });","handlingStrategy":"try-catch","validationCode":"const opts = { patterns: {...} };\nfor (const [name, re] of Object.entries(opts.patterns ?? {})) {\n  new RegExp(re); // throws early on invalid patterns\n}","typeGuard":"function isTripWire(e: unknown): e is TripWire {\n  return e instanceof TripWire;\n}\nfunction isProcessorFailure(e: unknown): e is Error {\n  return e instanceof Error && e.message.startsWith('PII detection failed');\n}","tryCatchPattern":"try {\n  processed = await processor.processInput(messages);\n} catch (e) {\n  if (isTripWire(e)) throw e;\n  if (isProcessorFailure(e)) {\n    logger.error('PII processor crashed', { stack: e.message });\n    return messages; // fail-open, or rethrow for fail-closed\n  }\n  throw e;\n}","preventionTips":["Validate regex patterns and processor options at construction time","Monitor detection model health/credentials if using model-based detection","Pin @mastra/core versions and review changelogs for message format changes","Decide and document fail-open vs fail-closed policy for detection outages"],"tags":["processors","pii","error-wrapping"],"backgroundTag":"processor-detection-failed","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}