Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for logging Flow fault messages #105

Merged
merged 4 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public without sharing class LogEntryEventHandler {
List<Log__c> logs = [
SELECT Id, ApiReleaseNumber__c, ApiReleaseVersion__c
FROM Log__c
WHERE CreatedDate >= :fourHoursAgo AND DAY_ONLY(CreatedDate) = TODAY AND ApiReleaseNumber__c != NULL
WHERE CreatedDate >= :fourHoursAgo AND CreatedDate = TODAY AND ApiReleaseNumber__c != NULL
ORDER BY CreatedDate DESC
LIMIT 1
];
Expand Down Expand Up @@ -287,7 +287,13 @@ public without sharing class LogEntryEventHandler {
System.debug('statusApiResponse==' + statusApiResponse);

List<Log__c> logsToUpdate = new List<Log__c>();
for (Log__c log : [SELECT Id FROM Log__c WHERE DAY_ONLY(CreatedDate) = TODAY AND ApiReleaseNumber__c = NULL ORDER BY CreatedDate LIMIT :Limits.getLimitDmlRows()]) {
for (Log__c log : [
SELECT Id
FROM Log__c
WHERE CreatedDate = TODAY AND ApiReleaseNumber__c = NULL
ORDER BY CreatedDate
LIMIT :Limits.getLimitDmlRows()
]) {
log.ApiReleaseNumber__c = statusApiResponse.releaseNumber;
log.ApiReleaseVersion__c = statusApiResponse.releaseVersion;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
<valueSet>
<valueSetDefinition>
<sorted>true</sorted>
<value>
<fullName>Flow.FaultError</fullName>
<default>false</default>
<label>Flow.FaultError</label>
</value>
<value>
<fullName>System.AsyncException</fullName>
<default>false</default>
Expand Down
28 changes: 24 additions & 4 deletions nebula-logger/main/logger-engine/classes/FlowLogEntry.cls
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ global inherited sharing class FlowLogEntry {
@InvocableVariable(required=true label='Log Entry Message')
global String message;

/**
* @description Optionally log a Flow fault error message
*/
@InvocableVariable(required=false label='(Optional) Flow Fault Error Message')
global String faultMessage;

/**
* @description Optionally choose to save any pending log entries.
*/
Expand All @@ -40,7 +46,7 @@ global inherited sharing class FlowLogEntry {
* @description Optionally specify a logging level - the default is 'DEBUG'
*/
@InvocableVariable(required=false label='(Optional) Logging Level')
global String loggingLevelName = 'DEBUG';
global String loggingLevelName;

/**
* @description Optionally provide a list of topics to dynamically assign to the log entry
Expand All @@ -59,6 +65,15 @@ global inherited sharing class FlowLogEntry {
global static List<String> addFlowEntries(List<FlowLogEntry> flowLogEntries) {
Boolean saveLog = false;
for (FlowLogEntry flowLogEntry : flowLogEntries) {
// Set the logging level if it's blank
jongpie marked this conversation as resolved.
Show resolved Hide resolved
if (String.isBlank(flowLogEntry.loggingLevelName)) {
if (String.isNotBlank(flowLogEntry.faultMessage)) {
flowLogEntry.loggingLevelName = 'ERROR';
} else {
flowLogEntry.loggingLevelName = 'DEBUG';
}
}

LoggingLevel loggingLevel = Logger.getLoggingLevel(flowLogEntry.loggingLevelName);

LogEntryEventBuilder logEntryEventBuilder = Logger.newEntry(loggingLevel, flowLogEntry.message)
Expand All @@ -67,20 +82,25 @@ global inherited sharing class FlowLogEntry {

LogEntryEvent__e logEntryEvent = logEntryEventBuilder.getLogEntryEvent();

if (logEntryEvent == null) {
if (logEntryEventBuilder.shouldSave() == false) {
continue;
}

logEntryEvent.OriginLocation__c = flowLogEntry.flowName;
logEntryEvent.OriginType__c = 'Flow';
logEntryEvent.Timestamp__c = flowLogEntry.timestamp;

if (flowLogEntry.saveLog) {
if (String.isNotBlank(flowLogEntry.faultMessage)) {
logEntryEvent.ExceptionMessage__c = flowLogEntry.faultMessage;
logEntryEvent.ExceptionType__c = 'Flow.FaultError';
}

if (flowLogEntry.saveLog == true) {
saveLog = flowLogEntry.saveLog;
}
}

if (saveLog) {
if (saveLog == true) {
Logger.saveLog();
}

Expand Down
28 changes: 24 additions & 4 deletions nebula-logger/main/logger-engine/classes/FlowRecordLogEntry.cls
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ global inherited sharing class FlowRecordLogEntry {
@InvocableVariable(required=true label='Record')
global SObject record;

/**
* @description Optionally log a Flow fault error message
*/
@InvocableVariable(required=false label='(Optional) Flow Fault Error Message')
global String faultMessage;
/**
* @description Optionally choose to save any pending log entries
*/
Expand All @@ -40,7 +45,7 @@ global inherited sharing class FlowRecordLogEntry {
* @description Optionally specify a logging level - the default is 'DEBUG'
*/
@InvocableVariable(required=false label='(Optional) Logging Level')
global String loggingLevelName = 'DEBUG';
global String loggingLevelName;

/**
* @description Optionally provide a list of topics to dynamically assign to the log entry
Expand All @@ -63,6 +68,15 @@ global inherited sharing class FlowRecordLogEntry {
global static List<String> addFlowRecordEntries(List<FlowRecordLogEntry> flowRecordLogEntries) {
Boolean saveLog = false;
for (FlowRecordLogEntry flowRecordLogEntry : flowRecordLogEntries) {
// Set the logging level if it's blank
if (String.isBlank(flowRecordLogEntry.loggingLevelName)) {
if (String.isNotBlank(flowRecordLogEntry.faultMessage)) {
flowRecordLogEntry.loggingLevelName = 'ERROR';
} else {
flowRecordLogEntry.loggingLevelName = 'DEBUG';
}
}

LoggingLevel loggingLevel = Logger.getLoggingLevel(flowRecordLogEntry.loggingLevelName);

LogEntryEventBuilder logEntryEventBuilder = Logger.newEntry(loggingLevel, flowRecordLogEntry.message)
Expand All @@ -71,19 +85,25 @@ global inherited sharing class FlowRecordLogEntry {

LogEntryEvent__e logEntryEvent = logEntryEventBuilder.getLogEntryEvent();

if (logEntryEvent == null) {
if (logEntryEventBuilder.shouldSave() == false) {
continue;
}

logEntryEvent.OriginLocation__c = flowRecordLogEntry.flowName;
logEntryEvent.OriginType__c = 'Flow';
logEntryEvent.Timestamp__c = flowRecordLogEntry.timestamp;

if (flowRecordLogEntry.saveLog) {
if (String.isNotBlank(flowRecordLogEntry.faultMessage)) {
logEntryEvent.ExceptionMessage__c = flowRecordLogEntry.faultMessage;
logEntryEvent.ExceptionType__c = 'Flow.FaultError';
}

if (flowRecordLogEntry.saveLog == true) {
saveLog = flowRecordLogEntry.saveLog;
}
}
if (saveLog) {

if (saveLog == true) {
Logger.saveLog();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,6 @@ private class LogEntryEventHandler_Tests {
String transactionId = '123-456-789-0';
LogEntryEvent__e logEntryEvent = new LogEntryEvent__e(Message__c = 'my message', Timestamp__c = System.now(), TransactionId__c = transactionId);


Database.SaveResult saveResult;

Test.startTest();
Expand Down
54 changes: 53 additions & 1 deletion nebula-logger/tests/logger-engine/classes/FlowLogEntry_Tests.cls
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ private class FlowLogEntry_Tests {
flowEntry.flowName = 'my test message';
flowEntry.message = 'MyFlowOrProcessBuilder';
flowEntry.saveLog = true;
flowEntry.loggingLevelName = 'DEBUG';

return flowEntry;
}
Expand Down Expand Up @@ -68,6 +67,59 @@ private class FlowLogEntry_Tests {
System.assertEquals(0, logEntries.size());
}

@isTest
static void it_should_use_debug_as_default_level_when_faultMessage_is_null() {
String userLoggingLevel = 'FINEST';

LoggerSettings__c loggerSettings = LoggerSettings__c.getInstance();
loggerSettings.LoggingLevel__c = userLoggingLevel;
update loggerSettings;

Test.startTest();

FlowLogEntry flowEntry = createFlowLogEntry();
System.assertEquals(null, flowEntry.loggingLevelName);

FlowLogEntry.addFlowEntries(new List<FlowLogEntry>{ flowEntry });
Logger.saveLog();

Test.stopTest();

LogEntry__c logEntry = [SELECT Id, LoggingLevel__c, Message__c, OriginType__c, OriginLocation__c FROM LogEntry__c ORDER BY CreatedDate LIMIT 1];
System.assertEquals('DEBUG', logEntry.LoggingLevel__c);
System.assertEquals(flowEntry.message, logEntry.Message__c);
System.assertEquals('Flow', logEntry.OriginType__c);
System.assertEquals(flowEntry.flowName, logEntry.OriginLocation__c);
}

@isTest
static void it_should_use_error_as_default_level_when_faultMessage_is_not_null() {
String userLoggingLevel = 'FINEST';

LoggerSettings__c loggerSettings = LoggerSettings__c.getInstance();
loggerSettings.LoggingLevel__c = userLoggingLevel;
update loggerSettings;

Test.startTest();

FlowLogEntry flowEntry = createFlowLogEntry();
flowEntry.faultMessage = 'Whoops, a Flow error has occurred.';
System.assertEquals(null, flowEntry.loggingLevelName);

FlowLogEntry.addFlowEntries(new List<FlowLogEntry>{ flowEntry });
Logger.saveLog();

Test.stopTest();

LogEntry__c logEntry = [SELECT Id, ExceptionMessage__c, ExceptionType__c, LoggingLevel__c, Message__c, OriginType__c, OriginLocation__c FROM LogEntry__c ORDER BY CreatedDate LIMIT 1];
System.assertEquals('ERROR', logEntry.LoggingLevel__c);
System.assertEquals(flowEntry.faultMessage, logEntry.ExceptionMessage__c);
System.assertEquals('Flow.FaultError', logEntry.ExceptionType__c);
System.assertEquals(flowEntry.message, logEntry.Message__c);
System.assertEquals('Flow', logEntry.OriginType__c);
System.assertEquals(flowEntry.flowName, logEntry.OriginLocation__c);
}

@isTest
static void it_should_set_related_record_id_when_id_parameter_is_used() {
String userLoggingLevel = 'FINEST';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ private class FlowRecordLogEntry_Tests {
flowRecordEntry.flowName = 'my test message';
flowRecordEntry.message = 'MyFlowOrProcessBuilder';
flowRecordEntry.saveLog = true;
flowRecordEntry.loggingLevelName = 'DEBUG';

return flowRecordEntry;
}
Expand Down Expand Up @@ -72,4 +71,58 @@ private class FlowRecordLogEntry_Tests {
List<LogEntry__c> logEntries = [SELECT Id FROM LogEntry__c];
System.assertEquals(0, logEntries.size());
}

@isTest
static void it_should_use_debug_as_default_level_when_faultMessage_is_null() {
String userLoggingLevel = 'FINEST';

LoggerSettings__c loggerSettings = LoggerSettings__c.getInstance();
loggerSettings.LoggingLevel__c = userLoggingLevel;
update loggerSettings;

Test.startTest();

FlowRecordLogEntry flowEntry = createFlowRecordLogEntry();
System.assertEquals(null, flowEntry.loggingLevelName);

FlowRecordLogEntry.addFlowRecordEntries(new List<FlowRecordLogEntry>{ flowEntry });
Logger.saveLog();

Test.stopTest();

LogEntry__c logEntry = [SELECT Id, LoggingLevel__c, Message__c, OriginType__c, OriginLocation__c FROM LogEntry__c ORDER BY CreatedDate LIMIT 1];
System.assertEquals('DEBUG', logEntry.LoggingLevel__c);
System.assertEquals(flowEntry.message, logEntry.Message__c);
System.assertEquals('Flow', logEntry.OriginType__c);
System.assertEquals(flowEntry.flowName, logEntry.OriginLocation__c);
}

@isTest
static void it_should_use_error_as_default_level_when_faultMessage_is_not_null() {
String userLoggingLevel = 'FINEST';

LoggerSettings__c loggerSettings = LoggerSettings__c.getInstance();
loggerSettings.LoggingLevel__c = userLoggingLevel;
update loggerSettings;

Test.startTest();

FlowRecordLogEntry flowEntry = createFlowRecordLogEntry();
flowEntry.faultMessage = 'Whoops, a Flow error has occurred.';
System.assertEquals(null, flowEntry.loggingLevelName);

FlowRecordLogEntry.addFlowRecordEntries(new List<FlowRecordLogEntry>{ flowEntry });
Logger.saveLog();

Test.stopTest();

LogEntry__c logEntry = [SELECT Id, ExceptionMessage__c, ExceptionType__c, LoggingLevel__c, Message__c, OriginType__c, OriginLocation__c FROM LogEntry__c ORDER BY CreatedDate LIMIT 1];
System.assertEquals('ERROR', logEntry.LoggingLevel__c);
System.assertEquals(flowEntry.faultMessage, logEntry.ExceptionMessage__c);
System.assertEquals('Flow.FaultError', logEntry.ExceptionType__c);
System.assertEquals(flowEntry.message, logEntry.Message__c);
System.assertEquals('Flow', logEntry.OriginType__c);
System.assertEquals(flowEntry.flowName, logEntry.OriginLocation__c);
}

}