We have some global classes that have been exposed to the customer so they can utilize some code. If an error occurs in this code, we write out a log record into a custom object. The problem comes when one of our customers decides to utilize this code in a controller or a context where DML is not allowed. If this happens SF generates a Limit Exception and crashes hard.
Salesforce gives you the ability to test for most limits to see how close you are using the limit class.
But there doesn't seem to be a way to tell if you can perform an insert and since Salesforce won't let you catch the exceptions, there seems to be no way around.
If I could do something like limit.getIsDMlAllowed(); I could avoid writing out the log messages and instead just return the errors to the user.
Does anyone know of a way to check if DML is allowed before performing DML?
By the way if anyone is looking for a work around for this issue. We decided to utilize the rest api and make an http Request to create the record. Just seems like an extreme workaround for the issue and I would rather only do the callout if we can't insert the record normally.