0

I have multiple stack traces and I want to generate unique id for each of them using the Throwable methods like getStackTrace, getCause etc. The problem I am facing is that these stack traces are already generated and I am fetching them from a db where they have been stored as strings. Is there any way I could convert them back to Throwable?

asked Jun 18, 2020 at 7:30
7
  • 2
    Don't they have some id (primary key) already associated with them if they are stored in a db? And no, I don't think it can be done and why would you need to. Commented Jun 18, 2020 at 7:35
  • Can you elaborate a bit more what are you trying to achieve? How would you generate a unique id for Throwable if you would have it? Why do you want to create IDs for them? Would help to answer the question better if we understand what is it that you try to achieve by creating IDs. Commented Jun 18, 2020 at 7:36
  • I want to group the stack traces uniquely. It happens that there are multiple stack traces that mostly vary due to differences in os or app version but essentially they are the same. This causes problem as there maybe tens of thousands of crash reports, but only a dozen of them may be unique. Commented Jun 18, 2020 at 7:43
  • Based on your comment (which gives important additional context which you should have included in the question's description from the start) I think it'd be better to try to solve this problem on the database (SQL) itself rather than fetching the data and processing it in Java. Why is solving your problem on the database not an option? Commented Jun 18, 2020 at 9:22
  • Possibly Find sql records containing similar strings, Find rows with similar string values or similar may be of interest. Commented Jun 18, 2020 at 9:29

2 Answers 2

1

Exception implements Serializable. If you store your exceptions into the database using Java's serialization API, you should be able to deserialize them back into Exception objects using that same API.

See https://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html for the official documentation on this.

If you do not currently store them through that mechanism, you are out of luck and will need to change that.

answered Jun 18, 2020 at 8:03
Sign up to request clarification or add additional context in comments.

Comments

0

You can create exceptions with stacktraces manually from a text-only stack trace if you are a little creative, but it obviously won't be identical to the original exception.

Here's an example stacktrace from a log:

org.apache.solr.common.SolrException: org.apache.solr.client.solrj.SolrServerException: cancel_stream_error/closing
 at org.apache.solr.handler.component.SearchHandler.throwSolrException(SearchHandler.java:665)
 at org.apache.solr.handler.component.SearchHandler.handleRequestBody(SearchHandler.java:583)
 at org.apache.solr.handler.RequestHandlerBase.handleRequest(RequestHandlerBase.java:226)
 at org.apache.solr.core.SolrCore.execute(SolrCore.java:2884)

Running ex.printStackTrace() will print the exact same thing from this hand crafted exception:

SolrException ex = new SolrException(
 SolrException.ErrorCode.SERVER_ERROR, 
 "org.apache.solr.client.solrj.SolrServerException: cancel_stream_error/closing"
);
ex.setStackTrace(new StackTraceElement[]{
 new StackTraceElement("org.apache.solr.handler.component.SearchHandler", "throwSolrException", "SearchHandler.java", 665),
 new StackTraceElement("org.apache.solr.handler.component.SearchHandler", "handleRequestBody", "SearchHandler.java", 583),
 new StackTraceElement("org.apache.solr.handler.RequestHandlerBase", "handleRequest", "RequestHandlerBase.java", 226),
 new StackTraceElement("org.apache.solr.core.SolrCore", "execute", "SolrCore.java", 2884),
});
answered Mar 14, 2024 at 9:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.