Last active
June 29, 2019 15:17
-
-
Save AlwaysThinkin/2dc05bb7907fe1f2301d735d324fbcc7 to your computer and use it in GitHub Desktop.
Files for Exception Handling Session
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LogUtility { | |
public static void messageLog(String classMethod, String customMessage, Set<Id> recordIds) { | |
Case logEntry = new Case( | |
Type = 'Message', | |
Subject = customMessage, | |
Description = 'Record IDs: ' + recordIds + '\n' | |
+ customMessage); | |
insertLog(logEntry, classMethod); | |
} | |
public static void errorLog(String classMethod, Exception e, Set<Id> recordIds){ | |
Case logEntry = new Case( | |
Type = 'Exception', | |
Subject = e.getMessage(), | |
Description = 'Record IDs: ' + recordIds + '\n' | |
+ 'Exception Type: ' + e.getTypeName() + '\n' | |
+ 'Stack Trace: ' + e.getStackTraceString() + '\n' | |
+ 'Line Number: ' + e.getLineNumber() | |
); | |
insertLog(logEntry, classMethod); | |
} | |
private static void insertLog(Case logEntry, String classMethod) { | |
if(logEntry != null) { | |
logEntry.Origin = classMethod; | |
logEntry.Reason = UserInfo.getUserId(); | |
Database.DMLOptions options = new Database.DMLOptions(); | |
options.allowFieldTruncation = true; | |
Database.insert(logEntry, options); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LogUtility { | |
//Populate fields on a Case record for a simple custom message | |
public static void messageLog(String classMethod, String customMessage, Set<Id> recordIds) { | |
//Prepare a Case record's fields to receive the class + method, the custom message and record IDs | |
//in the Subject and Description, and set Type to be "Message" | |
//Send the prepared Case record to insertLog() for final processing | |
} | |
//Populate fields on a Case record for an exception error message | |
public static void errorLog(String classMethod, Exception e, Set<Id> recordIds){ | |
//Prepare a Case record's fields to receive the class + method, the error message and record IDs | |
//in the Subject and Description, and set Type to be "Exception" | |
//Send the prepared Case record to insertLog() for final processing | |
} | |
//insertLog handles the parts of the message and exception methods that are the same | |
//and then use a Database methods instead of DML to insert the Case, allowing Field Truncation | |
private static void insertLog(Case logEntry, String classMethod) { | |
//Add the classMethod String to the Case.Origin field | |
//Add the running user to the Case.Reason field | |
//Prepare to insert the Case but... | |
//...make sure that we protect if from excessively long strings by allowing truncation... | |
//...which means using Database methods instead of DML | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@isTest | |
public class LogUtilityTest { | |
public class MyException extends Exception {} | |
public static final String TEST_CLASS_METHOD = 'TestClass.TestMethod'; | |
public static final String TEST_LOG_MESSAGE = 'Test this custom message'; | |
public static testMethod void testLogMessage() { | |
Test.startTest(); | |
LogUtility.messageLog(TEST_CLASS_METHOD, TEST_LOG_MESSAGE, null); | |
Test.stopTest(); | |
List<Case> testErrorLogs = new List<Case>([select Subject from Case | |
where Origin = :TEST_CLASS_METHOD]); | |
System.assertNotEquals(0, testErrorLogs.size()); | |
List<Id> errorLogIds = new List<Id>(); | |
for(Case c : testErrorLogs) { | |
if(c.Subject == TEST_LOG_MESSAGE) { | |
errorLogIds.add(c.Id); | |
} | |
} | |
System.assertNotEquals(0, testErrorLogs.size()); | |
} | |
public static testMethod void testLogException() { | |
Exception testException = new MyException(TEST_LOG_MESSAGE); | |
Test.startTest(); | |
LogUtility.errorLog(TEST_CLASS_METHOD, testException, null); | |
Test.stopTest(); | |
List<Case> testErrorLogs = new List<Case>([select Subject, Origin from Case where Origin = :TEST_CLASS_METHOD and Type = 'Exception']); | |
System.assertNotEquals(0, testErrorLogs.size()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Try this in Anonymous Apex | |
List<String> strings = new List<String>(); | |
strings.add('word'); | |
String s1 = strings[0]; | |
try{ | |
String s2 = strings[1]; // Causes a ListException because we've only got one item in our List | |
} | |
catch(Exception e){ | |
LogUtility.errorLog('From Anonymous Apex', e, new Set<Id>()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment