Around Advices
Around advices are the most powerful of all as you completely hijack the requested action with your own action that looks, smells and feels exactly as the requested action. This is usually referred to as a proxy design pattern.
1
users.list => users.aroundHandler() <=> list()
Copied!
This will allow you to run both before and after advices but also surround the method call with whatever logic you want like transactions, try/catch blocks, locks or even decide to NOT execute the action at all.
You can do it globally by using the aroundHandler() method or targeted to a specific action around{actionName}().
Examples
1
// executes around any action
2
function aroundHandler(event,targetAction,eventArguments,rc,prc){
3
}
4
5
// executes around the list() action ONLY
6
function aroundList(event,targetAction,eventArguments,rc,prc){
7
}
8
9
// Around handler advice for transactions
10
function aroundHandler(event,targetAction,eventArguments,rc,prc){
11
12
// log the call
13
log.debug("Starting to execute #targetAction.toString()#" );
14
15
// start a transaction
16
transaction{
17
18
// prepare arguments for action call
19
var args = {
20
event = arguments.event,
21
rc = arguments.rc,
22
prc = arguments.prc
23
24
};
25
structAppend( args, eventArguments );
26
// execute the action now
27
var results = arguments.targetAction( argumentCollection=args );
28
}
29
30
// log the call
31
log.debug( "Ended executing #targetAction.toString()#" );
32
33
// return if it exists
34
if( !isNull( results ) ){ return results; }
35
}
36
37
// Around handler advice for try/catches
38
function aroundHandler(event,targetAction,eventArguments,rc,prc){
39
40
// log the call
41
if( log.canDebug() ){
42
log.debug( "Starting to execute #targetAction.toString()#" );
43
}
44
45
// try block
46
try{
47
48
// prepare arguments for action call
49
var args = {
50
event = arguments.event,
51
rc = arguments.rc,
52
prc = arguments.prc
53
54
};
55
structAppend( args, eventArguments );
56
// execute the action now
57
return arguments.targetAction( argumentCollection=args );
58
}
59
catch(Any e){
60
// log it
61
log.error("Error executing #targetAction.toString()#: #e.message# #e.detail#", e);
62
// set exception in request collection and set view to render
63
event.setValue( "exception", e)
64
.setView( "errors/generic" );
65
66
}
67
68
}
Copied!
The arguments received by these interceptors are:
  • event : The request context reference
  • targetAction : The function pointer to the action that got the around interception. It will be your job to execute it (Look at samples)
  • eventArguments : The struct of extra arguments sent to an action if any
  • rc : The RC reference
  • prc : The PRC Reference

Exceptions & Only Lists

You can fine tune these interception methods by leveraging two public properties in the handler:
  • this.aroundhandler_only : A list of actions that the aroundHandler() action will fire ONLY!
  • this.aroundhandler_except : A list of actions that the aroundHandler() action will NOT fire on
1
// only fire for the actions: save(), delete()
2
this.aroundhandler_only = "save,delete";
3
// DO NOT fire for the actions: login(), doLogin(), logout()
4
this.aroundhandler_except = "login,doLogin,logout"
Copied!
Last modified 3yr ago
Copy link
Edit on GitHub