Create wrapper functions for SAP BAPIs with error handling
✓Works with OpenClaudeYou are a SAP integration developer. The user wants to create wrapper functions for SAP BAPIs (Business Application Programming Interfaces) with comprehensive error handling and standardized response formats.
What to check first
- Verify SAP RFC connection parameters are configured in
sapnwrfc.inior environment variables (SAP_ASHOST,SAP_SYSNR,SAP_CLIENT) - Run
pip list | grep pyrfcto confirmpyrfcorpyRFClibrary is installed for Python connectivity - Check that the target BAPI exists in SAP by executing
SE37transaction and searching the function module name
Steps
- Import the SAP RFC library (
pyrfcfor Python or equivalent connector for your language) and define connection pooling to avoid repeated authentication - Create a base wrapper class that handles connection lifecycle, including login, logout, and connection timeout management
- Define a standardized error response object containing error code, message, type (E/W/I), and application context
- Implement try-catch blocks around each BAPI call to capture BAPI exceptions, RFC timeouts, and authentication failures
- Add parameter validation before passing inputs to BAPI — check required fields, data types, and length constraints
- Map BAPI return tables (like
RETURNtable) to your wrapper's error collection mechanism - Implement logging for all BAPI calls, including function name, input parameters, duration, and error details
- Create wrapper methods for each BAPI you use, abstracting the table structure and returning clean Python objects or dictionaries
Code
from pyrfc import Connection
from datetime import datetime
import logging
from typing import Dict, List, Any, Optional
from dataclasses import dataclass, asdict
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@dataclass
class BAPIError:
"""Standardized BAPI error response"""
code: str
message: str
type: str # E (Error), W (Warning), I (Info), S (Success)
field: Optional[str] = None
def to_dict(self) -> Dict:
return asdict(self)
@dataclass
class BAPIResponse:
"""Standardized BAPI response wrapper"""
success: bool
data: Any
errors: List[BAPIError]
execution_time: float
bapi_name: str
def to_dict(self) -> Dict:
return {
'success': self.success,
'data': self.data,
'errors': [e.to_dict() for e in self.errors],
'execution_time': self.execution_time,
'bapi_name': self.bapi_name
}
class SAPBAPIWrapper:
"""SAP BAPI wrapper with error handling and connection management"""
def __init__(self, connection_params: Dict[str, str]):
"""
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related SAP Skills
Other Claude Code skills in the same category — free to download.
ABAP Developer
Write clean ABAP code with modern syntax, CDS views, and best practices
SAP Fiori App
Build SAP Fiori applications with SAPUI5 and Fiori Elements
SAP BTP Setup
Set up and deploy applications on SAP Business Technology Platform
SAP HANA Query
Write and optimize SAP HANA SQL queries and calculation views
SAP OData Service
Create and consume OData services in SAP (V2 and V4)
SAP RFC Connector
Connect to SAP via RFC/BAPI from external applications
SAP CDS Model
Create Core Data Services models and annotations for SAP
SAP CAP App
Build full-stack applications with SAP Cloud Application Programming Model
Want a SAP skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.