cancel
Showing results for 
Search instead for 
Did you mean: 

How to raise an application error at SXI_MONITOR from Java Mapping

Former Member
0 Kudos

Dear gurus,

I’m working with PI 7.30 and I’ve a Java mapping which I’d like to stop when a certain field comes empty from ERP.

I mean during my code I’m checking if the field is empty and in case it is an exception is raised to flag the message to red at SXI_MONITOR.

Unfortunately I’ve been trying the classes StreamTransformationException , StreamTransformationRuntimeException, RuntimeException, Exception, etc.. but without success.

my code inside a method:

if(fName.equalsIgnoreCase("") || fFrmt.equalsIgnoreCase("")){

     throw new RuntimeException("text");  

Does anyone knows how to achieve that?

Thanks in advance and regards,

Jorge

Accepted Solutions (1)

Accepted Solutions (1)

anupam_ghosh2
Active Contributor
0 Kudos

Hi Jorge,

                Your code seems to be correct one. What I guess is the fact that maybe the control is not reaching inside the if condition. Thus what I suggest is that you add a trace statement within if condition as shown below and I made little change to the exception type (assuming you have used this within  "transform" method of java mapping)

if(fName.equalsIgnoreCase("") || fFrmt.equalsIgnoreCase("")){

    

     this.getTrace().addInfo("inside if condition");

     throw new StreamTransformationException("text");  

}

    this.getTrace().addInfo("after if condition");

This will prove whether the program flow is actually reaching inside if condition or not.

Please also kindly re-check that there is no outer catch statement to catch the exception within the function itself.

In case the changes do not help ,could you please post the function where you wrote this if statement.

Regards

Anupam

Message was edited by: Anupam Ghosh

Former Member
0 Kudos

Hi Anupam,

The program is reaching inside as I'm able to see the text "inside the if condition" there. Also the excpetion is throwed cause its text gets logged at the SXI_MONITOR trace. However I cannot get the message in error (red flag) in order to stop going further and delivering an empty file.

Here is the an example with another method:

...

public void transform(TransformationInput in, TransformationOutput out) throws StreamTransformationException {

try

     {

     B64 = getBase64(doc, base64Xpath, ERROR_TEXT);

     fileName = getFileName(doc, fileNameXpath, ERROR_TEXT);

     }

     catch (Exception e) {

          getTrace().addWarning(ERROR_TEXT + "couldn't extract the data from the given Xpaths");

          getTrace().addWarning(e.getMessage());

     }

     private String getBase64(Node node, String base64Xpath, final String ERROR_TEXT) {


          String B64 = null;

          try {


               XPath xpath = XPathFactory.newInstance().newXPath();

               XPathExpression expr = xpath.compile(base64Xpath);


               Object result = expr.evaluate(node, XPathConstants.NODESET);

               NodeList nodes = (NodeList) result;


               if (nodes.item(0).getTextContent()=="" || nodes.item(0).getTextContent()==null) {

                    getTrace().addWarning("inside if condition");

                    throw new StreamTransformationException(ERROR_TEXT + "XML element containing the Base64 was not provided or provided empty");

               }

               else {


                    B64 = nodes.item(0).getTextContent();

               }


          } catch (XPathExpressionException e) {


               getTrace().addWarning(ERROR_TEXT + "couldn't read the XPath passed by the mapping parameter");

               getTrace().addWarning(e.getMessage());


          } catch (Exception ex){

               getTrace().addWarning(ERROR_TEXT + "couldn't extract the Base64 encoded content");

               getTrace().addWarning(ex.getMessage());

          }

          return B64;

      }

}

Thanks in advance and regards,

Jorge

anupam_ghosh2
Active Contributor
0 Kudos

Hi Jorge,

                   I am removing a little part of your code which is a catch statement for the time being. Please try this code and lets hope you get a red flag this time . I have also removed try catch block from transform function.

public void transform(TransformationInput in, TransformationOutput out) throws StreamTransformationException {

     B64 = getBase64(doc, base64Xpath, ERROR_TEXT);

     fileName = getFileName(doc, fileNameXpath, ERROR_TEXT);

   

}

     private String getBase64(Node node, String base64Xpath, final String ERROR_TEXT) {


          String B64 = null;

          try {


               XPath xpath = XPathFactory.newInstance().newXPath();

               XPathExpression expr = xpath.compile(base64Xpath);


               Object result = expr.evaluate(node, XPathConstants.NODESET);

               NodeList nodes = (NodeList) result;


               if (nodes.item(0).getTextContent()=="" || nodes.item(0).getTextContent()==null) {

                    getTrace().addWarning("inside if condition");

                    throw new StreamTransformationException(ERROR_TEXT + "XML element containing the Base64 was not provided or provided empty");

               }

               else {


                    B64 = nodes.item(0).getTextContent();

               }


          } catch (XPathExpressionException e) {


               getTrace().addWarning(ERROR_TEXT + "couldn't read the XPath passed by the mapping parameter");

               getTrace().addWarning(e.getMessage());

          }

          return B64;

      }

}

If this trick suceeds will plan something else to include the removed catch statement.

One more thing I need to point out is the fact where is the end braces } for the function "transform"?

"getBase64" function seems to be  within "tranform" function without the end braces for transform function.

I have added the end braces for the transform function in above code.

Hope the changes help this time.

Regards

Anupam

Message was edited by: Anupam Ghosh

Message was edited by: Anupam Ghosh

Former Member
0 Kudos

Hi Anupam,

The missing braces were a copy/paste mistake

You were right! Issue was at the outer catch, removing like you did made it work

Is there any trick to include the removed catch Exception ex ? Thinking better, probably is pointless having that one there.

Thanks and regards,

Jorge

anupam_ghosh2
Active Contributor
0 Kudos

Hi Jorge,

               I am including the try catch block in this manner in transform function as shown below.

public void transform(TransformationInput in, TransformationOutput out) throws StreamTransformationException {

try

     {

     B64 = getBase64(doc, base64Xpath, ERROR_TEXT);

     fileName = getFileName(doc, fileNameXpath, ERROR_TEXT);

     }

     catch (Exception e) {

          getTrace().addWarning(ERROR_TEXT + "couldn't extract the data from the given Xpaths");

          getTrace().addWarning(e.getMessage());

          throw new StreamTransformationException("stopping the mapping here");

     }

}

Similarly I am adding the catch statement in the function getBase64. I hope this is a function you wrote yourself and does not come up with standard function prototype. i have added a throws clause with the function just like transform.

  private String getBase64(Node node, String base64Xpath, final String ERROR_TEXT)

throws StreamTransformationException


          String B64 = null;

          try {


               XPath xpath = XPathFactory.newInstance().newXPath();

               XPathExpression expr = xpath.compile(base64Xpath);


               Object result = expr.evaluate(node, XPathConstants.NODESET);

               NodeList nodes = (NodeList) result;


               if (nodes.item(0).getTextContent()=="" || nodes.item(0).getTextContent()==null) {

                    getTrace().addWarning("inside if condition");

                    throw new StreamTransformationException(ERROR_TEXT + "XML element containing the Base64 was not provided or provided empty");

               }

               else {


                    B64 = nodes.item(0).getTextContent();

               }


          } catch (XPathExpressionException e) {


               getTrace().addWarning(ERROR_TEXT + "couldn't read the XPath passed by the mapping parameter");

               getTrace().addWarning(e.getMessage());

              

               throw new StreamTransformationException("Stopping the mapping here");


          } catch (Exception ex){

               getTrace().addWarning(ERROR_TEXT + "couldn't extract the Base64 encoded content");

               getTrace().addWarning(ex.getMessage());

               throw new StreamTransformationException("Stopping the mapping here");

          }

          return B64;

      }

}

Regards

Anupam

Former Member
0 Kudos

Thank you very much Anupam 😉

Answers (2)

Answers (2)

rajasekhar_reddy14
Active Contributor
0 Kudos

If you have implemented java mapping to raise exception then avoid java mapping and write UDF to throw exception.

baskar_gopalakrishnan2
Active Contributor
0 Kudos

public void methodName() throws Exception {

try{

}catch(Exception e){

throw e;

}

You might want to try like as above... Search online for the same.