Hi Samitha,
If you want to log the full stacktrace in jaggery, you can log it like
below.
log.error(e) //print the stacktrace
log.error('My error message') //print the message
(If you are using the latest jaggery release, it supports logging both
the message and the exception object as in log4j API[1
<http://mail.wso2.org/mailarchive/dev/2014-October/038165.html>])
In addition, you should not send the detailed backend error
information(stacktrace, class names etc...) to the front-end since
front-end(or the client) does not need to know those details(Principle
of Information Hiding[2
<https://en.wikipedia.org/wiki/Information_hiding>]).
My suggestion is, log the back end error message and send the
front-end message using jaggery response object[3
<http://jaggeryjs.org/documentation.jag?api=response>] as below.
try {
// you business logic
} catch(e) {
log.error(e);
log.error("Additional error information that you want to log");
response.status = 400 //
error code
response.content = "Front end error message"; // error message
}
[1] http://mail.wso2.org/mailarchive/dev/2014-October/038165.html
[2] https://en.wikipedia.org/wiki/Information_hiding[3]
http://jaggeryjs.org/documentation.jag?api=response
Regards,
Samith
On Thu, Mar 31, 2016 at 4:41 AM, Samitha Chathuranga <[email protected]>
wrote:
> Hi Praminda,
>
> Thanks for the suggestion. That approach seems suitable here.
>
> Regards.
>
> On Thu, Mar 31, 2016 at 10:33 AM, Praminda Jayawardana <[email protected]>
> wrote:
>
>> Hi Samitha,
>>
>> LBYL(Look Bfore You Leap) and EAFP(Easier to Ask Forgiveness than
>> Permission) is the two ways used to error checking. Different languages
>> have different preferences. Ex: Python prefers EAFP (try-catch) over LBYL
>> (if-else).
>> I haven't come across any standard for Java but people use try-catch
>> frequently. In JS, using try-catch for use cases where you can't test for
>> the error is not preferable due to performance issues and asynchronous
>> nature of JS.
>>
>> In you case my suggestion is to catch the exception in Java level , log
>> it if you want the stack trace, throw the error to jaggery and send a error
>> code to JS as tharik suggested.
>>
>> Thanks,
>> Praminda
>>
>> On Wed, Mar 30, 2016 at 2:02 PM, Samitha Chathuranga <[email protected]>
>> wrote:
>>
>>> Hi Tharik,
>>>
>>> Thanks Tharik. Yes in jaggery level I could detect exception type in
>>> catch block by following your approach. But it seems impossible to log or
>>> get the stack trace of the exception there. But as in the example in
>>> yours, e.javaException.getFaultGateWayString() might be giving it, but not
>>> all the Exceptions are having such methods as I know.
>>>
>>> Thanks
>>> Samitha
>>>
>>>
>>> On Wed, Mar 30, 2016 at 11:20 AM, Tharik Kanaka <[email protected]> wrote:
>>>
>>>> Hi Samitha,
>>>>
>>>> Backend server should send a proper HTTP Error code instead of return a
>>>> value with error message. Here you can throw exception from Java and do the
>>>> log in jaggery level. In jaggery level you can detect exception type in
>>>> catch block by following approach in the code below.
>>>>
>>>> if(e.javaException instanceof
>>>> org.wso2.carbon.apimgt.api.FaultGatewaysException){
>>>> return {
>>>> error:true,
>>>> message:e.javaException.getFaultGateWayString()
>>>> };
>>>>
>>>> }
>>>>
>>>> Regards,
>>>>
>>>> On Wed, Mar 30, 2016 at 11:08 AM, Samitha Chathuranga <[email protected]
>>>> > wrote:
>>>>
>>>>> Hi Sachith,
>>>>>
>>>>> So do you suppose to catch the error thrown from java, finally at the
>>>>> jaggery or even later at javascript?
>>>>> Anyway if we are printing the full stacktrace in Java side, what is
>>>>> the objective of throwing it to the jaggery ?
>>>>>
>>>>> Is it to indicate to the front end that an error was occurred in the
>>>>> back end? I guess this indication can also be done using a return value
>>>>> (boolean or something else) from java side function, but I'm afraid it is
>>>>> an accepted standard.
>>>>>
>>>>> Thanks,
>>>>> Samitha
>>>>>
>>>>> On Wed, Mar 30, 2016 at 10:45 AM, Sachith Withana <[email protected]>
>>>>> wrote:
>>>>>
>>>>>> Hi Samitha,
>>>>>>
>>>>>> From what I understand, you need to both print the full stack trace
>>>>>> and throw a proper message to the front end.
>>>>>>
>>>>>> If that so, you can catch the error, log it and throw a custom error
>>>>>> from the java side.
>>>>>>
>>>>>> Cheers,
>>>>>> Sachith
>>>>>>
>>>>>> On Wed, Mar 30, 2016 at 10:31 AM, Samitha Chathuranga <
>>>>>> [email protected]> wrote:
>>>>>>
>>>>>>> Hi,
>>>>>>>
>>>>>>> I am developing a feature in WSO2 PC and in it I have a JavaScript
>>>>>>> front end and a Java back end. To call java classes from the JavaScript
>>>>>>> I
>>>>>>> am using Jaggery in between the above two. So my question is what is the
>>>>>>> best practice in Exception handling and error logging in such a this
>>>>>>> scenario.
>>>>>>>
>>>>>>> As I understand I can come out with few options.
>>>>>>>
>>>>>>> Option 1- Catching the exception in the Java class itself and log
>>>>>>> the complete error with an appropriate custom error message using
>>>>>>> log.error(errMsg,
>>>>>>> e), without throwing exceptions from the method signature, using
>>>>>>> throws clause. (As we usually do in a sole java application). So
>>>>>>> here the exceptions are completely handled from the back end and doesn't
>>>>>>> come at least to the jaggery. And for the purpose of showing some error
>>>>>>> message to the front end user we can return a message or flag or
>>>>>>> something
>>>>>>> from the java function to the jaggery side. So at the jaggery side or
>>>>>>> even
>>>>>>> at the javascript, we can check that return value and show the error
>>>>>>> message as an alert and proceed depending conditionally on that return
>>>>>>> value.
>>>>>>>
>>>>>>> Option 2- We can throw the the exception in the catch block of java
>>>>>>> class using throw new ExceptionClassName("custom error message",e) and
>>>>>>> catch it in the jaggery file. But here I am getting the problem that we
>>>>>>> cannot log the complete long exception report using log.error(_ _) in
>>>>>>> jaggery. We can just print the custom error message set by us when
>>>>>>> throwing
>>>>>>> it from Java side. But I don't know is there any other way to trace that
>>>>>>> complete exception in jaggery. Anybody know something on it?
>>>>>>>
>>>>>>> Option 3- We can catch the error in the JavaScript, without catching
>>>>>>> in the Jaggery. But I don't think this is ok as we won't be able to
>>>>>>> print
>>>>>>> the exception in the server console if we do it so.
>>>>>>>
>>>>>>> I want to know what is the best practice in such a this scenario and
>>>>>>> if it is Option 2, what is the solution for the problem in it as
>>>>>>> mentioned
>>>>>>> there above. Appreciate support from somebody.
>>>>>>>
>>>>>>> Thanks,
>>>>>>> Samitha
>>>>>>>
>>>>>>> --
>>>>>>> Samitha Chathuranga
>>>>>>> Software Engineer, WSO2 Inc.
>>>>>>> lean.enterprise.middleware
>>>>>>> Mobile: +94715123761
>>>>>>>
>>>>>>> _______________________________________________
>>>>>>> Dev mailing list
>>>>>>> [email protected]
>>>>>>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Sachith Withana
>>>>>> Software Engineer; WSO2 Inc.; http://wso2.com
>>>>>> E-mail: sachith AT wso2.com
>>>>>> M: +94715518127
>>>>>> Linked-In: <http://goog_416592669>
>>>>>> https://lk.linkedin.com/in/sachithwithana
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Samitha Chathuranga
>>>>> Software Engineer, WSO2 Inc.
>>>>> lean.enterprise.middleware
>>>>> Mobile: +94715123761
>>>>>
>>>>> _______________________________________________
>>>>> Dev mailing list
>>>>> [email protected]
>>>>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>>
>>>> *Tharik Kanaka*
>>>>
>>>> WSO2, Inc |#20, Palm Grove, Colombo 03, Sri Lanka
>>>>
>>>> Email: [email protected] | Web: www.wso2.com
>>>>
>>>
>>>
>>>
>>> --
>>> Samitha Chathuranga
>>> Software Engineer, WSO2 Inc.
>>> lean.enterprise.middleware
>>> Mobile: +94715123761
>>>
>>> _______________________________________________
>>> Dev mailing list
>>> [email protected]
>>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>>
>>>
>>
>>
>> --
>> *Praminda Jayawardana*
>> Software Engineer
>> WSO2 Inc.; http://wso2.com
>> Mobile : +94 (0) 716 590818
>>
>
>
>
> --
> Samitha Chathuranga
> Software Engineer, WSO2 Inc.
> lean.enterprise.middleware
> Mobile: +94715123761
>
> _______________________________________________
> Dev mailing list
> [email protected]
> http://wso2.org/cgi-bin/mailman/listinfo/dev
>
>
--
Best Regards
Samith Dassanayake
Software Engineer | Cloud TG
WSO2, Inc. | http://wso2.com
lean. enterprise. middleware
Mobile : +947 76207351
Blog : buddycode.blogspot.com
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev