Error Handling
October 20, 2023 #Error Handling, #SAP SDK, #Development 6 minute read

Error Handling Best Practices in SAP B1 Add-On Development

Implementing robust error handling is crucial for developing reliable SAP Business One add-ons. This guide covers comprehensive error handling strategies and best practices.

1. DI API Error Handling

When working with the DI API, proper error handling is essential:


public class DIAPIErrorHandler
{
    private SAPbobsCOM.Company _company;

    public void HandleDIAPIError()
    {
        int errCode = 0;
        string errMsg = "";
        
        _company.GetLastError(out errCode, out errMsg);
        
        // Log the error
        Logger.LogError($"DI API Error: {errCode} - {errMsg}");
        
        // Handle specific error codes
        switch (errCode)
        {
            case -10:
                throw new InvalidLicenseException(errMsg);
            case -1011:
                throw new DatabaseConnectionException(errMsg);
            default:
                throw new DIAPIException($"Error {errCode}: {errMsg}");
        }
    }
}

2. UI API Event Error Handling

Handle UI API events gracefully to prevent application crashes:


public class UIEventHandler
{
    private SAPbouiCOM.Application _application;

    public void HandleItemEvent(string formUID, ref SAPbouiCOM.ItemEvent pVal, out bool bubbleEvent)
    {
        bubbleEvent = true;
        try
        {
            if (pVal.EventType == SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED)
            {
                // Handle button click
                ProcessButtonClick(formUID, pVal.ItemUID);
            }
        }
        catch (Exception ex)
        {
            _application.MessageBox($"Error processing event: {ex.Message}");
            Logger.LogError($"UI Event Error: {ex}");
        }
    }
}

3. Transaction Error Handling

Implement proper transaction management with error handling:


public class TransactionManager
{
    private readonly SAPbobsCOM.Company _company;

    public void ExecuteInTransaction(Action action)
    {
        try
        {
            if (_company.InTransaction)
            {
                throw new InvalidOperationException("Transaction already in progress");
            }

            _company.StartTransaction();
            
            action();
            
            _company.EndTransaction(SAPbobsCOM.BoWfTransOpt.wf_Commit);
        }
        catch (Exception ex)
        {
            if (_company.InTransaction)
            {
                _company.EndTransaction(SAPbobsCOM.BoWfTransOpt.wf_RollBack);
            }
            
            Logger.LogError($"Transaction Error: {ex}");
            throw;
        }
    }
}

4. Logging Best Practices

Implement comprehensive logging for better error tracking:

  • Log all exceptions with full stack traces
  • Include contextual information in logs
  • Implement different log levels (Debug, Info, Error)
  • Use structured logging for better analysis

public class Logger
{
    public static void LogError(Exception ex, string context = "")
    {
        var logEntry = new
        {
            Timestamp = DateTime.UtcNow,
            Context = context,
            Message = ex.Message,
            StackTrace = ex.StackTrace,
            InnerException = ex.InnerException?.Message
        };

        // Log to file/database
        WriteLog(JsonSerializer.Serialize(logEntry));
    }
}

5. User Notification Strategies

Implement user-friendly error notifications:

  • Show clear, actionable error messages
  • Hide technical details from end users
  • Provide guidance for error resolution
  • Support multiple languages for error messages

Thanks,

Ahmed Aboalia