home *** CD-ROM | disk | FTP | other *** search
/ The Developer Connection…ice Driver Kit for OS/2 3 / DEV3-D1.ISO / devtools / dataflex / error.pkg < prev    next >
Encoding:
Text File  |  1993-05-19  |  12.1 KB  |  371 lines

  1. //            Error Class and object
  2. //
  3. // This package describes the Error_Type class and an instance 
  4. // Error_Info_Object.  The error class allows for a interactive interface
  5. // for all errors generated in a program.  The object displays important
  6. // information about the error, and allows the user to get help about the
  7. // error condition.
  8. //
  9. // The programmer may define the error numbers that will be trapped by
  10. // the error object through the use of the Trap_Error, Ignore_Error,
  11. // Trap_All and Ignore_All messages.
  12. //
  13. // The Help buttons has been defined to generate a special help name which
  14. // will allow the user to get help on the specific error condition.  The
  15. // help name is made up of the constant ERROR: and the error number.  The
  16. // help data files should contain help subjects for any system errors or
  17. // program defined conditions.
  18. //
  19.  
  20.  
  21. #CHKSUB 1 1 // Verify the UI subsystem.
  22.  
  23. use ui
  24.  
  25.  
  26. // Include or define all useful symbols.
  27. #INCLUDE ERRORNUM.INC
  28. #REPLACE MAX_ERROR_NUMBER 32766
  29.  
  30.  
  31. class Error_button is a Button
  32.  
  33.     // This function provides a specific help name that will be searched
  34.     // for by the help system.  Note that the APPLICATION_NAME and the
  35.     // MODULE_NAME are not set, which allows for module specific help
  36.     // messages.  If there is not a help record found, the parent of the
  37.     // button will be asked.  The parent supplies a more general help name.
  38.  
  39.    function Help_Name returns string
  40.       function_return ( append("ERROR:",Lasterr))
  41.    end_function
  42.  
  43. End_class
  44.  
  45.  
  46. // This array stores the set of trapped errors as toggled ranges starting
  47. // with the errors that are trapped. The array should always contain 0 and 
  48. // MAX_ERROR_NUMBER + 1, which are the limits. If an array contained the 
  49. // following items...
  50. //
  51. // { 0, 5, 10, MAX_ERROR_NUMBER + 1 }.
  52. //
  53. // This would mean that errors 1 - 4 are trapped, 5 - 9 are ignored, and
  54. // 10 through the rest are trapped.
  55. //
  56. class Trapped_Errors_Array is an array
  57.  
  58.     // Find largest error LE targetError. Assumes array is sorted.
  59.     function findErrorLE integer targetError returns integer
  60.  
  61.         integer lowIndex hiIndex midIndex currError 
  62.  
  63.         // If error is outside of boudary conditions, use 
  64.         // value of closest valid error# instead.
  65.         if ( targetError le 0 );
  66.             move 1 to targetError
  67.         else if ( targetError ge MAX_ERROR_NUMBER ) ;
  68.             move ( MAX_ERROR_NUMBER - 1 ) to targetError    
  69.  
  70.         move 0                                    to lowIndex
  71.         move ( item_count( current_object ) - 1 ) to hiIndex
  72.  
  73.         // midIndex will contain the closest error LE to target upon exit.
  74.         repeat
  75.  
  76.             move ( ( lowIndex + hiIndex ) / 2 )                 to midIndex
  77.             move ( integer_value( current_object, midIndex ) )  to currError
  78.  
  79.             // midIndex is targetIndex if a match occurs
  80.             if currError eq targetError ;
  81.                 goto ERROR_FOUND
  82.  
  83.             // We are either on it or just below it.
  84.             if ( lowIndex eq midIndex ) begin
  85.  
  86.                 if ( integer_value( current_object, hiIndex ) le targetError ) ;
  87.                     move hiIndex to midIndex
  88.  
  89.                 goto ERROR_FOUND
  90.             
  91.             end
  92.  
  93.             // No match, so move the boundaries.
  94.             if currError gt targetError ;
  95.                 move ( midIndex - 1 ) to hiIndex
  96.             else ;
  97.                 move midIndex to lowIndex
  98.  
  99.         until lowIndex gt hiIndex
  100.  
  101. ERROR_FOUND:
  102.  
  103.         function_return midIndex
  104.  
  105.     end_function
  106.  
  107.  
  108.     // Boundaries of the table are assumed to hold error limits.
  109.     procedure initArray
  110.         send delete_data
  111.         set array_value item 0 to 0
  112.         set array_value item 1 to ( MAX_ERROR_NUMBER + 1 )
  113.     end_procedure
  114.  
  115.  
  116.     // Return 1 if Error is trapped, 0 otherwise.
  117.     function isTrapped integer Error# returns integer
  118.         function_return ( NOT ( mod( findErrorLE( current_object, Error# ), 2 ) ) )
  119.     end_function
  120.  
  121.  
  122.     // Add the error as long as it doesn't violate boundary conditions.
  123.     // This routine leaves the array unsorted.
  124.     procedure addError integer Error#
  125.         if ( ( Error# lt MAX_ERROR_NUMBER ) AND ( Error# gt 0 ) ) ;
  126.             set array_value item ( item_count( current_object ) ) to ( INTEGER( Error# ) )
  127.     end_procedure
  128.  
  129.  
  130.     // Set error to flagged state.
  131.     procedure handleError integer Error# integer trapFlag
  132.         local integer prevErrIndex prevErrFlag prevErrValue nextErrValue
  133.         
  134.         if ( ( Error# gt MAX_ERROR_NUMBER ) OR ( Error# lt 0 ) ) begin
  135.             error DFERR_ERROR_NUMBER_OUT_OF_RANGE 
  136.             procedure_return
  137.         end
  138.  
  139.         get findErrorLE to Error# prevErrIndex
  140.         get isTrapped   to Error# prevErrFlag
  141.  
  142.         // Error already handled in some range.
  143.         if ( prevErrFlag eq trapFlag ) ;
  144.             goto EXIT_HANDLE_ERROR
  145.  
  146.         // This is kind of complicated. If we are adding an error,
  147.         // we have to account for the error already being in the 
  148.         // array as well as rejoining ranges that have been previously
  149.         // split and splitting ranges when adding a new flag.
  150.  
  151.         get integer_value item ( prevErrIndex + 1 ) to nextErrValue
  152.         get integer_value item prevErrIndex         to prevErrValue
  153.  
  154.         // Do this first so prevErrIndex stays valid.
  155.         if nextErrValue eq ( Error# + 1 ) ;
  156.             send delete_item ( prevErrIndex + 1 )
  157.         else ;
  158.             send addError ( Error# + 1 )
  159.  
  160.         if ( prevErrValue lt Error# ) ;
  161.             send addError Error#
  162.         else ;
  163.             send delete_item prevErrIndex
  164.  
  165. EXIT_HANDLE_ERROR:
  166.         
  167.         send sort_items UPWARD_DIRECTION
  168.                                     
  169.     end_procedure
  170.  
  171.  
  172.         //*** Flag error as trappable
  173.         procedure Trap_Error integer Error#
  174.             send handleError Error# 1
  175.         end_procedure
  176.  
  177.         //*** Flag error as non-trappable
  178.         procedure Ignore_Error integer Error#
  179.             send handleError Error# 0
  180.         end_procedure
  181.  
  182.         //*** Flag all errors as trappable
  183.         procedure Trap_All
  184.             send initArray
  185.         end_procedure
  186.  
  187.         //*** Flag all errors as non-trappable
  188.         procedure Ignore_All
  189.             send delete_data
  190.             set array_value item 0 to 0
  191.             set array_value item 1 to 1
  192.             set array_value item 2 to ( MAX_ERROR_NUMBER + 1 )
  193.         end_procedure
  194.  
  195. end_class
  196.  
  197.  
  198. class Error_Type_c is an Error
  199.  
  200.     procedure construct_object integer Client_Img# integer Button_Img#
  201.         forward send construct_object Client_Img#
  202.  
  203.         property integer Verbose_State  public  1
  204.  
  205.         set client_area_state to TRUE
  206.         set ring_state to TRUE
  207.         set block_mouse_state to TRUE
  208.         set scope_state to TRUE
  209.  
  210.         object trappedErrors is a Trapped_Errors_Array
  211.             send initArray
  212.         end_object
  213.  
  214.         set value item 0 to "Error Message"
  215.         set center_state item 1 to TRUE
  216.  
  217.         send Trap_All
  218.         move current_object to Error_Object_Id
  219.  
  220.         object Error_Display is an edit
  221.             set object_color to ;
  222.             (hi(object_color(parent(current_object)))) ;
  223.             (low(object_color(parent(current_object))))
  224.             set focus_mode to pointer_only
  225.             set location to 5 3 relative
  226.             set size to 3 53
  227.         end_object
  228.  
  229.         object Action_Buttons is a Error_button Button_Img#
  230.             set object_color to 0 0
  231.             on_key KEY_F1 send Help
  232.             item_list
  233.                 on_item "<OK>"    send Request_Cancel    
  234.                 on_item "<HELP>"  send Help
  235.             end_item_list
  236.         end_object
  237.  
  238.    end_procedure
  239.  
  240.     //*** Catch and display error Error#.
  241.     procedure Trap_Error integer Error#
  242.         send Trap_Error to ( trappedErrors( current_object ) ) Error#
  243.     end_procedure
  244.  
  245.     //*** Pass error Error# on to the regular DataFlex error handler.
  246.     procedure Ignore_Error integer Error#
  247.         send Ignore_Error to ( trappedErrors( current_object ) ) Error#
  248.     end_procedure
  249.  
  250.     //*** Catch and display all errors.
  251.     procedure Trap_All
  252.         send Trap_All to ( trappedErrors( current_object ) ) 
  253.     end_procedure
  254.  
  255.     //*** Forward all error to regular DataFlex error handler.
  256.     procedure Ignore_All
  257.         send Ignore_All to ( trappedErrors( current_object ) ) 
  258.     end_procedure
  259.  
  260.     //*** Kill 1 UI level, deactivate error object, and return to prior scope.
  261.    procedure Request_Cancel
  262.       set client_area_state to false
  263.       send deactivate
  264.       set client_area_state to true
  265.       send stop_ui
  266.       indicate err true
  267.    end_procedure
  268.  
  269.    //*** Build complete error description from Flexerrs and user error message.
  270.    function Error_Description integer Error# string ErrMsg returns string
  271.       local string Full_Error_Text 
  272.  
  273.       trim ErrMsg to ErrMsg
  274.       move (trim(error_text(DESKTOP,Error#))) to Full_Error_Text
  275.  
  276.       if ErrMsg ne "" begin
  277.  
  278.         if ( ( Full_Error_Text ne "" ) AND ;
  279.                 error_text_available( DESKTOP, Error# ) ) ;
  280.             append Full_Error_Text " " ErrMsg
  281.         else ;
  282.             move ErrMsg to Full_Error_Text
  283.  
  284.       end
  285.  
  286.       function_return Full_Error_Text
  287.    end_function
  288.  
  289.    //** return true if an error number is critical
  290.    function Is_Critical integer Error# returns integer
  291.       function_return (".3.10.18.19.20.21.22.43.70.72.74.75.78.80.97.";
  292.             contains ("."+string(Error#)+".")) 
  293.    end_function
  294.  
  295.     //*** Handle error event, displaying error info to user.
  296.    procedure Error_Report integer Error_Info string ErrMsg
  297.       local integer Text_Obj# Error# Line# 
  298.       local string Temp
  299.  
  300.       move (hi(Error_Info))  to Error#
  301.       if NOT ( isTrapped( TrappedErrors( current_object ), Error# ) ) begin
  302.          forward send Error_Report Error_Info ErrMsg
  303.          procedure_return
  304.       end
  305.  
  306.       move (low(Error_Info)) to Line#
  307.       move (Error_Display(current_object)) to Text_Obj# 
  308.       if (Verbose_State(current_object)) ;
  309.           append Temp "Status << "  Error#   " >> on line #" Line#
  310.       else move '' to temp
  311.  
  312.       send delete_data to Text_Obj#
  313.       send beginning_of_data to Text_Obj#
  314.  
  315.       set value item 1 to Temp
  316.       send insert to Text_Obj# ; 
  317.                           (Error_Description(current_object, Error#, ErrMsg))
  318.  
  319.       if (active_state(current_object)) ne 1 start_ui current_object
  320.  
  321.       // abort on critical errors
  322.       if (Is_Critical(current_object,Error#)) abort
  323.  
  324.    end_procedure
  325.  
  326.     // The functions below are used to construct a general help
  327.     // name for errors that are generated by the system.  If processing
  328.     // comes here, then there was no module specific help found.  These
  329.     // functions will provide a more general help name that appears in
  330.     // the form of SYSTEM..ERROR:#.  All global errors should be
  331.     // places in the help file under this application and module name.
  332.  
  333.     //*** Returns "ERROR:errornum" to supply error help.
  334.    function Help_Name returns string
  335.       function_return (append("ERROR:",lastErr))
  336.    end_function
  337.  
  338.    function Application_Name returns string
  339.       function_return 'SYSTEM'
  340.    end_function
  341.  
  342.    function Module_Name returns string
  343.       function_return ''
  344.    end_function
  345.  
  346. end_class
  347.  
  348.  
  349.  
  350. /Error_Info_Img
  351. ╔═[ _____________ ]════════════════════════════════════════╗
  352. ║                                                          ║
  353. ║ ________________________________________________________ ║
  354. ║                                                          ║
  355. ║ ┌──────────────────────────────────────────────────────┐ ║
  356. ║ │                                                      │ ║
  357. ║ │                                                      │ ║
  358. ║ │                                                      │ ║
  359. ║ └──────────────────────────────────────────────────────┘ ║
  360. ║                  ____             ______                 ║
  361. ╚══════════════════════════════════════════════════════════╝
  362. /*
  363.  
  364. sub_page Error_Info_Buttons from Error_Info_Img 3 4
  365.  
  366. object Error_Info_Object is a Error_Type_c Error_Info_Img Error_Info_Buttons
  367.        set location to 6 10 absolute
  368. end_object
  369.  
  370.  
  371.