Skip to content

Commit

Permalink
feat(exception email): add building payloads from email exception data
Browse files Browse the repository at this point in the history
Closes #2
  • Loading branch information
ArturMoczulski committed May 18, 2019
1 parent 2d2952c commit a5e3c91
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 16 deletions.
68 changes: 52 additions & 16 deletions force-app/main/default/classes/DataBuilder.cls
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,31 @@ public with sharing class DataBuilder {

public Map<String, Object> buildPayload(String level, String message)
{
Map<String, Object> body = this.buildMessageBody(message);

Map<String, Object> data = this.buildDataStructure(level, this.config.environment(), body);

Map<String, Object> payload = this.buildPayloadStructure(data);

return payload;
return buildPayloadStructure(level, this.buildMessageBody(message));
}

public Map<String, Object> buildPayload(Exception exc)
{
Map<String, Object> body = this.buildTraceBody(exc);
return buildPayloadStructure('error', this.buildTraceBody(exc));
}

Map<String, Object> data = this.buildDataStructure('error', this.config.environment(), body);
public Map<String, Object> buildPayload(ExceptionData exData)
{
Map<String, Object> payload = buildPayloadStructure('error', this.buildTraceBody(exData));

Map<String, Object> custom = new Map<String, Object>();
custom.put('context', exData.context());

Map<String, Object> data = (Map<String, Object>)payload.get('data');
data.put('custom', custom);

Map<String, Object> payload = this.buildPayloadStructure(data);

return payload;
}

private Map<String, Object> buildPayloadStructure(Map<String, Object> data)
private Map<String, Object> buildPayloadStructure(String level, Map<String, Object> body)
{
Map<String, Object> data = this.buildDataStructure(level, this.config.environment(), body);

Map<String, Object> structure = new Map<String, Object>();
structure.put('access_token', this.config.accessToken());
structure.put('data', data);
Expand All @@ -35,7 +38,12 @@ public with sharing class DataBuilder {

private Map<String, Object> buildDataStructure(String level, String environment, Map<String, Object> body)
{
Map<String, Object> notifierMap = new Map<String, Object>();
notifierMap.put('name', Notifier.NAME);
notifierMap.put('version', Notifier.VERSION);

Map<String, Object> structure = new Map<String, Object>();
structure.put('notifier', notifierMap);
structure.put('level', level);
structure.put('environment', environment);
structure.put('framework', 'apex');
Expand All @@ -54,12 +62,28 @@ public with sharing class DataBuilder {
return body;
}

private Map<String, Object> buildTraceBody(Exception exc)
private Map<String, Object> buildTraceBody(ExceptionData exData)
{
Map<String, Object> body = new Map<String, Object>();
List<Map<String, Object>> framesList = new List<Map<String, Object>>();

Map<String, Object> traceMap = new Map<String, Object>();
Map<String, Object> frameMap = new Map<String, Object>();
frameMap.put('filename', exData.fileName());
frameMap.put('class_name', exData.className());
frameMap.put('method', exData.fileName());
frameMap.put('lineno', exData.line());
frameMap.put('colno', exData.column());

framesList.add(frameMap);

Map<String, Object> excMap = new Map<String, Object>();
excMap.put('class', exData.className());
excMap.put('message', exData.message());

return buildTraceStructure(excMap, framesList);
}

private Map<String, Object> buildTraceBody(Exception exc)
{
List<Map<String, Object>> framesList = new List<Map<String, Object>>();

String[] frames = exc.getStackTraceString().split('\n');
Expand Down Expand Up @@ -90,8 +114,20 @@ public with sharing class DataBuilder {
excMap.put('class', exc.getTypeName());
excMap.put('message', exc.getMessage());

traceMap.put('exception', excMap);
return buildTraceStructure(excMap, framesList);
}

private Map<String, Object> buildTraceStructure(
Map<String, Object> exceptionMap,
List<Map<String, Object>> framesList
) {
Map<String, Object> body = new Map<String, Object>();

Map<String, Object> traceMap = new Map<String, Object>();

traceMap.put('exception', exceptionMap);
traceMap.put('frames', framesList);

body.put('trace', traceMap);

return body;
Expand Down
57 changes: 57 additions & 0 deletions force-app/main/default/tests/DataBuilderTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ public class DataBuilderTest
Map<String, Object> result = subject.buildPayload('info', expected);

Map<String, Object> data = (Map<String, Object>)result.get('data');

Map<String, Object> notifierMap = (Map<String, Object>)data.get('notifier');

System.assertEquals(Notifier.NAME, (String)notifierMap.get('name'));
System.assertEquals(Notifier.VERSION, (String)notifierMap.get('version'));

Map<String, Object> body = (Map<String, Object>)data.get('body');

System.assertEquals(expected, ((Map<String, Object>)body.get('message')).get('body'));
Expand All @@ -30,6 +36,11 @@ public class DataBuilderTest
Map<String, Object> result = subject.buildPayload(exc);

Map<String, Object> data = (Map<String, Object>)result.get('data');

Map<String, Object> notifierMap = (Map<String, Object>)data.get('notifier');
System.assertEquals(Notifier.NAME, (String)notifierMap.get('name'));
System.assertEquals(Notifier.VERSION, (String)notifierMap.get('version'));

Map<String, Object> body = (Map<String, Object>)data.get('body');
Map<String, Object> trace = (Map<String, Object>)body.get('trace');
Map<String, Object> excMap = (Map<String, Object>)trace.get('exception');
Expand All @@ -43,4 +54,50 @@ public class DataBuilderTest
// Map<String, Object> frame = ((List<Map<String, Object>>)trace.get('frames'))[0];
}
}

@isTest
static void testBuildExceptionDataPayload()
{
DataBuilder subject = new DataBuilder(new Config('foo', 'bar'));

Map<String, Object> expected = new Map<String, Object>();
expected.put('environment', 'Sandbox');
expected.put('organization', 'TestOrg');
expected.put('className', 'TestClass');
expected.put('message', 'Test exception message');
expected.put('fileName', 'Class.ClassWithExceptionThrown.someMethod');
expected.put('context', 'Exception context');
expected.put('line', 14);
expected.put('column', 12);

ExceptionData exData = ExceptionData.fromMap(expected);

Map<String, Object> result = subject.buildPayload(exData);

Map<String, Object> data = (Map<String, Object>)result.get('data');

Map<String, Object> custom = (Map<String, Object>)data.get('custom');

System.assertEquals((String)expected.get('context'), (String)custom.get('context'));

Map<String, Object> notifierMap = (Map<String, Object>)data.get('notifier');
System.assertEquals(Notifier.NAME, (String)notifierMap.get('name'));
System.assertEquals(Notifier.VERSION, (String)notifierMap.get('version'));

Map<String, Object> body = (Map<String, Object>)data.get('body');
Map<String, Object> trace = (Map<String, Object>)body.get('trace');

Map<String, Object> excMap = (Map<String, Object>)trace.get('exception');

System.assertEquals(expected.get('message'), excMap.get('message'));
System.assertEquals(expected.get('className'), excMap.get('class'));

Map<String, Object> frameMap = ((List<Map<String, Object>>)trace.get('frames'))[0];

System.assertEquals(expected.get('fileName'), frameMap.get('filename'));
System.assertEquals(expected.get('className'), frameMap.get('class_name'));
System.assertEquals(expected.get('fileName'), frameMap.get('method'));
System.assertEquals(expected.get('line'), frameMap.get('lineno'));
System.assertEquals(expected.get('column'), frameMap.get('colno'));
}
}

0 comments on commit a5e3c91

Please sign in to comment.