UdfException.java

/*
** Module   : UdfException.java
** Abstract : Exception thrown when a UDF raises an error.
**
** Copyright (c) 2021-2022, Golden Code Development Corporation.
**
** -#- -I- --Date-- --------------------------------------Description--------------------------------------
** 001 ECF 20211223 Created initial version.
** 002 VVT 20220830 errorEntry(): fixed NumberFormatException. See #6694-7.
**     IAS 20220913 Re-work processing of UDFs errors/warnings.
*/

/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
**
** Additional terms under GNU Affero GPL version 3 section 7:
**
**   Under Section 7 of the GNU Affero GPL version 3, the following additional
**   terms apply to the works covered under the License.  These additional terms
**   are non-permissive additional terms allowed under Section 7 of the GNU
**   Affero GPL version 3 and may not be removed by you.
**
**   0. Attribution Requirement.
**
**     You must preserve all legal notices or author attributions in the covered
**     work or Appropriate Legal Notices displayed by works containing the covered
**     work.  You may not remove from the covered work any author or developer
**     credit already included within the covered work.
**
**   1. No License To Use Trademarks.
**
**     This license does not grant any license or rights to use the trademarks
**     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
**     of Golden Code Development Corporation. You are not authorized to use the
**     name Golden Code, FWD, or the names of any author or contributor, for
**     publicity purposes without written authorization.
**
**   2. No Misrepresentation of Affiliation.
**
**     You may not represent yourself as Golden Code Development Corporation or FWD.
**
**     You may not represent yourself for publicity purposes as associated with
**     Golden Code Development Corporation, FWD, or any author or contributor to
**     the covered work, without written authorization.
**
**   3. No Misrepresentation of Source or Origin.
**
**     You may not represent the covered work as solely your work.  All modified
**     versions of the covered work must be marked in a reasonable way to make it
**     clear that the modified work is not originating from Golden Code Development
**     Corporation or FWD.  All modified versions must contain the notices of
**     attribution required in this license.
*/

package com.goldencode.p2j.persist.orm;

import java.sql.*;
import java.util.regex.*;
import java.util.regex.Pattern;

import org.apache.commons.lang.*;

import com.goldencode.p2j.persist.*;
import com.goldencode.p2j.persist.dialect.*;
import com.goldencode.p2j.util.ErrorManager.*;
import com.google.gwt.i18n.client.CustomDateTimeFormat.*;

/**
 * Exception thrown when a UDF raises an error.
 */
public class UdfException extends PersistenceException
{
   /** Error descriptor */
   private final ErrorEntry errorEntry;

   /**
    * C'tor for the UdfException using source SQLException and pattern for parsing exemption message  
    * @param errorEntry
    *        error desccriptor
    * @param cause
    *        Source SQLException
    */
   public UdfException(ErrorEntry errorEntry, SQLException cause)
   {
      super(cause.getMessage(), cause);
      this.errorEntry = errorEntry;
   }

   /**
    * Create the error descriptor from the SQLException message
    * @param pattern
    *        Pattern for parsing exception message
    * @param message
    *        SQLException message
    * @return error descriptor. 
    */
   public static ErrorEntry errorEntry(Pattern pattern, String message)
   {
      Matcher matcher = pattern.matcher(message);
      if (matcher.matches() && matcher.groupCount() == 5)
      {
         try
         {
            return new ErrorEntry(Integer.parseInt(matcher.group(5)), // was 4 
                     matcher.group(2), !StringUtils.isBlank(matcher.group(1)));
         }
         catch (NumberFormatException e)
         {
            // no-op
         }
      }

      return new ErrorEntry(-1, message, false);
   }

   /**
    * Get the error descriptor.
    * @return the error descriptor.
    */
   public ErrorEntry getErrorEntry()
   {
      return errorEntry;
   }
   
   /**
    * Test program
    * @param args
    *        command line args
    */
   public static void main(String... args )
   {
      Pattern pattern = Pattern.compile(
               "^Exception calling user-defined function: .*: (\\*\\* )?(.*)(\\. *)?\\(([0-9]*)\\).*$",
               Pattern.DOTALL
      );
      ErrorEntry entry = errorEntry(pattern,
               "Exception calling user-defined function: \"toString(2021-07-19, 99999): ** Date format 99999 is incomplete (154)\"; SQL statement:\n"
               + "\n"
               + "select \n"
               + "   udftest_1_0_.recid as id0_, udftest_1_0_._multiplex as column1_0_, udftest_1_0_._errorFlag as column2_0_, udftest_1_0_._originRowid as column3_0_, udftest_1_0_._datasourceRowid as column4_0_, udftest_1_0_._errorString as column5_0_, udftest_1_0_._peerRowid as column6_0_, udftest_1_0_._rowState as column7_0_, udftest_1_0_.test_name as test8_0_, udftest_1_0_.fdate as fdate9_0_, udftest_1_0_.fmt as fmt10_0_, udftest_1_0_.fstr_result as fstr11_0_ \n"
               + "from\n"
               + "   tt1 udftest_1_0_ \n"
               + "where\n"
               + "   udftest_1_0_._multiplex = ? and upper(rtrim(udftest_1_0_.test_name)) = 'DTOSTRING' and udftest_1_0_.fdate = ? and upper(rtrim(udftest_1_0_.fmt)) = ? and eq_8(rtrim(toString_13(udftest_1_0_.fdate, ?)), upper(rtrim(udftest_1_0_.fstr_result)))\n"
               + "order by\n"
               + "   udftest_1_0_._multiplex asc, udftest_1_0_.recid asc\n"
               + " limit ? [90105-200]\n"
               + "");
      System.out.printf("%s%s (%d); addDot:%s\n", entry.prefix ? "** " : "", entry.text, entry.num, false);
      pattern = Pattern.compile(
               "^ERROR: (\\*\\* )?(.*)(\\.)? *\\((-?[0-9]*)\\).*$",
               Pattern.DOTALL
      ); 
      entry = errorEntry(pattern,
               "ERROR: Invalid time format: [HH:MM:SS.SSS A+HH:MM] (-1)\n"
               + "  Where: PL/pgSQL function udf.tostring(date,text");
      System.out.printf("%s%s (%d); addDot:%s\n", entry.prefix ? "** " : "", entry.text, entry.num, false);
   }
}