This document covers error handling flows, authentication patterns, and authorization mechanisms in the VTEX Rewriter application.
📋 Table of Contents
🚨 Error Handling Architecture
🔐 Authentication & Authorization
VTEX-Specific Error Handling
- Custom error classes in
node/errors/:ExpiredUserError,InvalidAdminUserError,UnauthorizedError - Authentication redirects use
__pickRuntimequery parameter detection - Error middleware handles VTEX admin login flows automatically
Authorization Flow
The authorization middleware validates user permissions and handles various authentication scenarios specific to the VTEX platform.
⚠️ Error Types & Responses
Custom Error Classes
ExpiredUserError: Thrown when user session has expired
- Response: 401 Unauthorized with login redirect
- Usage: Session timeout scenarios
InvalidAdminUserError: Thrown when user lacks admin privileges
- Response: 403 Forbidden
- Usage: Administrative operations
UnauthorizedError: Thrown for general authentication failures
- Response: 401 Unauthorized
- Usage: Missing or invalid authentication
🔍 Loop Detection
Loop Detection for Redirects
_34class LoopDetector {_34 private visited = new Set<string>()_34 private path: string[] = []_34 _34 async checkForLoop(redirect: Redirect): Promise<string[]> {_34 this.visited.clear()_34 this.path = []_34 _34 return this.traverse(redirect)_34 }_34 _34 private async traverse(redirect: Redirect | null): Promise<string[]> {_34 if (!redirect) return []_34 _34 const key = `$\{redirect.binding\}:$\{redirect.from\}`_34 _34 // Loop detected_34 if (this.visited.has(key)) {_34 const loopStart = this.path.indexOf(key)_34 return this.path.slice(loopStart).concat(key)_34 }_34 _34 this.visited.add(key)_34 this.path.push(key)_34 _34 // Search next redirect in chain_34 const next = await this.findRedirect({_34 from: redirect.to,_34 binding: redirect.binding_34 })_34 _34 return this.traverse(next)_34 }_34}