I have a class LogException
public class LogException extends Exception {
public String ModuleName {get; set;}
public String StackTrace {get; set;}
public String ClassName {get; set;}
public String MethodName {get; set;}
public String ExceptionCause {get; set;}
public void log(Exception ex)
{
try
{
extractExceptionData(ex);
writeToObject(ex);
}
catch(Exception e)
{
new LogException().Module('LogException').log(e);
}
}
public LogException Module(String Name)
{
ModuleName = name;
return this;
}
public LogException ExceptionCause(String cause)
{
ExceptionCause = cause;
return this;
}
public void extractExceptionData(Exception ex)
{
try
{
stackTrace = ex.getStackTraceString().substringBefore('\n');
className = stackTrace.substringAfter('.').substringBefore('.');
methodName = stackTrace.substringBefore(':').substringAfter(className).substringAfter('.');
}
catch(Exception e)
{
new LogException().Module('LogException').log(e);
}
}
public void writeToObject(Exception ex)
{
try
{
// insert to object(database) here
}
catch(Exception e)
{
new LogException().Module('LogException').log(e);
}
}
}
I implemented method chaining here. Therefore, I can call methods like
new LogException().Module('unitTestModule').Log(ex);
new LogException().ExceptionCause('divided by zero').Log(ex);
new LogException().Module('unitTestModule').ExceptionCause('Probably no data in account').Log(ex);
new LogException().ExceptionCause('Probably no data in account').Module('unitTestModule').Log(ex);
My question is, how can I implement the class so that, I can only call ExceptionCause
right after Module
or vice versa.
Aucun commentaire:
Enregistrer un commentaire