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
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;
}
}
}
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
On Mar 3, 4:17 pm, "tibor.boe...@gmail.com" <tibor.boe...@gmail.com>
wrote: