Attaching to the Controller Dispatch Event - zend-framework2

ZF2 question here.
I like the event driven stuff in ZF2.
Here's my goal.
I want to start the session, using a database table, and I want to be thrown to the error/error template if for some reason there is no database connection.
Here are some ways I thought about doing this. I could attach to the application 'route' event and start the session right before the route matching happens, or right after. There are no TRY catches though, in the Mvc\Application.php file so I don't think this is the place to do it.
There IS however a try catch block around the CONTROLLER's dispatch method being called. And I noticed that the controller has it's own internal 'dispatch' event it triggers.
So the other possibility is that I should attach to this internal controller dispatch event and setup my session THEN before the controller's action methods actually execute, and thus before they NEED the session.
So, how can I attach to this internal 'dispatch' method? Or how else can I make it so that any database connection exceptions go to my nice Exception page rather than getting spewed out as a normal php error without any exception handling???
There is no more init() method on the controller because this is functionality events are supposed to provide.
Thank you for the help.
FYI : I tried attaching to different events, such as 'bootstrap', 'loadmodules', 'loadmodules.post'. All I did in the attached method was throw an exception to see if I would end up in the nice error page I have but I didn't have any luck. I ended up debugging it and found this internal controller 'dispatch' method. The controllers get their own internal event manager. At least that's what I gather from my debugging. I might be wrong so correct me please!

Related

Execute controller method on startup (Grails)

I have an event controller with a populate() method which adds events from a JSON link to my database. I want this to occur on startup of my server.
I've tried various things in my Bootstrap file. Such as:
def event = new EventController.populate()
You really should move that code into a service and out of your controller. This gives you a lot more options, including injecting the service into your Bootstrap.groovy and calling the method on startup of the application.
That's a much more proper Grails solution.

Struts Interceptor workflow

In Struts2 if we have define an interceptor stack and it is called in First in First Out manner.
So in post processing phase what happened if one of the earlier interceptor return a control string which in result render the response to the client.
I want to know that would the left interceptor will be processed or not.
Well it will work like this.
Your action method will only called once the interceptor stack has been called fully.This means that once the first interceptor has been called successfully in the stack it will call the next interceptor defined in the stack and there reference being stored in the stack this chain will keep on calling till the last interceptor in the stack is called
invocation.invoke()
this call is the key to call next interceptor defined in the stack or of this is the last it will call the desired function in your action class.
now in other case suppose some of the interceptor failed say workflow it will return the result as INPUT and will halt the further execution of the interceptor and framework will output the desired JSP/Tempelate to the user.
Than comes the post -processing/cleaning in this case interceptors will be called in reverse order i.e top most or latest executed interceptor will be called first and den so on so.
The idea for this post-processing is to do any clean-up work or any other things which needs to be done (like cleaning up resources etc)
Hope this will give you some idea.

How can I stop Vaadin from triggering my window's ParameterHandler more than necessary?

I added a ParameterHandler to my application's main window so I can catch and process custom get parameters in my url. However, the presence of one parameter reloads the first page of my application, for some reason building this page triggers the handleParameters method again, this time with no parameters passed. If left unchecked (e.g. no ifs) the page's elements will just replicate themselves on top of themselves over and over again.
I suspect it has something to do with my main windows's addComponent method.
How can I stop it from doing that?
I completely forgot I still had this question open. I found that the problem was that the method got triggered for both get and post type parameters, and this is what caused the unexpected output.
I already implemented an HttpServletRequestListener because I used Vaadin's ThreadLocal pattern, so I just filtered all GET parameters out of the request object in one of the implemented methods and went from there.
debug your application and have a view at the call stack of the handleParameters method

ASP.Net MVC Try Catch Best Practice

Could anyone please guide me on what's the best practice to handle exception in ASP.NET MVC?
Controller?
Model?
Model (Contains EF logics i.e. save, update etc) throw any exception and catch in Controller?
In any method or block of code where you may expect specific exceptions (interacting with a database or an external service which may or may not not be available, etc.) wrap that code in a Try/Catch to catch the specific exception(s) in question. You'd want to know exactly what kind of exceptions occurred to handle them properly. (Naturally, use a Finally block to dispose of any open resources.) How to properly handle them is up to you and how you want your application to behave.
You should definitely have a global exception handling to catch anything unexpected which falls through the cracks. At no point should an unhandled exception bubble up to the user. The global exception handler should just present the user with a friendly error message and should log the exception and notify you of what happened. Generally, a good goal is to identify the exception and add error handling code to catch it in its localized state before it bubbles up to global. The goal, over time, should be to have as few global exceptions as possible and to have any potentially-exception-generating code have its own error handling to guard against those cases.
An example of the latter could be something as simple as a particular method receiving null arguments that you want to check for before using those arguments. One thing you want to avoid, however, is using exception handling for logic flow. For example...
Let's say you have a method which takes a custom object as an argument:
public MyMethod(MyObject obj)
{
// ...
}
Your original code assumes that obj will always have a value. However, after some time of production use, you discover that obj is sometimes null and that it's throwing a NullReferenceException from within that method, which is being caught by the global exception handler.
You may be tempted to just wrap the code in MyMethod in its own Try/Catch block to catch that exception. This isn't necessarily a good idea (though there may be cases where it is). Instead, you'd want to check for null at the start of the method:
public MyMethod(MyObject obj)
{
if (obj == null) throw new ArgumentNullException("obj can not be NULL");
// ...
}
This encapsulates the method better and allows it to throw controlled exceptions. Exceptions aren't bad things, only unexpected exceptions. Note that the above will still throw the exception which will still bubble up to the global handler. Thus, it's also a good idea to wrap the calls to this method in a Try/Catch block in order to catch the ArgumentNullException and handle it accordingly. Perhaps the code which calls this method can fix the null reference and try again, perhaps it can try something else, etc. You still don't want it to bubble up to the global handler if possible, since this has become an "expected" exception and can be handled accordingly.
Naturally, you still want to avoid the throwing/catching of the expected exceptions in the above example, so similar checks for null should happen before calling the method so that the method isn't even called.
Maybe if the object is null you can directly present the user with an error message and log the error and notify you of as much information about the state of things as possible so that you can research why it's null and fix it. Maybe being null is a perfectly acceptable state of that object at that time according to the logic of the application. If so, check if it's null and don't bother calling the method. Just carry on as normal.
It's a lot of error-checking and handling code, but that's a good thing.
Edit: Another thing to note about exception handling is that you should catch the exception only if you can actually handle it at that time. If the method can't handle it internally, let the exception bubble up from the method to the calling method, and so on. Thus, the only exceptions which should reach the global handler are exceptions which you can't actually handle anywhere in the code (which is why it's a good goal to fix and prevent global exceptions).
"Handle" in this case would mean being able to actually do something about it. Maybe that something is to just log it and carry on, maybe that something is to perform some specific logic, etc. But to catch, log, and re-throw is bad design. If you can't recover from the exception, let it bubble up.
The best way is to catch the exception globally, log it using Elmah. Otherwise you will have to put all your exceptions in your every controller code and that would be a lot of repetition especially for CRUD operations

Spring Security - Prevent AccessDeniedException from stopping application normal flow

I'm using Spring Secuirty 3 with ACL module. I'm securing the methods with #PreAuthentication annotations using a custom PermissionEvaluator. Its working fine, however every time the PermissionEvaluator returns an ACCESS_DENIED an AccessDeniedException is thrown at some point and this stops the application execution. The desired behaivore will be that when the PermissionEvaluator returns and ACCESS_DENIED, the secured method call is only prevented (skipped) and the rest of the application keeps running normally. Does anyone have an idea on how to achieve this?
If you wrap each call where you want this to happen in a try...catch, you can get this behavior. Basically, since it's an exception, any normal exception handling will apply to it. If your application can handle that exception and continue normally, then do exactly that!
Here's an example of what I mean:
// The 1st method that could be denied but you want to continue execution after
try {
// Call method A that will throw the exception
} catch (/*The exception you expect to be thrown and want to continue after*/){}
// The 2nd method that could be denied but you want to continue execution after
try {
// Call method B that will throw the exception
} catch (/*The exception you expect to be thrown and want to continue after*/){}
etc.
Yes, it does add a lot of overhead to calling those methods, but it is a fairly simple way of allowing execution to continue after an exception is raised.
I would also argue that it is also more correct, since the calling code does know how to deal with those exceptions. This also doesn't require any additional Spring configuration, which means that the code behavior remains closer to the default and does not rely on external configurations to determine it's behavior.

Resources