18
Thu, Apr
5 New Articles

The API Corner: What to Do with Messages in the Application Program

APIs
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Let's explore detecting and handling API application-related error messages.

 

The past several articles in this column have been related to detecting and managing messages that have been sent to other jobs on the system. We looked at handling message watches, validating inquiry message responses, and providing responses to inquiry messages. Today, we will continue looking at messages but turn inward. This article will review some of the approaches available to handle message conditions that exist within the current application program.

 

Most system APIs, at least those that start with a Q, provide you with an error code parameter that allows you to control how errors detected by the API should be returned to your program. There are two formats to the error code parameter, with the most important element being the Bytes provided field, which exists in both formats. When Bytes provided is set to zero, you are instructing the API to return any error information back to your program as an escape message. When Bytes provided is eight or greater for format ERRC0100, and twelve or greater for format ERRC0200, you are instructing the API to return any information back to your program in the error code parameter itself.

 

In writing an application program, there some situations where I anticipate specific errors in calling an API. To avoid the overhead of the system sending and the application program then receiving messages, I prefer to receive error-related information directly through the error code parameter. In other cases, I do not expect any error in calling an API and prefer using escape messages. For these reasons, I generally start out my programs with the following:

 

d/copy qsysinc/qrpglesrc,qusec                         

…                                                       

dErrCde           ds                  qualified        

d Common                              likeds(QUSEC)    

d ErrMsgTxt                    512                   

/free                                  

  monitor;

  QUSBPRV = 0;                             

  ErrCde.Common.QUSBPRV = %size(ErrCde);

  on-error;

  …

  endmon;

 /end-free

 

The /copy directive copies in the error code parameter definition as provided by IBM in the QSYSINC library. This library can be installed by restoring option 13 of the i operating system. IBM defines the ERRC0100 error code format using the data structure name QUSEC, and the subfield QUSBPRV represents the Bytes provided field of ERRC0100. I use this definition as-is when I want escape messages sent in response to API-related errors. The statement QUSBPRV = 0; sets the Bytes provided field of the QUSEC data structure to zero, indicating that error conditions should be returned as exception messages.

 

I also define a version of the error code structure named ErrCde. ErrCde is defined with the same first 16 bytes as the QUSEC data structure but has an additional 512 bytes allocated for possible message replacement data that may be returned in the ErrCde error code structure when an API returns error-related information. The use of 512 bytes for ErrMsgTxt is somewhat arbitrary and dependent on the error message replacement text that you anticipate coming back from a given API call. I find 512 bytes sufficient for most system messages as long as I am not expecting long IFS path names to be returned. The statement ErrCde.Common.QUSBRV = %size(ErrCde); sets the Bytes provided field of the ErrCde data structure to 528, indicating that error conditions should be returned in the ErrCde data structure rather than as exception messages.

 

The "monitor" and associated on-error, endmon operation codes are, among other things, an admission that, while I strive to write "perfect" code, I occasionally fall short of that goal. I use a global monitor to ensure that my program has an opportunity to send an appropriate error message for the application rather than a potentially cryptic error message from either RPG run-time (for instance RNQ0121 – An array index is out of range) or the i operating system (MCH0603 – Range of subscript value of character string error), neither of which tells the poor user to call support. Rather than using a monitor, I could use RPG-provided alternatives, such as a program exception/error subroutine (*PSSR) and a program status data structure (PSDS), but, as you will see later, I prefer a more granular approach to error handling than catchall PSSRs per procedure. The ability to nest monitor groups and have specific on-error processing directly associated with the failing code sold me on monitor groups long ago.

 

Returning to the discussion of error code usage, let's say we need to determine if a given object exists and, if not, have the application create the object. One approach to finding out if the object exists would be to use an API such as Retrieve Object Description (QUSROBJD). The QUSROBJD API will retrieve object information about a specific object and, while we don't really care about this object information, if anything is returned, then we know that the object exists. As we are checking for the existence of an object, this is a scenario where it is quite reasonable for the API to return an error condition such as CPF9801 – Object &2 in library &3 not found.

 

In this situation, I would use the ErrCde error code parameter as shown below:

 

 /copy qsysinc/qrpglesrc,qusrobjd                                    

 /copy qsysinc/qrpglesrc,qusec                                        

                                                                     

dRObjD            pr                  extpgm('QUSROBJD')             

d Receiver                       1    options(*varsize)              

d LenReceiver                   10i 0 const                          

d Format                         8    const                          

d ObjName                       20    const                          

d ObjType                       10    const                           

d ErrCde                              likeds(QUSEC) options(*nopass) 

d ASP                            1    const options(*varsize :*nopass)

                                                                     

dErrCde           ds                  qualified                      

d Common                              likeds(QUSEC)                  

d ErrMsgTxt                    512                                   

                                                                     

dQualName         ds                                                  

d Name                          10    inz('SOMEOBJECT')               

d Library                       10    inz('SOMELIB')                  

                                                                       

dType             s             10    inz('*USRSPC')                  

                                                                      

 /free                                                                

                                                                       

  monitor;                                                            

  QUSBPRV = 0;                                                        

  ErrCde.Common.QUSBPRV = %size(ErrCde);                               

                                                                      

  RObjD(QUSD0100 :%size(QUSD0100) :'OBJD0100' :QualName :Type :ErrCde);

  if ErrCde.Common.QUSBAVL > 0;                                       

     select;                                                           

        when ErrCde.Common.QUSEI = 'CPF9801';                         

             // Create the user space                                 

        when ErrCde.Common.QUSEI = '???????';                        

             // Additional error checks that I will handle          

        other;                                                      

             // Something more than I expected so send 

             // appropriate message(s) and end         

     endsl;                                                         

  endif;                                                            

                                                                    

  // Continue processing and eventually end the program normally    

                                                                    

  *inlr = *on;                                                      

  return;                                                           

                                                                     

  on-error;                                                         

  // ...                                                            

  endmon;                                                            

                                                                    

 /end-free          

 

A few points concerning the program code provided above: First and foremost, whenever you use an error code with a Bytes provided field set to a non-zero value, the first thing you need to do is examine the associated Bytes available field (ErrCde.Common.QUSBAVL in the example). If the value is greater than zero, then an error was encountered by the API and you must take appropriate action. You will not be sent an escape message from the API. To continue the application program without awareness that an error has occurred is just asking for trouble. QUSBAVL incidentally is the name of the field, within the QUSEC error code structure provided by IBM, that contains the Bytes available.

 

When Bytes available is greater than zero, I recommend entering a "select" group so that you can easily diagnose, and perhaps correct, the problem (and ensure with the "other" operation code that all possible error conditions are addressed in one way or another). In order to do a good job in the select processing, you will need to examine those errors that might be returned by the API. This list of possible error conditions (messages) can be found at the end of the API documentation in the Information Center.

 

In the case of QUSROBJD, one message condition of note is CPF9801 – Object &2 in library &3 not found. In the case of the sample program, the one shown "when" test is for CPF9801 with the recovery action being that the object is created. QUSEI incidentally is the name of the field, within the QUSEC error code data structure provided by IBM, that contains the Exception ID.

 

Examining the various error conditions that can be returned by the API may give you food for thought. You will notice for instance that the following error can be returned: CPF9810 – Library &1 not found. What do you want to do if the library SOMELIB doesn't exist? If it would be appropriate for the application to create the library, then a specific "when ErrCde.Common.QUSEI = CPF9810" and a recovery action of first creating the library followed by then creating the object may be called for.

 

Otherwise a more generic "when" such as shown below may be sufficient.

 

        when ((ErrCde.Common.QUSEI = 'CPF9810') or             

              (ErrCde.Common.QUSEI = 'CPF9802') or             

              (ErrCde.Common.QUSEI = 'CPF9820'));              

             // Send the original error information followed    

             // by a message indicating an environmental       

             // problem external to the application            

 

This "when" operation is checking for three possible error conditions that may be outside of the application program's control and may need to be addressed by the system administrator. The error conditions are CPF9810 – Library not found, CPF9802 – Not authorized to object, and CPF9820 – Not authorized to the library. In this case, sending the original error message (to document the specifics of what went wrong) followed by an application error message telling the user to contact the system administrator for resolution may be more appropriate. In a future article, we will look at the specifics of how to actually send these messages.

 

Note that I am greatly simplifying the work that should be done when examining and handling various error conditions. In the case of CPF9810, for instance, there are many possible recovery messages that may be sent. If our application is a command processing program (CPP), then the CPF9810 may indicate a user error when specifying the library name, in which case the end user should resolve the problem. If our application is intended to be called by other programs, and not directly as a CPP, then the CPF9810 may indicate a product configuration problem for the system administrator to resolve. If our application really has the library name hardcoded as in the example, then the CPF9810 may indicate an internal failure within the product, and software support is need to resolve the problem. These types of decisions can only be made by you within the context of the current application program.

 

 

Other error conditions that can be returned by the API will indicate that "something" is really wrong and that the application program has an internal problem (or in some cases that the job itself is experiencing internal problems). These error conditions, such as CPF3C21 – Format name not valid, should never occur once you have debugged the application and put it into production. But if they do occur, then sending the original error message followed up by an application error message telling the user to contact the system administrator, and for the administrator to then contact you for resolution, would be more appropriate. Unlike the case of CPF9810, the administrator can do little in resolving a CPF3C21. These are the types of error conditions that would be addressed by the "other" operation code of the "select" group. Again, how to actually send these messages will be shown in a subsequent article.

 

Now let's look at the case where we have no reasonable expectation that the API will return an error condition. Let's say the application program needs to determine the date format in use by the current job. One way to access this information is to use an API such as Retrieve Job Information (QUSRJOBI). The QUSRJOBI API retrieves specific information about a job, and, while many things could go predictably wrong when accessing job information related to any arbitrary job (for instance, the job is no longer on the system), we would not expect any difficulty in accessing job-related information for the current job. Or at least we would not expect any errors once we have debugged the application to ensure that correct format names are in use, receiver variable sizes are correct, etc.—in other words, the types of error conditions found at the end of the QUSRJOBI API documentation.

 

In this situation, I would use the QUSEC error code parameter as shown below:

 

/copy qsysinc/qrpglesrc,qusrjobi                                  

 /copy qsysinc/qrpglesrc,qusec                                    

                                                                  

dRJobI            pr                  extpgm('QSYS/QUSRJOBI')     

d Receiver                   65535    options(*varsize)           

d LenReceiver                   10i 0 const                       

d Format                         8    const                       

d QualJobName                   26    const                       

d IntJobID                      16    const                       

d ErrCde                         1    options(*varsize :*nopass)  

d ResetPfr                       1    options(*nopass)            

                                                                  

dErrCde           ds                  qualified                   

d Common                              likeds(QUSEC)               

d ErrMsgTxt                    512                                

                                                                   

 /free                                                             

                                                                   

  monitor;                                                         

  QUSBPRV = 0;                                                      

  ErrCde.Common.QUSBPRV = %size(ErrCde);                           

                                                                   

  RJobI(QUSI0400 :%size(QUSI0400) :'JOBI0400' :'*' :' ' :QUSEC);   

                                                                    

  // Continue processing and eventually end the program normally   

                                                                   

  *inlr = *on;                                                      

  return;                                                          

                                                                   

  on-error;                                                        

     // Something more than I expected so send                     

     // appropriate message(s) and end                             

  endmon;                                                          

                      

 /end-free            

 

As the Bytes provided field for the QUSEC error code structure is set to zero, the QUSRJOBI API will send an escape message if an error is encountered. The escape message will trigger the monitor, and control will be passed to the on-error block of the program. Here, you find a comment similar to what is found in the "other" operation of the previous ErrCde example. Unlike the ErrCde "other" logic, the application here does not need to send the original error message. The original error message was sent by the system and so is currently in the job log for review by the operator. We simply need to send the message for the user of the program to contact the administrator and for the administrator to, in turn, contact you. It is worth pointing out that this same on-error block is what will also run if we encounter an RPG run-time error such as an invalid array index. This one block of code represents a central location in the application for the management of totally unexpected error conditions.

 

One note of caution: if you decide to change the above code and introduce an error in the call to QUSRJOBI, please note that the on-error block is not currently doing anything; it's just a comment. Introducing an error, for instance by specifying an invalid format name for the third parameter, will quickly remind you that the RPG cycle is still alive and well!

 

So now we've reviewed how to detect API application-related errors. The next article will discuss how to report these error conditions back to the user, the system administrator, and you.

 

In the meantime, if you have any API questions, send them to me at This email address is being protected from spambots. You need JavaScript enabled to view it.. I'll see what I can do about answering your burning questions in future columns.

 

Bruce Vining

Bruce Vining is president and co-founder of Bruce Vining Services, LLC, a firm providing contract programming and consulting services to the System i community. He began his career in 1979 as an IBM Systems Engineer in St. Louis, Missouri, and then transferred to Rochester, Minnesota, in 1985, where he continues to reside. From 1992 until leaving IBM in 2007, Bruce was a member of the System Design Control Group responsible for OS/400 and i5/OS areas such as System APIs, Globalization, and Software Serviceability. He is also the designer of Control Language for Files (CLF).A frequent speaker and writer, Bruce can be reached at This email address is being protected from spambots. You need JavaScript enabled to view it.. 


MC Press books written by Bruce Vining available now on the MC Press Bookstore.

IBM System i APIs at Work IBM System i APIs at Work
Leverage the power of APIs with this definitive resource.
List Price $89.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: