Skip to content

Commit

Permalink
feat(exception email): add exception email parser
Browse files Browse the repository at this point in the history
Closes #2
  • Loading branch information
ArturMoczulski committed May 18, 2019
1 parent 1b1a210 commit 2d2952c
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 0 deletions.
82 changes: 82 additions & 0 deletions force-app/main/default/classes/ExceptionEmailParser.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
public with sharing class ExceptionEmailParser {
public static ExceptionData parse(String emailBody) {

System.debug(emailBody);

ExceptionData exData = new ExceptionData(
parseEnvironment(emailBody),
parseUserOrg(emailBody),
parseClassName(emailBody),
parseMessage(emailBody),
parseFileName(emailBody),
parseContext(emailBody),
parseLine(emailBody),
parseColumn(emailBody)
);

return exData;
}

public static String parseEnvironment(String emailBody) {
return emailBody.split('\\n')[0];
}

public static String parseUserOrg(String emailBody) {
return parseContent(
'Apex script unhandled( trigger)? exception by user/organization:(\n| )?(.*)',
emailBody,
3
);
}

public static String parseClassName(String emailBody) {
return parseContent(
'caused by: ([^:]*):.*',
emailBody,
1
);
}

public static String parseMessage(String emailBody) {
return parseContent(
'caused by: [^:]*: (.*)',
emailBody,
1
);
}

public static String parseFileName(String emailBody) {
return parseContent(
'(.*): line [0-9]+, column [0-9]+',
emailBody,
1
);
}

public static String parseContext(String emailBody) {
return emailBody.split('\\n')[4];
}

public static Integer parseLine(String emailBody) {
return Integer.valueOf(parseContent(
'.*: line ([0-9]+), column [0-9]+',
emailBody,
1
));
}

public static Integer parseColumn(String emailBody) {
return Integer.valueOf(parseContent(
'.*: line [0-9]+, column ([0-9]+)',
emailBody,
1
));
}

private static String parseContent(String regex, String body, Integer groupToReturn) {
Pattern pat = Pattern.compile(regex);
Matcher mat = pat.matcher(body);
mat.find();
return mat.group(groupToReturn);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="urn:metadata.tooling.soap.sforce.com" fqn="ExceptionEmailParser">
<apiVersion>45.0</apiVersion>
<status>Active</status>
</ApexClass>
100 changes: 100 additions & 0 deletions force-app/main/default/tests/ExceptionEmailParserTest.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
@isTest
public class ExceptionEmailParserTest {

private static String emailBody =
'Sandbox\n\n' +
'Apex script unhandled exception by user/organization: 0050R000001t3Br/00D0R000000DUxZ' + '\n\n' +
'ContactEx: execution of AfterInsert' + '\n\n' +
'caused by: System.NullPointerException: Attempt to de-reference a null object' + '\n\n' +
'Trigger.ContactEx: line 3, column 1';

@isTest
public static void testParse() {
ExceptionData exData = ExceptionEmailParser.parse(emailBody);

System.assertEquals('Sandbox', exData.environment());
System.assertEquals('0050R000001t3Br/00D0R000000DUxZ', exData.userOrg());
System.assertEquals('System.NullPointerException', exData.className());
System.assertEquals('Attempt to de-reference a null object', exData.message());
System.assertEquals('Trigger.ContactEx', exData.fileName());
System.assertEquals('ContactEx: execution of AfterInsert', exData.context());
System.assertEquals(3, exData.line());
System.assertEquals(1, exData.column());
}

@isTest
public static void testParseEnvironment() {
String result = ExceptionEmailParser.parseEnvironment(emailBody);
System.assertEquals('Sandbox', result);
}

@isTest
public static void testParseUserOrg() {
String result = ExceptionEmailParser.parseUserOrg(emailBody);
System.assertEquals('0050R000001t3Br/00D0R000000DUxZ', result);
}

@isTest
public static void testParseUserOrgTriggerSyntax() {
String emailBody = 'Apex script unhandled trigger exception by user/organization: 0050R000001t3Br/00D0R000000DUxZ';
String result = ExceptionEmailParser.parseUserOrg(emailBody);
System.assertEquals('0050R000001t3Br/00D0R000000DUxZ', result);
}

@isTest
public static void testParseUserOrgNewLineSyntax() {
String emailBody = 'Apex script unhandled exception by user/organization:\n0050R000001t3Br/00D0R000000DUxZ';
String result = ExceptionEmailParser.parseUserOrg(emailBody);
System.assertEquals('0050R000001t3Br/00D0R000000DUxZ', result);
}

@isTest
public static void testParseClassName() {
String result = ExceptionEmailParser.parseClassName(emailBody);
System.assertEquals('System.NullPointerException', result);
}

@isTest
public static void testParseClassNameDoubleColon() {
String emailBody = 'caused by: System.ListException: List index out of bounds: 2';
String result = ExceptionEmailParser.parseClassName(emailBody);
System.assertEquals('System.ListException', result);
}

@isTest
public static void testParseMessage() {
String result = ExceptionEmailParser.parseMessage(emailBody);
System.assertEquals('Attempt to de-reference a null object', result);
}

@isTest
public static void testParseMessageDoubleColon() {
String emailBody = 'caused by: System.ListException: List index out of bounds: 2';
String result = ExceptionEmailParser.parseMessage(emailBody);
System.assertEquals('List index out of bounds: 2', result);
}

@isTest
public static void testParseFileName() {
String result = ExceptionEmailParser.parseFileName(emailBody);
System.assertEquals('Trigger.ContactEx', result);
}

@isTest
public static void testParseContext() {
String result = ExceptionEmailParser.parseContext(emailBody);
System.assertEquals('ContactEx: execution of AfterInsert', result);
}

@isTest
public static void testParseLine() {
Integer result = ExceptionEmailParser.parseLine(emailBody);
System.assertEquals(3, result);
}

@isTest
public static void testParseColumn() {
Integer result = ExceptionEmailParser.parseColumn(emailBody);
System.assertEquals(1, result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="urn:metadata.tooling.soap.sforce.com" fqn="ExceptionEmailParserTest">
<apiVersion>45.0</apiVersion>
<status>Active</status>
</ApexClass>

0 comments on commit 2d2952c

Please sign in to comment.