| Home | Previous Lesson: Course 3:: Session 24 :: Page 140 Next Lesson: Course 3:: Session 24 :: Page 160 |
| Sourcing
Error Messages Good applications define standard error messages for the possible errors that are known at design time. Each message is assigned a message id and severity number. This allows reading the error message using the error number at run-time. Severity number defines how serious the message is. For example, in Sybase SQL Server, severity level below 16 are informational. You may allow the user continuing the application if the severity level is below certain number. All these defined messages are stored either in a text file or in a database. All developers use these standard messages with parameters. There are two advantages of this method over hard-coding the error messages in the program.
For example, to display error message about a non-existing product, you can define a message something like: Product No # %n is not existing the database. The percentage sign indicates the position of the parameter. Systems like Sybase SQL Server use different characters after the percentage sign depending on the data type of the parameter that would be placed in that place, say n for numbers, s for strings and so on. However, PFC uses s for all data types. When you store the error messages in the text/database, you should use the following format.
PFC ships with a database "pfc.db" and it has a table "messages". If you decide to use the database, you can add all of your error messages in the "messages" table. "messages" table has about five pre-defined messages that are used by the PFC. If you decide to use a text file, then make sure to follow the above schema, and include the messages that are defined in the "messages" table:
Now, lets see how to use the text file/database to error messages sourcing. First lets start with the text file. You need to call the following function either from the pfc_Open or constructor event of the n_cst_AppManager object. inv_Error.of_SetPredefinedSource( "err-msg.txt" ) PFC uses a DataStore object with the d_definedmessages DataWindow object. When you call this function, PFC loads all the messages in the DataStore object. If you decide to use database instead of the text file, then dont make the above call, instead use the following (Actually, the above function is overloaded). Before you write the following code, declare an instance variable "itr_error" of type "n_tr" at the n_cst_AppManager. n_tr itr_error Write the following to the pfc_Open event of "n_cst_AppManager" object. Integer li_rc
itr_Error = CREATE n_tr
li_rc = itr_Error.of_Init( of_GetAppINIFile(), "ErrorDB" )
If li_rc <> 1 Then
MessageBox( "Error", &
"Error Transaction Object failed " + &
"initialization. ~nThis stops the application.", &
StopSign!, OK!, 1)
This.Event Post pfc_exit()
Return
End If
li_rc = itr_Error.of_Connect()
If li_rc <> 0 Then
MessageBox( "Error", &
"Error while connecting to the " + &
"error database. ~nThis stops the application.", &
StopSign!, OK!, 1)
This.Event Post pfc_exit()
Return
End If
li_rc = inv_Error.of_SetPredefinedSource( itr_Error )
If li_rc <> 1 Then
MessageBox( "Error", &
"Error while setting the error " + &
" info source.~nThis stops the application.", &
StopSign!, OK!, 1)
This.Event Post pfc_exit()
Return
End If
In the above code, we are creating a transaction object of type "n_tr". The use of this transaction object is to connect to the database that has the "messages" table. After that, we are populating the transaction object with the database specific information to connect to the database. of_GetAppINIFile() returns the application INI file name. of_Init() is available at the error service object "n_cst_error". This function reads the specified section and populates the transaction object. Then we are calling of_Connect() to connect to the database. Once the connection is made successfully, we are calling of_SetPredefinedSource() to load all the messages in the DataStore object. The setup process installs the ODBC entries for the PFC database. If you go to the database painter, you can see the entries for the PFC database in the ODBC configuration manager. However, we need to provide details in the application INI file as shown below, otherwise, PowerBuilder wont be able to connect to the PFC database. [ErrorDB] Now, if you test the application with the above error, PowerBuilder uses the default message id pfc_SystemError and the message box looks like:
Actually you will see only one button "OK". We have changed it in our database, thats why you see three buttons in the above picture. We will explain sending parameters for the defined messages in the next session. |
| Home | Previous Lesson: Course 3:: Session 24 :: Page 140 Next Lesson: Course 3:: Session 24 :: Page 160 |