Skills Error Handling
A Skill that can only run under ideal conditions is fragile.
A robust Skill needs to anticipate common errors, provide clear feedback, and recover automatically when possible.
* * *
## Three Levels of Error Handling
| Level | Location | Handling |
| --- | --- | --- |
| Input Validation Error | Before Execution | Check input, reject directly if invalid and explain why |
| Runtime Error | During Script Execution | Catch exceptions, output structured error information |
| Result Anomaly | After Execution | Verify if output meets expectations, trigger remediation if not |
* * *
## Define Error Handling Strategy in SKILL.md
SKILL.md should contain a clear error handling section that tells Claude what to do when encountering problems.
## Examples
## Error Handling
### Input Issues
- File does not exist: Inform the user the file path is incorrect, please re-upload
- File format not supported: List supported formats (pdf, docx, txt), please convert and try again
- File is empty: Inform the user the file content is empty and cannot be processed
### Execution Issues
- Script execution failed: Display the complete error message to the user, do not hide it
- Dependency not installed: Try auto-install first (pip install), if it fails inform the user to install manually
- Timeout: If not completed after 60 seconds, output current progress and ask the user whether to continue waiting
### Result Issues
- Output file is empty: Re-check the processing logic and explain possible reasons to the user
- Output format does not match expectations: Display actual output and ask the user if they are satisfied
* * *
## Standard Error Handling Patterns in Scripts
Python scripts should use try-except structure and uniformly return JSON containing status field.
## Example
# File path: scripts/error_handling_demo.py
import json
import sys
import os
class SkillError(Exception):
"""Skill custom exception base class"""
def __init__ (self, message: str,code: str):
self.message= message
self.code=code# Error code for program to determine error type
super(). __init__ (message)
class InputError(SkillError):
"""Input validation error"""
pass
class ProcessError(SkillError):
"""Processing error"""
pass
def validate_input(file_path: str) ->None:
"""Validate input, raise InputError when error occurs"""
if not file_path:
raise InputError("File path not provided","MISSING_FILE")
if not os.path.exists(file_path):
raise InputError(f"File does not existοΌ{file_path}","FILE_NOT_FOUND")
if os.path.getsize(file_path)==0:
raise InputError(f"File is emptyοΌ{file_path}","EMPTY_FILE")
def process(file_path: str) ->dict:
"""Main processing logic"""
try:
validate_input(file_path)
with open(file_path,"r", encoding="utf-8")as f:
content = f.read()
# Actual processing logic (simplified to line count here)
lines = content.split("
")
return{
"status": "success",
"data": {
"file": file_path,
"lines": len(lines),
"chars": len(content)
}
}
except InputError as e:
# Input error: user can retry after correction
return{
"st
```
YouTip