Skip to content

Commit

Permalink
Moved logger service into core
Browse files Browse the repository at this point in the history
Added ServiceUtils for logging an exception, stripping the context and then re-throwing it
Changed all services over to the new standard that includes the exception handling
  • Loading branch information
rob-baillie-ortoo committed Apr 19, 2022
1 parent 875b961 commit 5ae972a
Show file tree
Hide file tree
Showing 30 changed files with 153 additions and 26 deletions.
13 changes: 8 additions & 5 deletions TODO.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
Framework Catch-up
* Start-up performance might need looking at - looks like it has a linear startup time based on the number of services / domains / etc configured.
* Docs to write up:
* Exception standards
* Logging standards
* How to test logging
* Potentially ServiceUtils.logAndRethrow( e )

Logger
* Write up standards
Expand All @@ -13,6 +8,14 @@ Docs for Logger:
* Custom Setting - if it doesn't exist, will log everything.
* TestLoggerService
* ortoo_Exception - stripContext
* ServiceUtils.logAndRethrow
* All code other than the Contracts
* If you are returning a value, ensure you instantiate and set to a resonable default. Generally, it will never be used without being set.
* Logging standards
* How to test logging

Tests:
* Each of the services need a test for the try / catch - where possible


Licenses that are needed with the source code and binary:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,25 @@ public with sharing class SearchServiceImpl implements ISearchService
Contract.requires( window != null, 'search called with a null window' );
Contract.requires( orderBy != null, 'search called with a null orderBy' );

ISearchConfiguration searchConfiguration = buildSearchConfiguration( searchConfigurationType );
SearchResults searchResults = null;
try
{
ISearchConfiguration searchConfiguration = buildSearchConfiguration( searchConfigurationType );

SobjectType sobjectType = searchConfiguration.getBaseSobjectType();
SobjectType sobjectType = searchConfiguration.getBaseSobjectType();

SearchResults searchResults = ((ISearchSelector)Application.SELECTOR.newInstance( sObjectType ) )
.selectBySearchCriteria( searchConfiguration, criteria, window, orderBy );
searchResults = ((ISearchSelector)Application.SELECTOR.newInstance( sObjectType ) )
.selectBySearchCriteria( searchConfiguration, criteria, window, orderBy );

if ( searchResults.hasRecords() )
if ( searchResults.hasRecords() )
{
ISearchResultBuilder searchResultsBuilder = ((ISearchResultBuilder)Application.DOMAIN.newInstance( (List<Sobject>)searchResults.records ) );
searchResults.records = searchResultsBuilder.buildSearchResults( searchConfiguration );
}
}
catch ( Exception e )
{
ISearchResultBuilder searchResultsBuilder = ((ISearchResultBuilder)Application.DOMAIN.newInstance( (List<Sobject>)searchResults.records ) );
searchResults.records = searchResultsBuilder.buildSearchResults( searchConfiguration );
ServiceUtils.logAndRethrow( e );
}

Contract.ensures( searchResults != null, 'search attempted to return with a null searchResults' );
Expand All @@ -47,7 +55,17 @@ public with sharing class SearchServiceImpl implements ISearchService
public List<String> getSortableFields( Type searchConfigurationType )
{
Contract.requires( searchConfigurationType != null, 'getSortableFields called with a null searchConfigurationType' );
return buildSearchConfiguration( searchConfigurationType ).getSortableFields();

List<String> sortableFields = new List<String>();
try
{
return buildSearchConfiguration( searchConfigurationType ).getSortableFields();
}
catch ( Exception e )
{
ServiceUtils.logAndRethrow( e );
}
return sortableFields;
}

private ISearchConfiguration buildSearchConfiguration( Type searchConfigurationType )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Utility class that provides core capabilities related to Services
*
* @group Utils
*/
// TODO: test
public class ServiceUtils
{
/**
* Logs the given exception using the LoggerService, strips the context (if it's an Ortoo Exception)
* and then rethrows the exception
*
* @param Exception The exception to log
*/
public static void logAndRethrow( Exception exceptionToLog )
{
Contract.requires( exceptionToLog != null, 'logAndRethrow called with a null exceptionToLog' );

LoggerService.log( exceptionToLog );

if ( exceptionToLog instanceOf ortoo_Exception )
{
((ortoo_Exception)exceptionToLog).stripContexts();
}
throw exceptionToLog;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>52.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ public with sharing class LicensingServiceImpl implements ILicensingService
{
public Boolean productIsLicensed()
{
return ( licenseDetails != null || OrganizationService.inDevOrg() || OrganizationService.inSandbox() );
Boolean productIsLicensed = false;

try
{
productIsLicensed = ( licenseDetails != null || OrganizationService.inDevOrg() || OrganizationService.inSandbox() );
}
catch ( Exception e )
{
ServiceUtils.logAndRethrow( e );
}

return productIsLicensed;
}

private static PackageLicense licenseDetails
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,31 @@ public with sharing class LimitsServiceImpl implements ILimitsService {

private static final Integer DML_ROWS_PERCENTAGE_LIMIT = 90;

public static Integer getAvailableDmlRecordsHeadroom( ortoo_SobjectUnitOfWork uow ) {
public static Integer getAvailableDmlRecordsHeadroom( ortoo_SobjectUnitOfWork uow )
{
Contract.requires( uow != null, 'getAvailableDmlRecordsHeadroom called with a null uow' );

Integer futureDmlRows = uow.getNumberOfPendingDmlRows();
Integer currentDmlRows = Limits.getDmlRows();
Integer headroom = 0;

Integer dmlRowsLimit = getEffectiveDmlRowsLimit();
try
{
Integer futureDmlRows = uow.getNumberOfPendingDmlRows();
Integer currentDmlRows = Limits.getDmlRows();

return dmlRowsLimit - ( currentDmlRows + futureDmlRows );
Integer dmlRowsLimit = getEffectiveDmlRowsLimit();

headroom = dmlRowsLimit - ( currentDmlRows + futureDmlRows );
}
catch ( Exception e )
{
ServiceUtils.logAndRethrow( e );
}

return headroom;
}

private static Integer getEffectiveDmlRowsLimit() {
return Integer.valueOf( Limits.getLimitDmlRows() * DML_ROWS_PERCENTAGE_LIMIT / 100 );
private static Integer getEffectiveDmlRowsLimit()
{
return Integer.valueOf( Limits.getLimitDmlRows() * DML_ROWS_PERCENTAGE_LIMIT / 100 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,44 @@ public with sharing class OrganizationServiceImpl implements IOrganizationServic
{
public Boolean inDevOrg()
{
return org.OrganizationType == 'Developer Edition';
Boolean inDevOrg = false;
try
{
inDevOrg = org.OrganizationType == 'Developer Edition';
}
catch ( Exception e )
{
ServiceUtils.logAndRethrow( e );
}
return inDevOrg;
}

public Boolean inSandbox()
{
return org.IsSandbox;
Boolean inSandbox = false;
try
{
inSandbox = org.IsSandbox;
}
catch ( Exception e )
{
ServiceUtils.logAndRethrow( e );
}
return inSandbox;
}

public String getOrgEdition()
{
return org.OrganizationType;
String orgEdition;
try
{
orgEdition = org.OrganizationType;
}
catch ( Exception e )
{
ServiceUtils.logAndRethrow( e );
}
return orgEdition;
}

private static Organization org
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@ public with sharing class PermissionsServiceImpl implements IPermissionsService
*/
public Boolean hasAccessToCorePlatformCache()
{
return hasCustomPermission( 'ProcessesCanAccessCache' );
Boolean hasAccessToCorePlatformCache = false;

try
{
hasAccessToCorePlatformCache = hasCustomPermission( 'ProcessesCanAccessCache' );
}
catch ( Exception e )
{
ServiceUtils.logAndRethrow( e );
}

return hasAccessToCorePlatformCache;
}

/**
Expand All @@ -18,6 +29,17 @@ public with sharing class PermissionsServiceImpl implements IPermissionsService
*/
private Boolean hasCustomPermission( String customPermissionName )
{
return FeatureManagement.checkPermission( customPermissionName );
Boolean hasCustomPermission = false;

try
{
hasCustomPermission = FeatureManagement.checkPermission( customPermissionName );
}
catch ( Exception e )
{
ServiceUtils.logAndRethrow( e );
}

return hasCustomPermission;
}
}

0 comments on commit 5ae972a

Please sign in to comment.