Sunday, May 2, 2010

Error Handling for Online and Application Engine PeopleCode

Its a common practice across programming languages to protect ones' code (PeopleCode). However, the so called scenarios that one can think of is limited because of various reasons. Hence, developers (including me till now :)) would catch the exception(s) and display something like "Unhandled exceptions encountered in ... ..."

Now coming to PeopleCode, be it the Application Engine or Online PeopleCode, we do, protect the PeopleCode we write appropriately. But wouldn't it be better if we allow PeopleCode itself to capture exceptions and display relevant text, since, PeopleSoft itself has captured all the possible scenarios that could occur, either while compiling or during execution.

The exception handling that I'm referring to is already delivered by PeopleSoft as most of the exception classes in PeopleSoft extend Exception

I created a sample Application Engine Program to demonstrate this further...I tried out code below in a PeopleCode Action ...

import SCM_UTILITIES:ExceptionUtilities:ExceptionHandler;

try
&FileName_Path_ = "D:\Temp\PSOPTIONS_" | %Date | ".xls";
/* Opening a CSV file */
&GL_LEDFile_ = GetFile(&FileName_Path_, "W", %FilePath_Absolute);
/* Checking whether the File if Opened Successfully */
If &GL_LEDFile_.IsOpen Then
&GL_QryObj_ = %Session.GetQuery();
/* Initialising the Rowset & Values */
&GL_LEDRS_ = CreateRowset(Record.PSOPTIONS);
&GL_LEDRS_.Fill();
&GL_CSVString_ = &GL_QryObj_.FormatResultString(&GL_LEDRS_, %Query_XLS, 1, &GL_LEDRS_.ActiveRowCount);
/* Writing the file with the Location Data */
&GL_LEDFile_.WriteLine(&GL_CSVString_);
Else
Error ("Error: Opening of File Failed");
End-If;
REM *** I've changed the variable name on purpose, so that a run time exception is generated as there is no file object as &GL_LEDFil_ ***;
REM *** the correct statement is &GL_LEDFile_.Close(); ***;
&GL_LEDFil_.Close();

catch Exception &ex
Local SCM_UTILITIES:ExceptionUtilities:ExceptionHandler &handler = create SCM_UTILITIES:ExceptionUtilities:ExceptionHandler();
REM *** Display the Message Text and Message Explain Text and then FAIL it!!! ***;
REM Error (&handler.getMessageText(&ex));
REM Be careful, since you’re using the messagebox and it causes an implicit COMMIT in certain tools version;

MessageBox(0, "", 0, 0, "Exception Logged > " | &handler.getMessageText(&ex));
Error (&handler.getMessageExplainText(&ex));
end-try;

The Application Engine did fail as expected, and the stdout showed the following error
This is the messageText --> Exception Logged > First operand of . is not an object, so cannot access member Close. (0,0)

This is the explanation for the messageText --> The first operand of the dot operator must be a value of some object type to access the method or property, but the value found was not an object type.

Process 10356 ABENDED at Step PP_TEST_AE.MAIN.Step02 (PeopleCode) -- RC = 4 (108,524)
It also servers another purpose, we need not use the Exit(1) feature and then set the Action On Return property to Abort
The same PeopleCode can be used for online customizations as well.

1 comment:

spymom said...

Wow! This is a great example, exactly what I was looking for! Thank you for your post