From 487acce3ceffe9090c032c7c7ea34dbd233f57d5 Mon Sep 17 00:00:00 2001 From: Artur Moczulski Date: Sat, 18 May 2019 06:20:44 +0200 Subject: [PATCH] feat(exception email): add log method for email exception data Closes #2 --- force-app/main/default/classes/Notifier.cls | 12 ++++-- force-app/main/default/tests/NotifierTest.cls | 37 +++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/force-app/main/default/classes/Notifier.cls b/force-app/main/default/classes/Notifier.cls index 1df95f0..4363474 100644 --- a/force-app/main/default/classes/Notifier.cls +++ b/force-app/main/default/classes/Notifier.cls @@ -1,5 +1,7 @@ public with sharing class Notifier { + public static final String NAME = 'rollbar-sf-apex'; + public static final String VERSION = '1.0.0'; public Notifier(Config config) { @@ -19,6 +21,12 @@ public with sharing class Notifier return reportPayload(payload); } + public HttpResponse log(ExceptionData exData) + { + String payload = JSON.serialize(this.dataBuilder.buildPayload(exData)); + return reportPayload(payload); + } + private HttpResponse reportPayload(String payload) { HttpRequest request = new HttpRequest(); @@ -26,13 +34,9 @@ public with sharing class Notifier request.setMethod('POST'); request.setBody(payload); - System.debug(payload); - Http http = new Http(); HttpResponse response = http.send(request); - System.debug(response); - return response; } diff --git a/force-app/main/default/tests/NotifierTest.cls b/force-app/main/default/tests/NotifierTest.cls index a9b0ba0..b1bf836 100644 --- a/force-app/main/default/tests/NotifierTest.cls +++ b/force-app/main/default/tests/NotifierTest.cls @@ -51,4 +51,41 @@ public class NotifierTest { System.assertEquals(0, err); } + + @isTest + public static void testLogExceptionData(){ + Test.setMock(HttpCalloutMock.class, new RollbarApiCalloutMock()); + + Config config = new Config('foo', 'bar'); + + Notifier subject = new Notifier(config); + + 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); + + HttpResponse response = subject.log(exData); + + System.assertEquals(200, response.getStatusCode()); + + JSONParser parser = JSON.createParser(response.getBody()); + Integer err = null; + while (parser.nextToken() != null) { + if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && + (parser.getText() == 'err')) { + parser.nextToken(); + err = parser.getIntegerValue(); + } + } + + System.assertEquals(0, err); + } } \ No newline at end of file