From 3d11521cd0e07d40c129f64d32eb2416e8679c00 Mon Sep 17 00:00:00 2001 From: Robert Baillie Date: Thu, 9 Dec 2021 10:29:27 +0000 Subject: [PATCH] Added constant based error codes for DML violations --- .../default/classes/FrameworkErrorCodes.cls | 6 +++++ framework/main/default/classes/SecureDml.cls | 22 ++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/framework/default/ortoo-core/default/classes/FrameworkErrorCodes.cls b/framework/default/ortoo-core/default/classes/FrameworkErrorCodes.cls index 470b25b8d5b..70fc8349e9e 100644 --- a/framework/default/ortoo-core/default/classes/FrameworkErrorCodes.cls +++ b/framework/default/ortoo-core/default/classes/FrameworkErrorCodes.cls @@ -21,4 +21,10 @@ public inherited sharing class FrameworkErrorCodes { public final static String CONFIGURATION_WITH_INVALID_TYPE = 'APP-00001'; public final static String CONFIGURATION_WITH_INVALID_CLASS = 'APP-00002'; public final static String CONFIGURATION_WITH_INVALID_SOBJECT_TYPE = 'APP-00003'; + + public final static String DML_ON_INACCESSIBLE_FIELDS = '00000'; + public final static String DML_INSERT_NOT_ALLOWED = '00001'; + public final static String DML_UPDATE_NOT_ALLOWED = '00002'; + public final static String DML_DELETE_NOT_ALLOWED = '00003'; + public final static String DML_PUBLISH_NOT_ALLOWED = '00004'; } \ No newline at end of file diff --git a/framework/main/default/classes/SecureDml.cls b/framework/main/default/classes/SecureDml.cls index ce4518544d8..869d95607c2 100644 --- a/framework/main/default/classes/SecureDml.cls +++ b/framework/main/default/classes/SecureDml.cls @@ -15,7 +15,13 @@ */ public inherited sharing class SecureDml extends fflib_SobjectUnitOfWork.SimpleDML implements fflib_SobjectUnitOfWork.IDml { - public inherited sharing class SecureDmlException extends ortoo_Exception {} // TODO: add error code prefix + public inherited sharing class SecureDmlException extends ortoo_Exception + { + protected virtual override String resolveErrorCode( String errorCode ) + { + return 'DML-' + errorCode; + } + } /** * Interface that defines a handler for if and when a CUD violation occurs. @@ -240,7 +246,7 @@ public inherited sharing class SecureDml extends fflib_SobjectUnitOfWork.SimpleD SobjectType type = SobjectUtils.getSobjectType( objList[0] ); - if ( shouldCheckCud( type ) && ! SobjectUtils.isCreateable( objList[0] ) ) // SobjectUtils.isCreateable( objList[0] ) + if ( shouldCheckCud( type ) && ! SobjectUtils.isCreateable( objList[0] ) ) { cudViolationHandler.handleUnableToPublishEvents( objList ); return; @@ -427,7 +433,7 @@ public inherited sharing class SecureDml extends fflib_SobjectUnitOfWork.SimpleD */ public void handleUnableToInsertRecords( List objList ) { - throwUnableException( '00001', 'Attempted to insert {0} records without the required permission', objList ); // TODO: label + throwUnableException( FrameworkErrorCodes.DML_INSERT_NOT_ALLOWED, 'Attempted to insert {0} records without the required permission', objList ); // TODO: label } /** @@ -437,7 +443,7 @@ public inherited sharing class SecureDml extends fflib_SobjectUnitOfWork.SimpleD */ public void handleUnableToUpdateRecords( List objList ) { - throwUnableException( '00002', 'Attempted to update {0} records without the required permission', objList ); // TODO: label + throwUnableException( FrameworkErrorCodes.DML_UPDATE_NOT_ALLOWED, 'Attempted to update {0} records without the required permission', objList ); // TODO: label } /** @@ -447,7 +453,7 @@ public inherited sharing class SecureDml extends fflib_SobjectUnitOfWork.SimpleD */ public void handleUnableToDeleteRecords( List objList ) { - throwUnableException( '00003', 'Attempted to delete {0} records without the required permission', objList ); // TODO: label + throwUnableException( FrameworkErrorCodes.DML_DELETE_NOT_ALLOWED, 'Attempted to delete {0} records without the required permission', objList ); // TODO: label } /** @@ -457,14 +463,14 @@ public inherited sharing class SecureDml extends fflib_SobjectUnitOfWork.SimpleD */ public void handleUnableToPublishEvents( List objList ) { - throwUnableException( '00004', 'Attempted to publish {0} events without the required permission', objList ); // TODO: label + throwUnableException( FrameworkErrorCodes.DML_PUBLISH_NOT_ALLOWED, 'Attempted to publish {0} events without the required permission', objList ); // TODO: label } private void throwUnableException( String errorCode, String label, List objList ) { String sobjectTypeName = SobjectUtils.getSobjectName( objList[0] ); throw new SecureDmlException( StringUtils.formatLabel( label, new List{ sobjectTypeName } ) ) - .setErrorCode( '0000' ) + .setErrorCode( errorCode ) .addContext( 'sobjectTypeName', sobjectTypeName ) .addContext( 'records', objList ) .regenerateStackTraceString( 2 ); // push the stack trace string into the point that called the hander, rather than the handler itself @@ -496,7 +502,7 @@ public inherited sharing class SecureDml extends fflib_SobjectUnitOfWork.SimpleD String sobjectTypeName = SobjectUtils.getSobjectName( sobjectType ); throw new SecureDmlException( StringUtils.formatLabel( label, new List{ modeDescription, sobjectTypeName, fieldsInViolation.toString() } ) ) - .setErrorCode( '0000' ) + .setErrorCode( FrameworkErrorCodes.DML_ON_INACCESSIBLE_FIELDS ) .addContext( 'sobjectTypeName', sobjectTypeName ) .addContext( 'fieldsInViolation', fieldsInViolation ) .regenerateStackTraceString( 2 ); // push the stack trace string into the point that called the hander, rather than the handler itself