The bug is likely in your Exception handling code.
The following message is indicating a method has been called on an instance of an Exception class that isn't applicable.
FATAL_ERROR System.TypeException: Procedure is only valid for System.DmlException and System.MailException
You need to be careful with exceptions because Apex will compile code that probably doesn't make sense for the Type. E.g. The following with produce the same error as you are getting:
try {
Integer divideByZero = 1 / 0;
} catch (Exception e) {
e.getDmlType(1);
}
The exception that gets thrown in this example is a MathException. The method getDmlType(i)
isn't valid for it and apex will instead throw a TypeException if you aren't dealing with a DMLException or EmailException.
This can occur for the following methods:
getDmlFieldNames(i)
getDmlFields(i)
getDmlId(i)
getDmlIndex(i)
getDmlMessage(i)
getDmlStatusCode(i)
getDmlType(i)
getNumDml()
The line number of the TypeException
should take you directly to the problem in the exception handling.