custom exception message in spring cloud function deployed in aws-lambda - exception

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.

Related

The noticeError method was not found. while using Coldbox and NewRelic for Error tracking

I'm just using NewRelic error trapping for my coldbox application. From OnException method, I'm just sending the error struct to log the error.
My code in onexception method
public function onException(event,rc,prc){
NewRelic.logError( prc.exception.getExceptionStruct());
}
The logerror() method resides in NewRelic.cfc and contains the following code
public boolean function logError(
required struct exception
) {
var cause = arguments.exception;
var params = {
error_id = createUUID(),
type: arguments.exception.type,
message: arguments.exception.message
};
writeDump(this.newRelic);
this.newRelic.noticeError(cause, params);abort;
return true;
}
So while error, I'm gettig the following error.
The noticeError method was not found.
You can see that, the noticeError() method is there in the object, but it is overloaded with arguments.
I'm using the same code for NewRelic error trapping in another coldfusion project without any frameworks.
Calling error.cfm through Cferror tag, and the code in error.cfm as follows
<cfset Application.newRelic.logError( variables.error )>
And in NewRelic.cfc, the logerror() method contains the same code as in the coldbox application. But it is logging errors in NewRelic without any issues.
This is the method I need to notice errors and log it in NewRelic.
noticeError(java.lang.Throwable, java.util.Map)
So I just thought to get the classname of the first argument Cause through the following code from both applications within logError() in NewRelic.cfc, to get the difference.
writeDump(cause.getClass().getName());
I'm getting
coldfusion.runtime.ExceptionScope for Coldbox application
and
coldfusion.runtime.UndefinedVariableException for normal coldfusion application
The cause argument is not throwable from coldbox application. So how to get the original error struct from coldbox application? and make it throwable to fix the noticeError method was not found issue.
The change in the underlying class happens when ColdBox duplicates the error object with CFML's duplicate() method. I doubt that ColdFusion behavior is documented anywhere, but I don't see an easy way to get around it right now other than creating your own instance of a java.langException and populating it with the details of the original error.
If you want to modify the ColdBox core code, this happens here:
https://github.com/ColdBox/coldbox-platform/blob/master/system/web/context/ExceptionBean.cfc#L43
I have entered this ticket for the ColdBox framework for us to review if we can stop duplicating the error object in future versions of the framework.
https://ortussolutions.atlassian.net/browse/COLDBOX-476
Update: Adam Cameron pointed out this ticket in the Adobe bug tracker that details this behavior in the engine. It was closed as "neverFix".
https://bugbase.adobe.com/index.cfm?event=bug&id=3976478

Spring integration | Service Activator - Error Channel , Exception handling

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.

Handle specific exception that is not related to an exchange

I created a custom component for a proprietary service. If this service is down i get noticed via a call of a callback function. I am throwing a custom exception at this point.
Sending exchanges to the producer/ consumer will yield no errors or exceptions (all seems to fine).
So i need to implement an emergency stop if my custom exception is thrown. I read a bit about exception handling in camel. I think i need a context-scoped onException(MyException.class).??? but what then?
Is this working on exceptions that are called without relation to an exchange? If this is working how to handle it. I want to stop certain routes in this case.
here you can find to stop routes from a route: http://camel.apache.org/how-can-i-stop-a-route-from-a-route.html.
If you do the call of the proprietary service in a route you do have an exchange btw.
kind regards,
soilworker
I created a little workaround: I set a boolean i the callback method is called. On each call of process i check this boolean and if true i throw an exception.
With this the exception is within normal camel exception handling and onException could be used.

Spring Integration - writing to a error queue when Exception happended in a Service Activator component

I'm starting to use Spring integration and I don't know how to resolve this situation if it's possible.
I would like to 'catch' automatically every Exception who could happend in the service activators of my application and send this errors to a dedicated queue.
Gateway is not a solution because I need some custom code so I have to use service activator elements if I have correctly understood the principle.
I thought that something like would be ok:
<jms:outbound-channel-adapter channel="errorChannel"
connection-factory="jmsConnectionFactory" destination="oneErrorQueue"/>
That is not working. I don't know if errorChannel is used by spring integration for putting the errors in indeed.
thank you, It seems to work.
I've put the transformer listening to the error-channel of the inbound component starting the flow and it gets the MessagingException when an error happens in service activator. Now the problem is that this error doesn't arrive to my queue. I let you see the code:
<jms:message-driven-channel-adapter
channel="input-channel" concurrent-consumers="1"
max-concurrent-consumers="3" connection-factory="jmsConnectionFactory"
transaction-manager="jtaTransactionManager" destination="myQueue" error-channel="myErrorChannel"/>
<transformer input-channel="myErrorChannel" output-channel="create-error-channel" ref="errorTransporter" method="transform"/>
<jms:outbound-channel-adapter channel="create-error-channel"
connection-factory="jmsConnectionFactory" destination="creationErrorQueue" extract-payload="true"/>
...
And the transformer:
public class ErrorTransporter {
#Transformer
public Message<CreateCustomerOrder> transform(MessagingException exception) {
return (Message<CreateCustomerOrder>) exception.getFailedMessage();
}
}
Thanks in advance for helping!
Add an error-channel attribute to the inbound component that starts the flow
error-channel="myErrorChannel"
when an upstream component (such as your service invoked by the service-activator) throws an exception, the inbound component will put an error message on the error channel. The payload of that message is a MessagingException that has two properies:
failedMessage
cause
So, on your error flow, add a transformer...
<int:transformer input-channel="myErrorChannel"
output-channel="toJmsChannel"
expression="payload.failedMessage"
followed by your jms outbound channel adapter.

How to handle exception in Struts

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.