This question already has an answer here:
Struts2 Exception handling usage?
(1 answer)
Closed 6 years ago.
How to create custom exception handler class in Struts2? I have tried adding the exception code in execute() but failed.
I hope that this guidelines will be usefull for your question, since it looks a little too broad, so if you have a specific requirement just edit your question. First of all I suggest you to read the official documentation in order to retrieve information about the Exception handling, Exception configuration and Exception Interceptor.
Now, I try to be more detailed as I can two explain how you can handle the exceptions in Struts2:
Global exception handling: everytime your application throws an exception, Struts2 will handle the result with the <global-results> and <global-exception-mappings> tags in struts.xml file.
Action exception handling: everytime your specific action throws an exception, Struts2 will handle the result <exception-mapping> tag inside a <action> tag
Global exception handling
Using the Struts 2 framework you can specify in the struts.xml how the framework should handle uncaught exceptions. The handling logic can apply to all actions (global exception handling) or to a specific action. Let's first discuss how to enable global exception handling.
To enable global exception handling you need to add two nodes to struts.xml: global-exception-mapping and global-results. For example examine struts.xml from the exception_handling project.
<global-results>
<result name="securityerror">/securityerror.jsp</result>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="org.apache.struts.register.exceptions.SecurityBreachException" result="securityerror" />
<exception-mapping exception="java.lang.Exception" result="error" />
</global-exception-mappings>
Now when a SecurityBreachException is thrown, the securityerror.jsp will be loaded. In my opinion global exception are very usefull for generic errors. For example you can create an ApplicationException view with a form used to send to your assistance the stack trace of the problem.
Action exception handling
If you need to handle an exception in a specific way for a certain action you can use the exception-mapping node within the action node.
<action name="actionspecificexception" class="org.apache.struts.register.action.Register" method="throwSecurityException">
<exception-mapping exception="org.apache.struts.register.exceptions.SecurityBreachException" result="login" />
<result>/register.jsp</result>
<result name="login">/login.jsp</result>
</action>
In this example when throwSecurityException method of the Register action throws a SecurityBreachException it returns a LOGIN result and the login.jsp is loaded. I've used this kind of solution for actions that may throw errors after a validation (for example to execute CRUD operations in my database).
Custom error jsp
By default, the ExceptionMappingInterceptor adds the following values to the Value Stack:
exception : The exception object itself
exceptionStack : The value from the stack trace
You can use them to create a custom jsp that prints the error occurred and the stacktrace. Here you have an example:
<h2>An unexpected error has occurred</h2>
<p>
Please report this error to your system administrator
or appropriate technical support personnel.
Thank you for your cooperation.
</p>
<hr/>
<h3>Error Message</h3>
<s:actionerror/>
<p>
<s:property value="%{exception.message}"/>
</p>
<hr/>
<h3>Technical Details</h3>
<p>
<s:property value="%{exceptionStack}"/>
</p>
Logs
I think that also the LoggingExceptions paragraph will be usefull.
Related
I have a java project written using spring-cloud-function and deployed in aws-lambda.
I am trying to return a custom exception with some fields in the exception message body, something like"
{
reason: <exception reason>
code: <error code>
<some other fields>
}
#ExceptionHandler, that is generally used in spring boot doesn't seem to work here.
I can return the exception in the required format by creating a class for building the exception message in required format but in that case the error code will always be 200 since it will not be an exception object per se. Instead it will be my custom error object.
Is there a way I can set this up so that the above required format of exception object is returned and correct error code can be returned too?
Thanks in advance
First, the exception has nothing to do with Spring Boot. It's part of spring-web, so yes it would not work here since s-c-function is a general purpose framework, where the same function could be deployed and triggered via web, streaming, aws-lambda etc. .
Now, yes we had a problem returning JSON error (as you show) or object that represents the same, but that was fixed. So please update s-c-function to 3.2.3.
I am using Struts 2.3 and I have dynamic method invocation turned on as well as strict-method-invocation="true" on packages.
The problem is that if the user tries to invoke a method that does not exist or isn't allowed on an action he gets ugly message.
I would like to display my own custom error page for that.
I tried to setup a global exception handler like that:
<global-results>
<result name="error">/WEB-INF/ErrorPage.html</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error" />
</global-exception-mappings>
This works when I throw some exception from one of my action classes, but it does not handle Strut's "Invalid method x for action y" errors.
Relatively hacky solution. Since using not allowed method with strict DMI results in response with a 404 status code, you can use <error-page> to map it to some page in web.xml file.
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
If you want to execute some action instead of just showing some page you can send redirect from this page.
<% response.sendRedirect("some_action"); %>
I have a problem in catching the exceptions in my spring integration application.
Flow of operations in my application.
Http:inbound gateway which receives the request (error-channel defined to my custom error channel)
Service Activator for basic validations (Exceptions which are thrown from here are handled by error-channel defined on the GW)
splitter
Aggregator
Exceptions on my splitter or Aggregator are not handled by my error channel. why?
Steps taken:
I added a chain and included a header enricher and specified an error channel just before the splitter.
After this, any exception on my splitter is handled by my error channel mentioned in the header enricher.
<chain input-channel="invitations">
<header-enricher>
<error-channel ref="failed-invitations" />
</header-enricher>
<int:splitter ref="payloadSplitter" />
</chain>
But the same doesnt work when do the same on my Aggregator. why?
Whenever there is an exception in my code, it retries and gets executed more than one time. why?
I have a "errorChannel" defined which logs the exceptions. it doesnt work.
I know the thread is too old, but I was also facing a similar issue and found I declared error-channel in header-enricher but not provide 'overwrite="true"' as a parameter. And after providing 'overwrite="true"'it is working as needed. I am surprised why spring integration does not provide an overwrite=true by default.
Let us know this is what solution you did in your old code? So everyone can find out the solution for such a scenario.
Our application is JSF2 based and we are desiging a exception handling for that. I tried using JSF2 exception handling framework configured in faces-config.xml.
When I throw a null pointer from backing bean, which is referred in Facelets page, then it is coming to exception handling function as javax.el.PropertyNotFoundException and also it doesn't have any other stack trace about.
Is there any way to get the exact cause of the exception and the stack trace?
That will happen when the action method is called from inside a composite component. This is already been reported as JSF issue 1806 which is scheduled to be fixed for JSF 2.2.
As far now, there's no way to get the root cause in the exception handler, because it's completely swallowed. Your best bet is using a separate logger or, as a hacky workaround, to pass the exception as a request attribute yourself.
Enabling the Development mode usually gives better error information.
Add the following to your web.xml.
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
However you should turn this off for your production.
How can we handle exceptions in Struts? Please share the sample code.
The following URL should help http://www.objectsource.com/j2eechapters/Ch18-Exception_Handling.htm
Use log4j to log the exceptions,
Never use System.out.println in struts application to catch exceptions, as it is expensive ,
Tutorials for log4j> http://logging.apache.org/log4j/1.2/manual.html
to handle the errors:
in struts project to handle the error objet by using ActionError object and to handle the errors by using ActionErrors object.
for suppose
ActionError ae1=new ActionError("err.one");
ActionError ae2=new ActionError("err.two");
Action Errors aes=new ActionErrors();
aes.add(ae1);
aes.add(ae2);
saveErrors(request,aes);//store the errors object in request object
to handle exception:
1)using try and cach blocks
2)using declarative exception handling technique
to handle the exceptions by using global exceptons tag in struts-config.xml
whenever that exception will be came it executes the gen.jsp page.