A Simple File Error Template

by Don Reynolds

Published 1997-07-01    Printer-friendly version

Download the code here

Errorcodes

If you know all about errorcodes, skip this part.

Each time we access a file, a return value is generated which can be queried with the command errorcode(). If errorcode() is zero or false, then the file action was successful. If errorcode() is true then the number returned with errorcode() and the description returned with error() tell us what went wrong. These values vary according to the file access command being used. The Language Reference Manual contains a list of errorcodes for each command.

Levels of concern

In my programming experiences I've found differing levels of concern about whether a file error occurs. These have included the following:

  • I don't care whether an error occurred. Processing should continue.
SET(Any:ByAnyId,Any:ByAnyId)
LOOP
  NEXT(AnyFile)
  IF ERRORCODE() THEN BREAK.
  !
  ! multiple lines of wonderful code go here
  !
END !LOOP
  • When an error does occur, I always want to know some basic information. This includes the file name, the error number, and the error description. (Saves going to the manual to find out what error 37 is...let's see, where did I put that manual, anyway!) I also want to find out the same information each time.
GET(AnyFile,Any:ByAnyId)
IF ERRORCODE()
  MESSAGE('File processing error:' |
  & '<10,10>' |
  & 'File: ' & ERRORFILE() & '<10>' |
  & 'Errorcode: ' & ERRORCODE() & '<10>' |
  & 'Description: ' & ERROR(), |
  'Data File Problem', |
  ICON:EXCLAMATION,BUTTON:OK,BUTTON:OK,0)
END !IF

This generates the window shown in Figure 1.

Figure 1
Figure 1
  • Optionally, I may also want to know the following additional information to quickly find the point in the program where the error occurred:

The procedure name (Saves figuring out which procedure out of many was executing when the error occurred.) This example is for the procedure ProcessCodes.

GET(AnyFile,Any:ByAnyId)
IF ERRORCODE()
  MESSAGE('File Processing Error:' |
  & '<10,10>' |
  & 'File: ' & ERRORFILE() & '<10>' |
  & 'Errorcode: ' & ERRORCODE() & '<10>' |
  & 'Description: ' & ERROR() |
  &'<10,10>' |
  & 'Procedure: ProcessCodes', |
  'Data File Problem', |
  ICON:EXCLAMATION,BUTTON:OK,BUTTON:OK,0)
END !IF

This generates the window shown in Figure 2.

Figure 2
Figure 2
  • The embed point (Saves figuring out where in the procedure you are) This example is for the procedure ProcessCodes, embed point after Lookups.
GET(AnyFile,Any:ByAnyId)
IF ERRORCODE()
  MESSAGE('File processing error:' |
  & '<10,10>' |
  & 'File: ' & ERRORFILE() & '<10>' |
  & 'Errorcode: ' & ERRORCODE() & '<10>' |
  & 'Description: ' & ERROR() |
  &'<10,10>' |
  & 'Procedure: ProcessCodes' |
  &'<10>' |
  & 'Location: After Lookups', |
  'Data File Problem', |
  ICON:EXCLAMATION,BUTTON:OK,BUTTON:OK,0)
END !IF

This generates the window shown in Figure 3.

Figure 3
Figure 3

Is a better solution possible?

This ended up being a fair amount of code to generate each time. What I wanted was a single template flexible enough to address each of the above levels of concern.

The simple file error template

Enter the following code into the file SMPL0001.TPL (or download the template at the top of the article), then register the template.

#TEMPLATE(SMPL0001,'Simple File Error')
#PROCEDURE(FileErrorMessage,'Simple File Error Procedure')
#PROTOTYPE('(<<STRING>,<<STRING>)')
#DISPLAY('')
#DISPLAY('This is a generic file error message procedure')
#DISPLAY('')
#DISPLAY(' Default Procedure is Unspecified')
#DISPLAY(' Default Embed point is Unspecified')
#DISPLAY(' Icon is Exclamation')
#DISPLAY(' Title is File Error')
#DISPLAY('')
#DISPLAY('Possible Calling Conventions:')
#DISPLAY(' O_FileError()')
#DISPLAY(' O_FileError(''Procedure'')')
#DISPLAY('..O_FileError(''Procedure'',''Embed Point'')')
#Display('')
#LOCALDATA
L:Procedure String(30)
L:EmbedPt String(40)
#ENDLOCALDATA
%PROCEDURE %ProcedureType(ErrorProc,ErrorEmbed)
#FOR(%LocalData)
%[20]LocalData %LocalDataStatement
#ENDFOR
CODE
  L:Procedure = ErrorProc
  IF L:Procedure = ''
    L:Procedure = 'Unspecified'
  END
  L:EmbedPt = ErrorEmbed
  IF L:EmbedPt = ''
    L:EmbedPt = 'Unspecified'
  END
  !
  ! Display message
  !
  MESSAGE('File processing error:' |
  & '<10,10>' |
  & 'File: ' & ERRORFILE() & '<10>' |
  & 'Errorcode: ' & ERRORCODE() & '<10>' |
  & 'Description: ' & ERROR() |
  & '<10,10>' |
  & 'Procedure: ' & L:Procedure & '<10>' |
  & 'Location: ' & L:EmbedPt, |
  'Data File Problem', |
  ICON:Exclamation,Button:OK,Button:OK,0)

The Template in action

First, add a new procedure to your application.

  • At the application tree press Insert
  • Enter O_FileError for the procedure name
  • Select FileErrorMessage as the procedure type.
  • Press OK

This should look something like this:

Figure 4
Figure 4

Then, depending on your needs you could call FileErrorMessage as follows and get the following results. Compare the code required and generated results with what we did under levels of concern.

The code fragment...

GET(AnyFile,Any:ByAnyId)
IF errorcode()
  O_FileError()
END !IF

...produces the screen:

Figure 5
Figure 5

The code fragment...

GET(AnyFile,Any:ByAnyId)
IF ERRORCODE()
  O_FileError('ProcessCodes')
END !IF

...produces the screen:

Figure 6
Figure 6

Finally, the code fragment...

GET(AnyFile,Any:ByAnyId)
IF ERRORCODE()
  O_FileError('ProcessCodes','After Lookups')
END !IF

...produces the screen:

Figure 7
Figure 7

Conclusion

Using the above template has provided a flexible, quick, standardized way to handle file error messages. For extended needs the above template could be modified appropriately.

Printer-friendly version

Reader Comments

To add a comment to this article you must log in.