Skip to content

Commit

Permalink
Merge pull request #22 from rollbar/wj-basic-config-options
Browse files Browse the repository at this point in the history
Basic config options: Environment and enable/disable options
  • Loading branch information
waltjones authored Jul 29, 2020
2 parents 883f5f9 + ff88fc1 commit 74e2061
Show file tree
Hide file tree
Showing 22 changed files with 338 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.sfdx/
.vscode/
.vscode/
deploy/
7 changes: 6 additions & 1 deletion force-app/main/default/classes/Config.cls
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ public with sharing class Config {
throw new RollbarNotInitializedException();
}
this.accessToken = accessToken;
this.environment = environment;

if (environment == null || environment == '') {
this.environment = UserInfo.getOrganizationName();
} else {
this.environment = environment;
}
}

public Config() {
Expand Down
71 changes: 54 additions & 17 deletions force-app/main/default/classes/DataBuilder.cls
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,23 @@ public with sharing class DataBuilder {
}

private Map<String, Object> buildTraceBody(Exception exc)
{
String stackTraceString = exc.getStackTraceString();
String typeName = exc.getTypeName();
String message = exc.getMessage();

return buildTraceBody(stackTraceString, typeName, message);

}

@testVisible
private Map<String, Object> buildTraceBody(String stackTraceString, String typeName, String message)
{
List<Map<String, Object>> framesList = new List<Map<String, Object>>();

String[] frames = exc.getStackTraceString().split('\n');
String[] frames = stackTraceString.split('\n');
for (String frameStr : frames) {
if (frameStr == '()') {
if (frameStr == '()' || frameStr == '') {
continue;
} else if (frameStr.toLowerCase() == 'caused by') {
break;
Expand All @@ -144,32 +155,58 @@ public with sharing class DataBuilder {
Map<String, Object> frameMap = new Map<String, Object>();
frameMap.put('filename', frameStr);

String className = frameStr.split(':')[0];
String methodName = '';
if (className != 'AnonymousBlock') {
className = className.split('\\.')[1];
methodName = frameStr.split(':')[0].split('\\.')[2];
String[] location = frameStr.split(':')[0].split('\\.');
Integer locationLength = location.size();
String className = '(unknown)';
String methodName = '(unknown)';
switch on location[0] {
when 'Class' {
if (locationLength > 1) {
className = location[1];
}
if (locationLength > 2) {
methodName = location[2];
}
}
when 'Trigger' {
className = 'Trigger';
if (locationLength > 1) {
methodName = location[1];
}
}
when else { // e.g. "AnonymousBlock"
className = 'Unknown';
methodName = location[0];
}
}

frameMap.put('class_name', className);
frameMap.put('method', methodName);

Pattern linePattern = Pattern.compile('line (\\d+)');
Matcher lineMatcher = linePattern.matcher(frameStr);
lineMatcher.find();
frameMap.put('lineno', Integer.valueOf(lineMatcher.group(1)));
try {
Pattern linePattern = Pattern.compile('line (\\d+)');
Matcher lineMatcher = linePattern.matcher(frameStr);
lineMatcher.find();
frameMap.put('lineno', Integer.valueOf(lineMatcher.group(1)));
} catch (StringException e) {
frameMap.put('lineno', '(unknown)');
}

Pattern colPattern = Pattern.compile('column (\\d+)');
Matcher colMatcher = colPattern.matcher(frameStr);
colMatcher.find();
frameMap.put('colno', Integer.valueOf(colMatcher.group(1)));
try {
Pattern colPattern = Pattern.compile('column (\\d+)');
Matcher colMatcher = colPattern.matcher(frameStr);
colMatcher.find();
frameMap.put('colno', Integer.valueOf(colMatcher.group(1)));
} catch (StringException e) {
frameMap.put('colno', '(unknown)');
}

framesList.add(frameMap);
}

Map<String, Object> excMap = new Map<String, Object>();
excMap.put('class', exc.getTypeName());
excMap.put('message', exc.getMessage());
excMap.put('class', typeName);
excMap.put('message', message);

return buildTraceStructure(excMap, framesList);
}
Expand Down
6 changes: 6 additions & 0 deletions force-app/main/default/classes/Notifier.cls
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public with sharing class Notifier

public HttpResponse send(Map<String, Object> payload, SendMethod method)
{
if(!RollbarSettings__c.getOrgDefaults().SendReports__c) { return null; }

return methodSend(JSON.serialize(payload), method);
}

Expand Down Expand Up @@ -76,6 +78,10 @@ public with sharing class Notifier
return response;
}

public Config config() {
return config;
}

private Config config;
private DataBuilder dataBuilder;
}
10 changes: 8 additions & 2 deletions force-app/main/default/classes/Rollbar.cls
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ global with sharing class Rollbar {

public static Rollbar init()
{
RollbarSettings__c settings = RollbarSettings__c.getOrgDefaults();

return Rollbar.init(
RollbarSettings__c.getOrgDefaults().AccessToken__c,
UserInfo.getOrganizationName()
settings.AccessToken__c,
settings.Environment__c
);
}

Expand Down Expand Up @@ -102,6 +104,10 @@ global with sharing class Rollbar {
return notifier;
}

public Config config() {
return config;
}

private static Rollbar instance = null;
private Config config = null;
private Notifier notifier = null;
Expand Down
36 changes: 34 additions & 2 deletions force-app/main/default/classes/RollbarConfigureController.cls
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
public with sharing class RollbarConfigureController {

protected RollbarSettings__c settings { get; set; }

public Boolean reportUncaughtExceptions { get; set; }
public Boolean reportFlowErrors { get; set; }
public Boolean reportUnknownEmailType { get; set; }
public Boolean sendReports { get; set; }
public String environment { get; set; }
public String accessToken {
get {
return this.settings.AccessToken__c;
Expand All @@ -21,6 +27,11 @@ public with sharing class RollbarConfigureController {

public RollbarConfigureController() {
this.settings = RollbarSettings__c.getOrgDefaults();
this.reportUncaughtExceptions = this.settings.ReportUncaughtExceptions__c;
this.reportFlowErrors = this.settings.ReportFlowErrors__c;
this.reportUnknownEmailType = this.settings.ReportUnknownEmailType__c;
this.sendReports = this.settings.SendReports__c;
this.environment = this.settings.Environment__c;
updateConfigState();
}

Expand All @@ -35,12 +46,12 @@ public with sharing class RollbarConfigureController {
return null;
}

public Pagereference save()
public Pagereference verifyToken()
{
String message;

try {
Config config = new Config(this.accessToken, UserInfo.getOrganizationName());
Config config = new Config(this.accessToken, this.settings.Environment__c);
message = RollbarInstaller.verifyToken(config);
} catch (RollbarNotInitializedException e) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'Provided access token is incorrect.'));
Expand All @@ -49,6 +60,27 @@ public with sharing class RollbarConfigureController {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, message));
}
this.settings = RollbarSettings__c.getOrgDefaults();
this.reportUncaughtExceptions = this.settings.ReportUncaughtExceptions__c;
this.reportFlowErrors = this.settings.ReportFlowErrors__c;
this.reportUnknownEmailType = this.settings.ReportUnknownEmailType__c;
this.sendReports = this.settings.SendReports__c;
this.environment = this.settings.Environment__c;
updateConfigState();
return null;
}

public Pagereference updateSettings()
{
String message;

this.settings = RollbarSettings__c.getOrgDefaults();
this.settings.ReportUncaughtExceptions__c = this.reportUncaughtExceptions;
this.settings.ReportFlowErrors__c = this.reportFlowErrors;
this.settings.ReportUnknownEmailType__c = this.reportUnknownEmailType;
this.settings.SendReports__c = this.sendReports;
this.settings.Environment__c = this.environment;
upsert this.settings;

updateConfigState();
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,30 @@ global class RollbarExceptionEmailHandler implements Messaging.InboundEmailHandl
}

private void parseAndSend(Messaging.InboundEmail email) {
RollbarSettings__c settings = RollbarSettings__c.getOrgDefaults();
String emailBody = '';

Rollbar.init();

// The fromName field is the most reliable known way to differentiate
// Exception and Flow email types.
if (email.fromName == ExceptionEmailParser.fromName()) {
if (!settings.ReportUncaughtExceptions__c) { return; }

emailBody = email.plainTextBody;
ExceptionData exData = ExceptionEmailParser.parse(emailBody);

Rollbar.log(exData);
} else if (email.fromName == FlowEmailParser.fromName()) {
if (!settings.ReportFlowErrors__c) { return; }

emailBody = email.htmlBody;
List<Telemetry> telemetry = FlowEmailParser.parseTelemetry(emailBody);

Rollbar.log('error', email.subject, null, telemetry, SendMethod.SYNC);
} else {
if (!settings.ReportUnknownEmailType__c) { return; }

// Unknown email type.
// TODO: Use emailBody and fromName in notifier.diagnostic

Expand Down
10 changes: 4 additions & 6 deletions force-app/main/default/classes/RollbarInstaller.cls
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,11 @@ public with sharing class RollbarInstaller {
{
if (!rollbarApiEndpointAllowed(apiDomain)) {
MetadataService.createRemoteSiteSetting('RollbarAPI', apiDomain);

settings.SalesforceApiEnabled__c = metadataApiAllowed();
settings.RollbarApiEnabled__c = rollbarApiEndpointAllowed(apiDomain);
settings.RollbarNetworkAccess__c = rollbarPingSuccessful();
} else if (!settings.RollbarNetworkAccess__c) {
settings.RollbarNetworkAccess__c = rollbarPingSuccessful();
}

settings.SalesforceApiEnabled__c = metadataApiAllowed();
settings.RollbarApiEnabled__c = rollbarApiEndpointAllowed(apiDomain);
settings.RollbarNetworkAccess__c = rollbarPingSuccessful();
}

public static void enableApexNotificationsForwarding(RollbarSettings__c settings)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Environment__c</fullName>
<externalId>false</externalId>
<label>Environment</label>
<length>80</length>
<required>false</required>
<trackTrending>false</trackTrending>
<type>Text</type>
<unique>false</unique>
</CustomField>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>ReportFlowErrors__c</fullName>
<defaultValue>true</defaultValue>
<externalId>false</externalId>
<label>Report Flow Errors</label>
<trackTrending>false</trackTrending>
<type>Checkbox</type>
</CustomField>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>ReportUncaughtExceptions__c</fullName>
<defaultValue>true</defaultValue>
<externalId>false</externalId>
<label>Report Uncaught Exceptions</label>
<trackTrending>false</trackTrending>
<type>Checkbox</type>
</CustomField>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>ReportUnknownEmailType__c</fullName>
<defaultValue>true</defaultValue>
<externalId>false</externalId>
<label>Report Unknown Error Emails</label>
<trackTrending>false</trackTrending>
<type>Checkbox</type>
</CustomField>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>SendReports__c</fullName>
<defaultValue>true</defaultValue>
<externalId>false</externalId>
<label>Send Reports</label>
<trackTrending>false</trackTrending>
<type>Checkbox</type>
</CustomField>
27 changes: 24 additions & 3 deletions force-app/main/default/pages/RollbarConfigure.page
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
<apex:inputText value="{!accessToken}" id="rollbarAccessToken" html-placeholder="Rollbar Access Token" />
</p>
<div class="next-step">
<apex:commandButton action="{!save}" value="Save and Verify" id="rollbarConfigure" reRender="setup" />
<apex:commandButton action="{!verifyToken}" value="Save and Verify" id="rollbarConfigure" reRender="setup" />
</div>
</apex:form>
</apex:outputPanel>
Expand Down Expand Up @@ -205,13 +205,34 @@
<apex:outputPanel rendered="{!configState == 'ConfigState.COMPLETE'}" layout="block">
<div class="section">
<div class="section">
<h4>Update Rollbar Token</h4>
<h4>Update Settings</h4>
<apex:form>
<p>
<apex:inputCheckbox value="{!reportUncaughtExceptions}" label="Report Uncaught Exceptions" />
<span>Report Uncaught Exceptions</span>
</p>
<p>
<apex:inputCheckbox value="{!reportFlowErrors}" label="Report Flow Errors" />
<span>Report Flow Errors</span>
</p>
<p>
<apex:inputCheckbox value="{!reportUnknownEmailType}" label="Report Unknown Error Emails" />
<span>Report Unknown Error Emails</span>
</p>
<p>
<apex:inputCheckbox value="{!sendReports}" label="Send Events To Rollbar" />
<span>Send Events To Rollbar</span>
</p>
<p>
<h4>Environment</h4>
<apex:inputText value="{!environment}" id="rollbarEnvironment" html-placeholder="Rollbar Environment" />
</p>
<p class="center">
<h4>Access Token</h4>
<apex:inputText value="{!accessToken}" id="rollbarAccessToken" html-placeholder="Rollbar Access Token" />
</p>
<div class="next-step">
<apex:commandButton action="{!save}" value="Update and Verify" id="rollbarConfigure" reRender="setup" />
<apex:commandButton action="{!updateSettings}" value="Update" id="rollbarConfigure" reRender="setup" />
</div>
</apex:form>
</div>
Expand Down
Loading

0 comments on commit 74e2061

Please sign in to comment.