Throwing JSONException's with no trace

25 views
Skip to first unread message

progre55

unread,
Mar 1, 2010, 11:07:40 PM3/1/10
to jabsorb-user
Hi people.

Is it possible to throw a JSONExcpetion, but only with some exception
message, and no StackTrace?
For instance, I have a method login(String login, String passwd), and
if login is unsuccessful, I want to throw a JSONException saying
something like "username/password incorrect", but I dont really want
to reveal my stacktrace in the exception message.

I'm throwing (throw new JSONException("myMessageHere");) a new
exception, but it still get the whole "trace" in my "error" array
inside the response.

Any suggestions, please?

/Ikrom

Michael Clark

unread,
Mar 1, 2010, 11:25:37 PM3/1/10
to jabsor...@googlegroups.com

Have a look at
JSONRPCBridge.setExceptionTransformer(ExceptionTransformer
exceptionTransformer);

You could implement it with an instanceof check for your exception type:

class MyExceptionTransformer implements ExceptionTransformer
{
/**
* Transform the exception to the format desired for transport to the
client.
* This method should not itself throw an exception.
*
* @param t The exception to be transformed
* @return one of the JSON-compatible types (JSONObject, String, Boolean
* etc.), or a Throwable
*/
public Object transform(Throwable t) {
if (t instanceof TheExceptionIWantToTransform) {
return "customer error message";
} else {
return t;
}
}
}

progre55

unread,
Mar 2, 2010, 4:46:24 AM3/2/10
to jabsorb-user
Great, thanks a lot! Appreciate!

tibor....@gmail.com

unread,
Mar 3, 2010, 10:17:34 AM3/3/10
to jabsorb-user
progre55,

just one small note on the topic: Instead of using JSONException, I
would suggest having your own exception for application level
exceptions, this way one has a clean separation of domain/framework
code. JSONException for me has the semantics that some exception has
happended in the JSON/jabsorb part, not deeper in the application
logic... And generally I do not want to expose my stack trace to the
client, at least not in production - instead I log the error on the
server and communicate some polite and less technical message. These
are my reasons for using a custom ExceptionTransformer implementation.

Cheers,
T

progre55

unread,
Mar 3, 2010, 5:28:37 PM3/3/10
to jabsorb-user
Thanks for the tips, Tibor. I'll definitely consider them.
Appreciate!

On Mar 3, 4:17 pm, "tibor.boe...@gmail.com" <tibor.boe...@gmail.com>
wrote:

Reply all
Reply to author
Forward
0 new messages