How do you receive the last error message from the ProvideX Session object?

I am trying to write good error handling for the connection to Sage 100 in VB.Net. I have the following code:

Dim pvx = New COMDispatch("ProvideX.Script")

Try
    pvx.InvokeMethod("Init", Path)
Catch ex As Exception
    Dim lastErrorMsg = pvx.InvokeMethod("LastErrorMsg")
    console.writeline(lastErrorMsg)
End Try

Wanting to be able to catch issues such as invalid paths and network problems. What is the right way to catch / report those kinds of errors?

  • 0

    I don't know how far you want to go with logging errors, but:

    1. The below function takes some scripting objects as arguments, then some defined types or data structures, when a call fails based on the boi_retval, I call my boi_trap_column err function, which I pass boi_ciItem.slasterrormsg

    2.  boi_trap_column_error function is passed the ciItem.slasterrormsg, col_name, "context_tag"

    Then calls boi_ci_item_log

    I can't remember how many times I've used the log to find errors.

    The more you write the interface, the more you'll re-write it to get log files to troubleshoot

    '*************************************************************************************************
    'Function boi_ci_item_add
    'Args: 3
    'mas_company_record struct (defined in header_mas)
    'mas_user_record (defined in header_mas)
    'mas_ci_item_record (defined in header_mas)

    'Returns: Boolean
    'True (write successful)
    'False (Write Failed)

    'Revised: 11/15/2013
    'Author : B Stern
    '*************************************************************************************************
    Function boi_ci_item_add(boi_script As Object, boi_session As Object, boi_ciItem As Variant, mcr As mas_company_record, ci_item As mas_ci_item_record) As Boolean
      Dim fstatus As Boolean       'T/F function boolean
      Dim boi_retval As Long   'boi call return value
      Dim col_name As String

        col_name = "ItemCode$"
        boi_retval = boi_ciItem.nSetKeyValue(col_name, ci_item.ItemCode)
        If (boi_retval < 1) Then
          boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If

        boi_retval = boi_ciItem.nSetKey()
        'boi_retval
        'returns 1 if item exists
        'returns 2 if item does not exist
        If (boi_retval < 1) Then
          boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If
       
       
        col_name = "ItemCodeDesc$"
        boi_retval = boi_ciItem.nSetValue(col_name, ci_item.ItemCodeDesc)
        If (boi_retval < 1) Then
          boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If
       
       
        col_name = "ItemType$"
        boi_retval = boi_ciItem.nSetValue(col_name, "1")
        If (boi_retval < 1) Then
          boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If
     
       
        'variable item value assignments
       
        'item pricing assignments
        'DO NOT CHANGE THE ORDER OF PRICING ASSIGNMENTS
        'OTHERWISE MAS WILL PRODUCE ERRORS
        col_name = "StandardUnitPrice"
        boi_retval = boi_ciItem.nSetValue(col_name, ci_item.StandardUnitPrice)
        If (boi_retval < 1) Then
          boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If

        col_name = "SuggestedRetailPrice"
        boi_retval = boi_ciItem.nSetValue(col_name, ci_item.SuggestedRetailPrice)
        If (boi_retval < 1) Then
          boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If
       
        col_name = "StandardUnitCost"
        boi_retval = boi_ciItem.nSetValue(col_name, ci_item.StandardUnitCost)
        If (boi_retval < 1) Then
          boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If
       
       
        'unit of measure
        boi_retval = boi_ciItem.nSetValue("SalesUnitOfMeasure$", ci_item.SalesUnitOfMeasure)
        boi_retval = boi_ciItem.nSetValue("PurchaseUnitOfMeasure$", ci_item.SalesUnitOfMeasure)
        boi_retval = boi_ciItem.nSetValue("StandardUnitOfMeasure$", ci_item.SalesUnitOfMeasure)
       
       
        col_name = "ProductLine$"
        boi_retval = boi_ciItem.nSetValue(col_name, ci_item.ProductLine)
        If (boi_retval < 1) Then
          boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If

       
        'item vendor assignments only after initial item added to ci_item table
        If (ci_item.UpdateState = 3) Then
            col_name = "PrimaryVendorNo$"
            boi_retval = boi_ciItem.nSetValue(col_name, ci_item.PrimaryVendorNo)
           
            If (boi_retval < 1) Then
              boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
            End If
        End If
           
       
        'primary AP Division NO
        col_name = "PrimaryAPDivisionNo$"
        boi_retval = boi_ciItem.nSetValue(col_name, "00")
        If (boi_retval < 1) Then
            boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If


        'inactive item
        col_name = "InactiveItem$"
        boi_retval = boi_ciItem.nSetValue(col_name, ci_item.InactiveItem)
        If (boi_retval < 1) Then
            boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If
        
       
       
        'user defined field assignments
        col_name = "UDF_FULL_ITEM_DESC$"
        boi_retval = boi_ciItem.nSetValue(col_name, ci_item.udf_full_item_description)
        If (boi_retval < 1) Then
          boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If
       
        col_name = "UDF_BOX_SF"
        boi_retval = boi_ciItem.nSetValue(col_name, ci_item.udf_box_sf)
        If (boi_retval < 1) Then
          boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If
       
        col_name = "UDF_BOX_COUNT"
        boi_retval = boi_ciItem.nSetValue(col_name, ci_item.udf_box_count)
        If (boi_retval < 1) Then
          boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If
       
        col_name = "UDF_ITEM_DIMENSIONS$"
        boi_retval = boi_ciItem.nSetValue(col_name, ci_item.udf_item_dimensions)
        If (boi_retval < 1) Then
          boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If
       
        col_name = "UDF_EDIT_FLAG$"
        boi_retval = boi_ciItem.nSetValue(col_name, ci_item.udf_edit_flag)
        If (boi_retval < 1) Then
          boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If

       
        col_name = "UDF_EDB_MANAGED$"
        boi_retval = boi_ciItem.nSetValue(col_name, ci_item.udf_edb_managed)
        If (boi_retval < 1) Then
          boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If

        col_name = "UDF_PARTIAL_BOX_OK$"
        boi_retval = boi_ciItem.nSetValue(col_name, ci_item.udf_partial_box_ok)
        If (boi_retval < 1) Then
          boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If

        col_name = "UDF_WARN_ON_MARGIN$"
        boi_retval = boi_ciItem.nSetValue(col_name, ci_item.udf_warn_on_margin)
        If (boi_retval < 1) Then
          boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If

        col_name = "UDF_DELETE_FLAG$"
        boi_retval = boi_ciItem.nSetValue(col_name, ci_item.udf_delete_flag)
        If (boi_retval < 1) Then
          boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If
       
        col_name = "UDF_MAT_CATEGORY$"
        boi_retval = boi_ciItem.nSetValue(col_name, ci_item.udf_mat_category)
        If (boi_retval < 1) Then
          boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If

        col_name = "UDF_MAT_CONFIG$"
        boi_retval = boi_ciItem.nSetValue(col_name, ci_item.udf_mat_config)
        If (boi_retval < 1) Then
          boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If


        col_name = "UDF_MAT_TYPE$"
        boi_retval = boi_ciItem.nSetValue(col_name, ci_item.udf_mat_type)
        If (boi_retval < 1) Then
          boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If
       
        col_name = "UDF_MAT_FINISH$"
        boi_retval = boi_ciItem.nSetValue(col_name, ci_item.udf_mat_finish)
        If (boi_retval < 1) Then
          boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If

        col_name = "UDF_MAT_FORMAT$"
        boi_retval = boi_ciItem.nSetValue(col_name, ci_item.udf_mat_format)
        If (boi_retval < 1) Then
          boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If

        col_name = "UDF_VENDOR_COLLECTION$"
        boi_retval = boi_ciItem.nSetValue(col_name, ci_item.udf_vendor_collection)
        If (boi_retval < 1) Then
          boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If

        col_name = "UDF_PREV_ITEM_CODE$"
        boi_retval = boi_ciItem.nSetValue(col_name, ci_item.udf_prev_item_code)
        If (boi_retval < 1) Then
          boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
        End If

       
    '    col_name = "UDF_ITEM_COST_MULTIPLIER"
    '    boi_retval = boi_ciItem.nSetValue(col_name, ci_item.udf_item_cost_multiplier)
    '    If (boi_retval < 1) Then
    '      boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
    '    End If

    '    col_name = "UDF_SHOWROOM_AVAILABILITY$"
    '    boi_retval = boi_ciItem.nSetValue(col_name, ci_item.udf_showroom_availability)
    '    If (boi_retval < 1) Then
    '      boi_trap_column_error ci_item, boi_ciItem.slasterrormsg, col_name, "boi_add_update_ci_item"
    '    End If

       
       
       
       
        'item default assignments
        boi_retval = boi_ciItem.nSetValue("Valuation$", "3")
        boi_retval = boi_ciItem.nSetValue("DropShip$", "N")
        boi_retval = boi_ciItem.nSetValue("AllowBackOrders$", "Y")
        boi_retval = boi_ciItem.nSetValue("AllowTradeDiscount$", "Y")
        boi_retval = boi_ciItem.nSetValue("PrintReceiptLabels$", "N")
        boi_retval = boi_ciItem.nSetValue("ConfirmCostIncrInRcptOfGoods$", "Y")
        boi_retval = boi_ciItem.nSetValue("SaleMethod$", "N")
        boi_retval = boi_ciItem.nSetValue("ExplodeKitItems$", "P")
        boi_retval = boi_ciItem.nSetValue("ProcurementType$", "B")
        boi_retval = boi_ciItem.nSetValue("PlannedByMRP$", "N")
        boi_retval = boi_ciItem.nSetValue("SetupCharge$", "N")
        boi_retval = boi_ciItem.nSetValue("TaxClass$", "TX")
        boi_retval = boi_ciItem.nSetValue("PurchaseTaxClass$", "NT")

        boi_retval = boi_ciItem.nWrite()
        'MsgBox boi_ciItem.sLastErrorMsg
       
      If (boi_retval = 1) Then
          fstatus = True
        Else
          fstatus = False
      End If


      boi_ci_item_add = fstatus

    End Function

    'this is here to add some ledgible descriptions and resolutions to
    'errors reported by boi com object
    'Incomplete for purposes of the example code

    Sub boi_trap_column_error(ci_item As mas_ci_item_record, boi_last_error As String, column_name As String, function_context As String)

      If (Len(boi_last_error) > 0) Then
          boi_ci_item_log ci_item, boi_last_error, column_name, function_context
      End If
     
    End Sub

    Sub boi_ci_item_log(ci_item As mas_ci_item_record, boi_last_error As String, column_name As String, function_context As String)
       
      Dim ls As Boolean   'log status
      Dim s As String
     
      s = vbNewLine & "Item Code: " & ci_item.ItemCode & "    " & _
      "Context: " & function_context & "     " & "Column:" & column_name & "     " & boi_last_error & vbNewLine
     
      ls = boi_LogWrite(s, ci_item_log)

    End Sub

    Sample log output (no ci_item log errors shown, but other non-fatal messages are shown

    Session Message: Context: Set Company    Last Message: Module W/O is not on file.

    Session Message: Context: Opening IM_ProductLine_bus    Last Message: Module I/T is not on file.

    Session Message: Context: Set Company    Last Message: Module W/O is not on file.

    Session Message: Context: Opening IM_ProductLine_bus    Last Message: Module I/T is not on file.