refactor: chuẩn hóa xử lý lỗi bằng cách giới thiệu tiện ích getErrorMessage và cập nhật kiểu catch thành unknown.
This commit is contained in:
67
scripts/add-error-imports.py
Normal file
67
scripts/add-error-imports.py
Normal file
@@ -0,0 +1,67 @@
|
||||
#!/usr/bin/env python3
|
||||
import os
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
# Base directory
|
||||
base_dir = Path("/Users/velikho/Desktop/WORKING/Base/services/iam-service/src")
|
||||
|
||||
# Find all files that use getErrorMessage
|
||||
for root, dirs, files in os.walk(base_dir):
|
||||
# Skip test directories
|
||||
if '__tests__' in root or 'node_modules' in root:
|
||||
continue
|
||||
|
||||
for file in files:
|
||||
if not (file.endswith('.controller.ts') or file.endswith('.middleware.ts') or file.endswith('.service.ts')):
|
||||
continue
|
||||
if file.endswith('.test.ts') or file.endswith('.e2e.ts'):
|
||||
continue
|
||||
|
||||
filepath = Path(root) / file
|
||||
|
||||
# Read file content
|
||||
with open(filepath, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
|
||||
# Check if file uses getErrorMessage
|
||||
if 'getErrorMessage(' not in content:
|
||||
continue
|
||||
|
||||
# Check if import already exists
|
||||
if re.search(r'import.*getErrorMessage.*from.*error-utils', content):
|
||||
continue
|
||||
|
||||
# Calculate relative path
|
||||
rel_path = filepath.relative_to(base_dir)
|
||||
depth = len(rel_path.parts) - 1 # -1 for the file itself
|
||||
|
||||
# Build relative import path
|
||||
if depth == 1: # src/middlewares/
|
||||
import_path = '../utils/error-utils'
|
||||
elif depth == 2: # src/modules/xxx/
|
||||
import_path = '../../utils/error-utils'
|
||||
elif depth == 3: # src/modules/xxx/yyy/
|
||||
import_path = '../../../utils/error-utils'
|
||||
else:
|
||||
import_path = '../../utils/error-utils' # default
|
||||
|
||||
# Find the last import line
|
||||
lines = content.split('\n')
|
||||
last_import_idx = -1
|
||||
for i, line in enumerate(lines):
|
||||
if line.strip().startswith('import '):
|
||||
last_import_idx = i
|
||||
|
||||
if last_import_idx >= 0:
|
||||
# Insert new import after last import
|
||||
import_statement = f"import {{ getErrorMessage }} from '{import_path}';"
|
||||
lines.insert(last_import_idx + 1, import_statement)
|
||||
|
||||
# Write back
|
||||
with open(filepath, 'w', encoding='utf-8') as f:
|
||||
f.write('\n'.join(lines))
|
||||
|
||||
print(f"Added import to {filepath}")
|
||||
|
||||
print("Done!")
|
||||
42
scripts/add-error-imports.sh
Executable file
42
scripts/add-error-imports.sh
Executable file
@@ -0,0 +1,42 @@
|
||||
#!/bin/bash
|
||||
# Script to add getErrorMessage import to files that use it
|
||||
|
||||
cd /Users/velikho/Desktop/WORKING/Base/services/iam-service/src
|
||||
|
||||
# Find all files that use getErrorMessage but don't have the import
|
||||
for file in $(find . \( -name "*.controller.ts" -o -name "*.middleware.ts" -o -name "*.service.ts" \) -not -path "*/__tests__/*" -not -name "*.test.ts" -exec grep -l "getErrorMessage" {} \;); do
|
||||
# Check if import already exists
|
||||
if ! grep -q "getErrorMessage.*from.*error-utils" "$file"; then
|
||||
# Calculate relative path to error-utils
|
||||
depth=$(echo "$file" | grep -o "/" | wc -l)
|
||||
if [ $depth -eq 2 ]; then
|
||||
# File is in src/modules/xxx/
|
||||
relative_path="../utils/error-utils"
|
||||
elif [ $depth -eq 3 ]; then
|
||||
# File is in src/modules/xxx/yyy/
|
||||
relative_path="../../utils/error-utils"
|
||||
elif [ $depth -eq 4 ]; then
|
||||
# File is in src/modules/xxx/yyy/zzz/
|
||||
relative_path="../../../utils/error-utils"
|
||||
else
|
||||
# Default to ../../utils/error-utils
|
||||
relative_path="../../utils/error-utils"
|
||||
fi
|
||||
|
||||
# Add import after the last import statement
|
||||
awk -v path="$relative_path" '
|
||||
/^import/ { last_import=NR }
|
||||
{ lines[NR]=$0 }
|
||||
END {
|
||||
for(i=1; i<=NR; i++) {
|
||||
print lines[i]
|
||||
if(i==last_import) {
|
||||
print "import { getErrorMessage } from \"" path "\";"
|
||||
}
|
||||
}
|
||||
}
|
||||
' "$file" > "$file.tmp" && mv "$file.tmp" "$file"
|
||||
|
||||
echo "Added import to $file"
|
||||
fi
|
||||
done
|
||||
Reference in New Issue
Block a user