From a5e3c914085e7523e410e59bc7ab29b666642f18 Mon Sep 17 00:00:00 2001 From: Artur Moczulski Date: Sat, 18 May 2019 06:13:42 +0200 Subject: [PATCH] feat(exception email): add building payloads from email exception data Closes #2 --- .../main/default/classes/DataBuilder.cls | 68 ++++++++++++++----- .../main/default/tests/DataBuilderTest.cls | 57 ++++++++++++++++ 2 files changed, 109 insertions(+), 16 deletions(-) diff --git a/force-app/main/default/classes/DataBuilder.cls b/force-app/main/default/classes/DataBuilder.cls index 568a064..e4a2a75 100644 --- a/force-app/main/default/classes/DataBuilder.cls +++ b/force-app/main/default/classes/DataBuilder.cls @@ -5,28 +5,31 @@ public with sharing class DataBuilder { public Map buildPayload(String level, String message) { - Map body = this.buildMessageBody(message); - - Map data = this.buildDataStructure(level, this.config.environment(), body); - - Map payload = this.buildPayloadStructure(data); - - return payload; + return buildPayloadStructure(level, this.buildMessageBody(message)); } public Map buildPayload(Exception exc) { - Map body = this.buildTraceBody(exc); + return buildPayloadStructure('error', this.buildTraceBody(exc)); + } - Map data = this.buildDataStructure('error', this.config.environment(), body); + public Map buildPayload(ExceptionData exData) + { + Map payload = buildPayloadStructure('error', this.buildTraceBody(exData)); + + Map custom = new Map(); + custom.put('context', exData.context()); + + Map data = (Map)payload.get('data'); + data.put('custom', custom); - Map payload = this.buildPayloadStructure(data); - return payload; } - private Map buildPayloadStructure(Map data) + private Map buildPayloadStructure(String level, Map body) { + Map data = this.buildDataStructure(level, this.config.environment(), body); + Map structure = new Map(); structure.put('access_token', this.config.accessToken()); structure.put('data', data); @@ -35,7 +38,12 @@ public with sharing class DataBuilder { private Map buildDataStructure(String level, String environment, Map body) { + Map notifierMap = new Map(); + notifierMap.put('name', Notifier.NAME); + notifierMap.put('version', Notifier.VERSION); + Map structure = new Map(); + structure.put('notifier', notifierMap); structure.put('level', level); structure.put('environment', environment); structure.put('framework', 'apex'); @@ -54,12 +62,28 @@ public with sharing class DataBuilder { return body; } - private Map buildTraceBody(Exception exc) + private Map buildTraceBody(ExceptionData exData) { - Map body = new Map(); + List> framesList = new List>(); - Map traceMap = new Map(); + Map frameMap = new Map(); + 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 excMap = new Map(); + excMap.put('class', exData.className()); + excMap.put('message', exData.message()); + + return buildTraceStructure(excMap, framesList); + } + private Map buildTraceBody(Exception exc) + { List> framesList = new List>(); String[] frames = exc.getStackTraceString().split('\n'); @@ -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 buildTraceStructure( + Map exceptionMap, + List> framesList + ) { + Map body = new Map(); + + Map traceMap = new Map(); + + traceMap.put('exception', exceptionMap); traceMap.put('frames', framesList); + body.put('trace', traceMap); return body; diff --git a/force-app/main/default/tests/DataBuilderTest.cls b/force-app/main/default/tests/DataBuilderTest.cls index 404a45c..2d167a9 100644 --- a/force-app/main/default/tests/DataBuilderTest.cls +++ b/force-app/main/default/tests/DataBuilderTest.cls @@ -10,6 +10,12 @@ public class DataBuilderTest Map result = subject.buildPayload('info', expected); Map data = (Map)result.get('data'); + + Map notifierMap = (Map)data.get('notifier'); + + System.assertEquals(Notifier.NAME, (String)notifierMap.get('name')); + System.assertEquals(Notifier.VERSION, (String)notifierMap.get('version')); + Map body = (Map)data.get('body'); System.assertEquals(expected, ((Map)body.get('message')).get('body')); @@ -30,6 +36,11 @@ public class DataBuilderTest Map result = subject.buildPayload(exc); Map data = (Map)result.get('data'); + + Map notifierMap = (Map)data.get('notifier'); + System.assertEquals(Notifier.NAME, (String)notifierMap.get('name')); + System.assertEquals(Notifier.VERSION, (String)notifierMap.get('version')); + Map body = (Map)data.get('body'); Map trace = (Map)body.get('trace'); Map excMap = (Map)trace.get('exception'); @@ -43,4 +54,50 @@ public class DataBuilderTest // Map frame = ((List>)trace.get('frames'))[0]; } } + + @isTest + static void testBuildExceptionDataPayload() + { + DataBuilder subject = new DataBuilder(new Config('foo', 'bar')); + + Map expected = new Map(); + 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 result = subject.buildPayload(exData); + + Map data = (Map)result.get('data'); + + Map custom = (Map)data.get('custom'); + + System.assertEquals((String)expected.get('context'), (String)custom.get('context')); + + Map notifierMap = (Map)data.get('notifier'); + System.assertEquals(Notifier.NAME, (String)notifierMap.get('name')); + System.assertEquals(Notifier.VERSION, (String)notifierMap.get('version')); + + Map body = (Map)data.get('body'); + Map trace = (Map)body.get('trace'); + + Map excMap = (Map)trace.get('exception'); + + System.assertEquals(expected.get('message'), excMap.get('message')); + System.assertEquals(expected.get('className'), excMap.get('class')); + + Map frameMap = ((List>)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')); + } }