Skip to content

Commit

Permalink
feat(exception email): add log method for 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 9180e3d commit 487acce
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
12 changes: 8 additions & 4 deletions force-app/main/default/classes/Notifier.cls
Original file line number Diff line number Diff line change
@@ -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)
{
Expand All @@ -19,20 +21,22 @@ 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();
request.setEndpoint(this.config.endpoint());
request.setMethod('POST');
request.setBody(payload);

System.debug(payload);

Http http = new Http();
HttpResponse response = http.send(request);

System.debug(response);

return response;
}

Expand Down
37 changes: 37 additions & 0 deletions force-app/main/default/tests/NotifierTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -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<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);

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);
}
}

0 comments on commit 487acce

Please sign in to comment.