@paulduvall/claude-dev-toolkit 0.0.1-alpha.2 → 0.0.1-alpha.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +88 -37
- package/bin/claude-commands +307 -65
- package/commands/active/xarchitecture.md +393 -0
- package/commands/active/xconfig.md +127 -0
- package/commands/active/xcontinue.md +92 -0
- package/commands/active/xdebug.md +130 -0
- package/commands/active/xdocs.md +178 -0
- package/commands/active/xexplore.md +94 -0
- package/commands/active/xgit.md +149 -0
- package/commands/active/xpipeline.md +152 -0
- package/commands/active/xquality.md +96 -0
- package/commands/active/xrefactor.md +198 -0
- package/commands/active/xrelease.md +142 -0
- package/commands/active/xsecurity.md +92 -0
- package/commands/active/xspec.md +174 -0
- package/commands/active/xtdd.md +151 -0
- package/commands/active/xtest.md +89 -0
- package/commands/active/xverify.md +80 -0
- package/commands/experiments/xact.md +742 -0
- package/commands/experiments/xanalytics.md +113 -0
- package/commands/experiments/xanalyze.md +70 -0
- package/commands/experiments/xapi.md +161 -0
- package/commands/experiments/xatomic.md +112 -0
- package/commands/experiments/xaws.md +85 -0
- package/commands/experiments/xcicd.md +337 -0
- package/commands/experiments/xcommit.md +122 -0
- package/commands/experiments/xcompliance.md +182 -0
- package/commands/experiments/xconstraints.md +89 -0
- package/commands/experiments/xcoverage.md +90 -0
- package/commands/experiments/xdb.md +102 -0
- package/commands/experiments/xdesign.md +121 -0
- package/commands/experiments/xdevcontainer.md +238 -0
- package/commands/experiments/xevaluate.md +111 -0
- package/commands/experiments/xfootnote.md +12 -0
- package/commands/experiments/xgenerate.md +117 -0
- package/commands/experiments/xgovernance.md +149 -0
- package/commands/experiments/xgreen.md +66 -0
- package/commands/experiments/xiac.md +118 -0
- package/commands/experiments/xincident.md +137 -0
- package/commands/experiments/xinfra.md +115 -0
- package/commands/experiments/xknowledge.md +115 -0
- package/commands/experiments/xmaturity.md +120 -0
- package/commands/experiments/xmetrics.md +118 -0
- package/commands/experiments/xmonitoring.md +128 -0
- package/commands/experiments/xnew.md +903 -0
- package/commands/experiments/xobservable.md +114 -0
- package/commands/experiments/xoidc.md +165 -0
- package/commands/experiments/xoptimize.md +115 -0
- package/commands/experiments/xperformance.md +112 -0
- package/commands/experiments/xplanning.md +131 -0
- package/commands/experiments/xpolicy.md +115 -0
- package/commands/experiments/xproduct.md +98 -0
- package/commands/experiments/xreadiness.md +75 -0
- package/commands/experiments/xred.md +55 -0
- package/commands/experiments/xrisk.md +128 -0
- package/commands/experiments/xrules.md +124 -0
- package/commands/experiments/xsandbox.md +120 -0
- package/commands/experiments/xscan.md +102 -0
- package/commands/experiments/xsetup.md +123 -0
- package/commands/experiments/xtemplate.md +116 -0
- package/commands/experiments/xtrace.md +212 -0
- package/commands/experiments/xux.md +171 -0
- package/commands/experiments/xvalidate.md +104 -0
- package/commands/experiments/xworkflow.md +113 -0
- package/hooks/.smellrc.example.json +19 -0
- package/hooks/README.md +263 -0
- package/hooks/check-commit-signing.py +127 -0
- package/hooks/check-complexity.py +38 -0
- package/hooks/check-security.py +37 -0
- package/hooks/claude-wrapper.sh +29 -0
- package/hooks/config.py +110 -0
- package/hooks/file-logger.sh +100 -0
- package/hooks/lib/argument-parser.sh +427 -0
- package/hooks/lib/config-constants.sh +230 -0
- package/hooks/lib/context-manager.sh +560 -0
- package/hooks/lib/error-handler.sh +423 -0
- package/hooks/lib/execution-engine.sh +444 -0
- package/hooks/lib/execution-results.sh +113 -0
- package/hooks/lib/execution-simulation.sh +114 -0
- package/hooks/lib/field-validators.sh +104 -0
- package/hooks/lib/file-utils.sh +398 -0
- package/hooks/lib/subagent-discovery.sh +468 -0
- package/hooks/lib/subagent-validator.sh +407 -0
- package/hooks/lib/validation-reporter.sh +134 -0
- package/hooks/on-error-debug.sh +226 -0
- package/hooks/pre-commit-quality.sh +204 -0
- package/hooks/pre-commit-test-runner.sh +132 -0
- package/hooks/pre-write-security.sh +115 -0
- package/hooks/prevent-credential-exposure.sh +279 -0
- package/hooks/security_bandit.py +177 -0
- package/hooks/security_checks.py +97 -0
- package/hooks/security_secrets.py +81 -0
- package/hooks/security_trojan.py +61 -0
- package/hooks/settings.example.json +52 -0
- package/hooks/smell_checks.py +238 -0
- package/hooks/smell_javascript.py +231 -0
- package/hooks/smell_python.py +110 -0
- package/hooks/smell_ruff.py +70 -0
- package/hooks/smell_types.py +72 -0
- package/hooks/subagent-trigger-simple.sh +202 -0
- package/hooks/subagent-trigger.sh +253 -0
- package/hooks/suppression.py +82 -0
- package/hooks/tab-color.sh +70 -0
- package/hooks/verify-before-edit.sh +135 -0
- package/lib/backup-restore-command.js +140 -0
- package/lib/base/base-command.js +252 -0
- package/lib/base/command-result.js +184 -0
- package/lib/config/constants.js +255 -0
- package/lib/config.js +48 -6
- package/lib/configure-command.js +428 -0
- package/lib/dependency-validator.js +64 -5
- package/lib/hook-installer-core.js +2 -2
- package/lib/installation-instruction-generator.js +213 -495
- package/lib/installer.js +134 -56
- package/lib/oidc-command.js +740 -0
- package/lib/services/backup-list-service.js +226 -0
- package/lib/services/backup-service.js +230 -0
- package/lib/services/command-installer-service.js +217 -0
- package/lib/services/logger-service.js +201 -0
- package/lib/services/package-manager-service.js +319 -0
- package/lib/services/platform-instruction-service.js +294 -0
- package/lib/services/recovery-instruction-service.js +348 -0
- package/lib/services/restore-service.js +221 -0
- package/lib/setup-command.js +359 -0
- package/lib/setup-wizard.js +155 -262
- package/lib/uninstall-command.js +100 -0
- package/lib/utils/claude-path-config.js +184 -0
- package/lib/utils/file-system-utils.js +152 -0
- package/lib/utils.js +8 -4
- package/lib/verify-command.js +430 -0
- package/package.json +7 -3
- package/scripts/postinstall.js +172 -157
- package/subagents/debug-specialist.md +7 -0
- package/templates/README.md +115 -0
- package/templates/basic-settings.json +30 -0
- package/templates/comprehensive-settings.json +57 -0
- package/templates/global-claude.md +344 -0
- package/templates/hybrid-hook-config.yaml +132 -0
- package/templates/security-focused-settings.json +62 -0
- package/templates/subagent-hooks.yaml +188 -0
- package/lib/package-manager-service.js +0 -270
- package/subagents/debug-context.md +0 -197
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
set -uo pipefail
|
|
3
|
+
|
|
4
|
+
# Error Handling Module for Subagent-Hook Integration
|
|
5
|
+
#
|
|
6
|
+
# This module provides standardized error handling, logging, and recovery
|
|
7
|
+
# mechanisms for the subagent-hook integration system.
|
|
8
|
+
|
|
9
|
+
# Include guard
|
|
10
|
+
[[ -n "${_ERROR_HANDLER_LOADED:-}" ]] && return 0
|
|
11
|
+
_ERROR_HANDLER_LOADED=1
|
|
12
|
+
|
|
13
|
+
# Source required modules
|
|
14
|
+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
15
|
+
source "$SCRIPT_DIR/config-constants.sh"
|
|
16
|
+
source "$SCRIPT_DIR/file-utils.sh"
|
|
17
|
+
|
|
18
|
+
##################################
|
|
19
|
+
# Logging Functions
|
|
20
|
+
##################################
|
|
21
|
+
|
|
22
|
+
log_message() {
|
|
23
|
+
local level="$1"
|
|
24
|
+
local message="$2"
|
|
25
|
+
local timestamp
|
|
26
|
+
|
|
27
|
+
timestamp="$(date +'%Y-%m-%d %H:%M:%S')"
|
|
28
|
+
|
|
29
|
+
# Ensure log file exists
|
|
30
|
+
ensure_log_files || return $?
|
|
31
|
+
|
|
32
|
+
# Format log entry
|
|
33
|
+
local log_entry="[$timestamp] [$HOOK_NAME] [$level] $message"
|
|
34
|
+
|
|
35
|
+
# Write to log file and display to user
|
|
36
|
+
echo "$log_entry" | tee -a "$LOG_FILE"
|
|
37
|
+
|
|
38
|
+
return $EXIT_SUCCESS
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
log_info() {
|
|
42
|
+
local message="$1"
|
|
43
|
+
log_message "INFO" "$message" >&2
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
log_warning() {
|
|
47
|
+
local message="$1"
|
|
48
|
+
log_message "WARN" "$message" >&2
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
log_error() {
|
|
52
|
+
local message="$1"
|
|
53
|
+
log_message "ERROR" "$message" >&2
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
log_critical() {
|
|
57
|
+
local message="$1"
|
|
58
|
+
log_message "CRITICAL" "$message" >&2
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
log_debug() {
|
|
62
|
+
local message="$1"
|
|
63
|
+
# Only log debug messages if debug mode is enabled
|
|
64
|
+
if [[ "${DEBUG:-false}" == "true" ]]; then
|
|
65
|
+
log_message "DEBUG" "$message"
|
|
66
|
+
fi
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
##################################
|
|
70
|
+
# Violation Logging
|
|
71
|
+
##################################
|
|
72
|
+
|
|
73
|
+
log_violation() {
|
|
74
|
+
local violation_type="$1"
|
|
75
|
+
local details="$2"
|
|
76
|
+
local timestamp
|
|
77
|
+
|
|
78
|
+
timestamp="$(date +'%Y-%m-%d %H:%M:%S')"
|
|
79
|
+
|
|
80
|
+
# Ensure violation log exists
|
|
81
|
+
ensure_log_files || return $?
|
|
82
|
+
|
|
83
|
+
local violation_entry="[$timestamp] VIOLATION: $violation_type - $details"
|
|
84
|
+
|
|
85
|
+
# Write to both violation log and main log
|
|
86
|
+
echo "$violation_entry" | tee -a "$VIOLATION_LOG" >> "$LOG_FILE"
|
|
87
|
+
|
|
88
|
+
return $EXIT_SUCCESS
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
##################################
|
|
92
|
+
# Error Handler Functions
|
|
93
|
+
##################################
|
|
94
|
+
|
|
95
|
+
handle_missing_subagent() {
|
|
96
|
+
local subagent_name="$1"
|
|
97
|
+
|
|
98
|
+
log_error "Subagent not found: $subagent_name"
|
|
99
|
+
|
|
100
|
+
# Provide helpful guidance
|
|
101
|
+
echo "Available subagents:" >&2
|
|
102
|
+
if [[ -d "$SUBAGENTS_DIR" ]]; then
|
|
103
|
+
find "$SUBAGENTS_DIR" -name "*$SUBAGENT_FILE_EXTENSION" -type f 2>/dev/null | \
|
|
104
|
+
sed "s|$SUBAGENTS_DIR/||g" | \
|
|
105
|
+
sed "s|$SUBAGENT_FILE_EXTENSION||g" | \
|
|
106
|
+
sed 's/^/ - /' >&2
|
|
107
|
+
fi
|
|
108
|
+
|
|
109
|
+
return $EXIT_SUBAGENT_NOT_FOUND
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
handle_validation_failure() {
|
|
113
|
+
local subagent_name="$1"
|
|
114
|
+
local reason="$2"
|
|
115
|
+
|
|
116
|
+
log_error "Subagent validation failed: $subagent_name - $reason"
|
|
117
|
+
|
|
118
|
+
echo "Validation failure details:" >&2
|
|
119
|
+
echo " Subagent: $subagent_name" >&2
|
|
120
|
+
echo " Reason: $reason" >&2
|
|
121
|
+
echo " Fix: Check subagent file format and required fields" >&2
|
|
122
|
+
|
|
123
|
+
return $EXIT_VALIDATION_FAILED
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
handle_execution_failure() {
|
|
127
|
+
local subagent_name="$1"
|
|
128
|
+
local error_details="$2"
|
|
129
|
+
|
|
130
|
+
log_error "Subagent execution failed: $subagent_name - $error_details"
|
|
131
|
+
|
|
132
|
+
echo "Execution failure:" >&2
|
|
133
|
+
echo " Subagent: $subagent_name" >&2
|
|
134
|
+
echo " Error: $error_details" >&2
|
|
135
|
+
|
|
136
|
+
return $EXIT_EXECUTION_FAILED
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
handle_timeout() {
|
|
140
|
+
local subagent_name="$1"
|
|
141
|
+
local timeout_seconds="$2"
|
|
142
|
+
|
|
143
|
+
log_error "Subagent execution timeout: $subagent_name after ${timeout_seconds}s"
|
|
144
|
+
|
|
145
|
+
echo "Timeout occurred:" >&2
|
|
146
|
+
echo " Subagent: $subagent_name" >&2
|
|
147
|
+
echo " Timeout: ${timeout_seconds}s" >&2
|
|
148
|
+
echo " Fix: Increase timeout or optimize subagent" >&2
|
|
149
|
+
|
|
150
|
+
return $EXIT_TIMEOUT
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
handle_security_violation() {
|
|
154
|
+
local violation_type="$1"
|
|
155
|
+
local details="$2"
|
|
156
|
+
local file_path="${3:-unknown}"
|
|
157
|
+
|
|
158
|
+
log_critical "Security violation detected: $violation_type"
|
|
159
|
+
log_violation "$violation_type" "$details (file: $file_path)"
|
|
160
|
+
|
|
161
|
+
echo "🚨 SECURITY VIOLATION DETECTED!" >&2
|
|
162
|
+
echo "Type: $violation_type" >&2
|
|
163
|
+
echo "Details: $details" >&2
|
|
164
|
+
echo "File: $file_path" >&2
|
|
165
|
+
echo "" >&2
|
|
166
|
+
echo "The operation has been BLOCKED for security reasons." >&2
|
|
167
|
+
|
|
168
|
+
# Send notification if webhook is configured
|
|
169
|
+
send_security_notification "$violation_type" "$details" "$file_path"
|
|
170
|
+
|
|
171
|
+
return $EXIT_SECURITY_VIOLATION
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
handle_filesystem_error() {
|
|
175
|
+
local operation="$1"
|
|
176
|
+
local path="$2"
|
|
177
|
+
local error_msg="$3"
|
|
178
|
+
|
|
179
|
+
log_error "Filesystem operation failed: $operation on $path - $error_msg"
|
|
180
|
+
|
|
181
|
+
echo "Filesystem error:" >&2
|
|
182
|
+
echo " Operation: $operation" >&2
|
|
183
|
+
echo " Path: $path" >&2
|
|
184
|
+
echo " Error: $error_msg" >&2
|
|
185
|
+
|
|
186
|
+
return $EXIT_GENERAL_ERROR
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
##################################
|
|
190
|
+
# Recovery Functions
|
|
191
|
+
##################################
|
|
192
|
+
|
|
193
|
+
attempt_recovery() {
|
|
194
|
+
local error_type="$1"
|
|
195
|
+
local context="$2"
|
|
196
|
+
|
|
197
|
+
log_info "Attempting recovery for error: $error_type"
|
|
198
|
+
|
|
199
|
+
case "$error_type" in
|
|
200
|
+
"missing_directories")
|
|
201
|
+
ensure_required_directories
|
|
202
|
+
return $?
|
|
203
|
+
;;
|
|
204
|
+
"corrupted_config")
|
|
205
|
+
log_warning "Config file appears corrupted, using defaults"
|
|
206
|
+
return $EXIT_SUCCESS
|
|
207
|
+
;;
|
|
208
|
+
"temp_file_cleanup")
|
|
209
|
+
cleanup_temp_files
|
|
210
|
+
return $EXIT_SUCCESS
|
|
211
|
+
;;
|
|
212
|
+
*)
|
|
213
|
+
log_warning "No recovery method available for: $error_type"
|
|
214
|
+
return $EXIT_GENERAL_ERROR
|
|
215
|
+
;;
|
|
216
|
+
esac
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
##################################
|
|
220
|
+
# Cleanup Functions
|
|
221
|
+
##################################
|
|
222
|
+
|
|
223
|
+
safe_exit() {
|
|
224
|
+
local exit_code="$1"
|
|
225
|
+
local cleanup_context="${2:-general}"
|
|
226
|
+
|
|
227
|
+
log_debug "Initiating safe exit with code: $exit_code"
|
|
228
|
+
|
|
229
|
+
# Cleanup temporary files
|
|
230
|
+
cleanup_temp_files
|
|
231
|
+
|
|
232
|
+
# Cleanup specific context files if provided
|
|
233
|
+
if [[ -n "$CONTEXT_FILE" ]] && [[ -f "$CONTEXT_FILE" ]]; then
|
|
234
|
+
cleanup_specific_temp_file "$CONTEXT_FILE"
|
|
235
|
+
fi
|
|
236
|
+
|
|
237
|
+
# Log exit
|
|
238
|
+
if [[ "$exit_code" -eq "$EXIT_SUCCESS" ]]; then
|
|
239
|
+
log_info "Operation completed successfully"
|
|
240
|
+
else
|
|
241
|
+
log_error "Operation failed with exit code: $exit_code"
|
|
242
|
+
fi
|
|
243
|
+
|
|
244
|
+
exit "$exit_code"
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
emergency_cleanup() {
|
|
248
|
+
log_critical "Emergency cleanup initiated"
|
|
249
|
+
|
|
250
|
+
# Remove all temporary files
|
|
251
|
+
cleanup_temp_files
|
|
252
|
+
|
|
253
|
+
# Kill any background processes if they exist
|
|
254
|
+
local bg_processes
|
|
255
|
+
bg_processes=$(jobs -p 2>/dev/null)
|
|
256
|
+
if [[ -n "$bg_processes" ]]; then
|
|
257
|
+
echo "$bg_processes" | xargs kill 2>/dev/null || true
|
|
258
|
+
fi
|
|
259
|
+
|
|
260
|
+
log_info "Emergency cleanup completed"
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
##################################
|
|
264
|
+
# Notification Functions
|
|
265
|
+
##################################
|
|
266
|
+
|
|
267
|
+
send_security_notification() {
|
|
268
|
+
local violation_type="$1"
|
|
269
|
+
local details="$2"
|
|
270
|
+
local file_path="$3"
|
|
271
|
+
local webhook_url="${SECURITY_WEBHOOK_URL:-}"
|
|
272
|
+
|
|
273
|
+
if [[ -z "$webhook_url" ]]; then
|
|
274
|
+
return $EXIT_SUCCESS
|
|
275
|
+
fi
|
|
276
|
+
|
|
277
|
+
local safe_type safe_path safe_details safe_user safe_ts
|
|
278
|
+
safe_type=$(json_escape "$violation_type")
|
|
279
|
+
safe_path=$(json_escape "$file_path")
|
|
280
|
+
safe_details=$(json_escape "$details")
|
|
281
|
+
safe_user=$(json_escape "$USER")
|
|
282
|
+
safe_ts=$(json_escape "$(date)")
|
|
283
|
+
|
|
284
|
+
local payload
|
|
285
|
+
payload=$(cat <<EOF
|
|
286
|
+
{
|
|
287
|
+
"text": "SECURITY ALERT: Subagent hook violation",
|
|
288
|
+
"attachments": [{
|
|
289
|
+
"color": "danger",
|
|
290
|
+
"fields": [
|
|
291
|
+
{"title": "Violation Type", "value": "$safe_type", "short": true},
|
|
292
|
+
{"title": "File", "value": "$safe_path", "short": true},
|
|
293
|
+
{"title": "Details", "value": "$safe_details", "short": false},
|
|
294
|
+
{"title": "User", "value": "$safe_user", "short": true},
|
|
295
|
+
{"title": "Timestamp", "value": "$safe_ts", "short": true}
|
|
296
|
+
]
|
|
297
|
+
}]
|
|
298
|
+
}
|
|
299
|
+
EOF
|
|
300
|
+
)
|
|
301
|
+
|
|
302
|
+
# Send notification (don't fail if notification fails)
|
|
303
|
+
if command -v curl >/dev/null 2>&1; then
|
|
304
|
+
curl -s -X POST "$webhook_url" \
|
|
305
|
+
-H "Content-Type: application/json" \
|
|
306
|
+
-d "$payload" >/dev/null 2>&1 || true
|
|
307
|
+
fi
|
|
308
|
+
|
|
309
|
+
log_debug "Security notification sent"
|
|
310
|
+
return $EXIT_SUCCESS
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
##################################
|
|
314
|
+
# Error Context Functions
|
|
315
|
+
##################################
|
|
316
|
+
|
|
317
|
+
get_error_context() {
|
|
318
|
+
local error_type="$1"
|
|
319
|
+
|
|
320
|
+
cat <<EOF
|
|
321
|
+
Error Context:
|
|
322
|
+
Hook: $HOOK_NAME
|
|
323
|
+
Version: $SUBAGENT_HOOK_VERSION
|
|
324
|
+
User: $USER
|
|
325
|
+
Working Directory: $(pwd)
|
|
326
|
+
Git Branch: $(git branch --show-current 2>/dev/null || echo 'not-in-git')
|
|
327
|
+
Timestamp: $(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
|
328
|
+
Process ID: $$
|
|
329
|
+
Error Type: $error_type
|
|
330
|
+
Environment:
|
|
331
|
+
CLAUDE_TOOL: ${CLAUDE_TOOL:-unset}
|
|
332
|
+
CLAUDE_FILE: ${CLAUDE_FILE:-unset}
|
|
333
|
+
CLAUDE_HOOK_TRIGGER: ${CLAUDE_HOOK_TRIGGER:-unset}
|
|
334
|
+
EOF
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
log_error_with_context() {
|
|
338
|
+
local error_message="$1"
|
|
339
|
+
local error_type="${2:-general}"
|
|
340
|
+
|
|
341
|
+
log_error "$error_message"
|
|
342
|
+
log_debug "$(get_error_context "$error_type")"
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
##################################
|
|
346
|
+
# Trap Handlers
|
|
347
|
+
##################################
|
|
348
|
+
|
|
349
|
+
setup_error_traps() {
|
|
350
|
+
# Set up error trapping
|
|
351
|
+
trap 'handle_script_error $LINENO $? $BASH_COMMAND' ERR
|
|
352
|
+
trap 'emergency_cleanup; safe_exit 130' INT TERM
|
|
353
|
+
trap 'cleanup_temp_files' EXIT
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
handle_script_error() {
|
|
357
|
+
local line_no="$1"
|
|
358
|
+
local exit_code="$2"
|
|
359
|
+
local failed_command="$3"
|
|
360
|
+
|
|
361
|
+
log_critical "Script error at line $line_no: $failed_command (exit: $exit_code)"
|
|
362
|
+
|
|
363
|
+
# Try to provide helpful context
|
|
364
|
+
case "$failed_command" in
|
|
365
|
+
*"mkdir"*|*"chmod"*|*"chown"*)
|
|
366
|
+
handle_filesystem_error "permission_error" "unknown" "check file/directory permissions"
|
|
367
|
+
;;
|
|
368
|
+
*"curl"*|*"wget"*)
|
|
369
|
+
log_error "Network operation failed - check connectivity"
|
|
370
|
+
;;
|
|
371
|
+
*)
|
|
372
|
+
log_error "Unknown error occurred"
|
|
373
|
+
;;
|
|
374
|
+
esac
|
|
375
|
+
|
|
376
|
+
emergency_cleanup
|
|
377
|
+
safe_exit "$exit_code"
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
##################################
|
|
381
|
+
# Validation Functions
|
|
382
|
+
##################################
|
|
383
|
+
|
|
384
|
+
validate_error_handler_dependencies() {
|
|
385
|
+
local missing_commands=()
|
|
386
|
+
|
|
387
|
+
# Check for required commands
|
|
388
|
+
local required_commands=("date" "tee" "find" "sed")
|
|
389
|
+
|
|
390
|
+
for cmd in "${required_commands[@]}"; do
|
|
391
|
+
if ! command -v "$cmd" >/dev/null 2>&1; then
|
|
392
|
+
missing_commands+=("$cmd")
|
|
393
|
+
fi
|
|
394
|
+
done
|
|
395
|
+
|
|
396
|
+
if [[ ${#missing_commands[@]} -gt 0 ]]; then
|
|
397
|
+
echo "ERROR: Missing required commands: ${missing_commands[*]}" >&2
|
|
398
|
+
return $EXIT_GENERAL_ERROR
|
|
399
|
+
fi
|
|
400
|
+
|
|
401
|
+
return $EXIT_SUCCESS
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
##################################
|
|
405
|
+
# Initialize Error Handling
|
|
406
|
+
##################################
|
|
407
|
+
|
|
408
|
+
initialize_error_handling() {
|
|
409
|
+
# Validate dependencies
|
|
410
|
+
validate_error_handler_dependencies || return $?
|
|
411
|
+
|
|
412
|
+
# Ensure required directories exist
|
|
413
|
+
ensure_required_directories || return $?
|
|
414
|
+
|
|
415
|
+
# Ensure log files exist
|
|
416
|
+
ensure_log_files || return $?
|
|
417
|
+
|
|
418
|
+
# Set up error traps
|
|
419
|
+
setup_error_traps
|
|
420
|
+
|
|
421
|
+
log_debug "Error handling system initialized"
|
|
422
|
+
return $EXIT_SUCCESS
|
|
423
|
+
}
|