Class BugReport
- java.lang.Object
-
- org.openstreetmap.josm.tools.bugreport.BugReport
-
- All Implemented Interfaces:
java.io.Serializable
public final class BugReport extends java.lang.Object implements java.io.Serializable
This class contains utility methods to create and handle a bug report.It allows you to configure the format and request to send the bug report.
It also contains the main entry point for all components to use the bug report system: Call
intercept(Throwable)to start handling an exception.Handling Exceptions
In your code, you should add try...catch blocks for any runtime exceptions that might happen. It is fine to catch throwable there.You should then add some debug information there. This can be the OSM ids that caused the error, information on the data you were working on or other local variables. Make sure that no exceptions may occur while computing the values. It is best to send plain local variables to put(...). If you need to do computations, put them into a lambda expression. Then simply throw the throwable you got from the bug report. The global exception handler will do the rest.
int id = ...; String tag = "..."; try { ... your code ... } catch (RuntimeException t) { throw BugReport.intercept(t).put("id", id).put("tag", () → x.getTag()); }Instead of re-throwing, you can callReportedException.warn(). This will display a warning to the user and allow it to either report the exception or ignore it.- Since:
- 10285
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceBugReport.BugReportListenerA listener that listens to changes to this report.
-
Field Summary
Fields Modifier and Type Field Description private ReportedExceptionexceptionprivate booleanincludeAllStackTracesprivate booleanincludeDataprivate booleanincludeStatusReportprivate java.util.concurrent.CopyOnWriteArrayList<BugReport.BugReportListener>listenersprivate static byteMAXIMUM_SUPPRESSED_EXCEPTIONSThe maximum suppressed exceptions to keep to reportprivate static longserialVersionUIDprivate static java.util.Deque<Pair<java.time.Instant,java.lang.Throwable>>SUPPRESSED_EXCEPTIONSThe list of suppressed exceptions, Pair<time reported, exception>
-
Constructor Summary
Constructors Constructor Description BugReport(ReportedException e)Create a new bug report
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddChangeListener(BugReport.BugReportListener listener)Add a new change listener.static voidaddSuppressedException(java.lang.Throwable t)Add a suppressed exception.private voidfireChange()static java.lang.StringgetCallingMethod(int offset)Find the method that called us.static java.lang.StackTraceElementgetCallingMethod(int offset, java.lang.String className, java.util.function.Predicate<java.lang.String> methodName)Find the method that called the given method on the current stack trace.java.lang.StringgetReportText(java.lang.String header)Gets the full string that should be send as error report.static ReportedExceptionintercept(java.lang.Throwable t)This should be called whenever you want to add more information to a given exception.booleanisIncludeAllStackTraces()Determines if this report should include the stack traces for all other threads.booleanisIncludeData()Determines if this report should include the data that was traced.booleanisIncludeStatusReport()Determines if this report should include a system status reportvoidremoveChangeListener(BugReport.BugReportListener listener)Remove a change listener.voidsetIncludeAllStackTraces(boolean includeAllStackTraces)Sets if this report should include the stack traces for all other threads.voidsetIncludeData(boolean includeData)Set if this report should include the data that was traced.voidsetIncludeStatusReport(boolean includeStatusReport)Set if this report should include a system status report
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
MAXIMUM_SUPPRESSED_EXCEPTIONS
private static final byte MAXIMUM_SUPPRESSED_EXCEPTIONS
The maximum suppressed exceptions to keep to report- See Also:
- Constant Field Values
-
SUPPRESSED_EXCEPTIONS
private static final java.util.Deque<Pair<java.time.Instant,java.lang.Throwable>> SUPPRESSED_EXCEPTIONS
The list of suppressed exceptions, Pair<time reported, exception>
-
includeStatusReport
private boolean includeStatusReport
-
includeData
private boolean includeData
-
includeAllStackTraces
private boolean includeAllStackTraces
-
exception
private final ReportedException exception
-
listeners
private final java.util.concurrent.CopyOnWriteArrayList<BugReport.BugReportListener> listeners
-
-
Constructor Detail
-
BugReport
public BugReport(ReportedException e)
Create a new bug report- Parameters:
e- TheReportedExceptionto use. No more data should be added after creating the report.
-
-
Method Detail
-
addSuppressedException
public static void addSuppressedException(java.lang.Throwable t)
Add a suppressed exception. Mostly useful for when a chain of exceptions causes an actual bug report. This should only be used when an exception is raised inGuiHelperorProgressMonitorExecutorat this time.Loggingmay call this in the future, when logging a throwable.- Parameters:
t- The throwable raised. Ifnull, we add a newNullPointerExceptioninstead.- Since:
- 18549
-
isIncludeStatusReport
public boolean isIncludeStatusReport()
Determines if this report should include a system status report- Returns:
trueto include it.- Since:
- 10597
-
setIncludeStatusReport
public void setIncludeStatusReport(boolean includeStatusReport)
Set if this report should include a system status report- Parameters:
includeStatusReport- if the status report should be included- Since:
- 10585
-
isIncludeData
public boolean isIncludeData()
Determines if this report should include the data that was traced.- Returns:
trueto include it.- Since:
- 10597
-
setIncludeData
public void setIncludeData(boolean includeData)
Set if this report should include the data that was traced.- Parameters:
includeData- if data should be included- Since:
- 10585
-
isIncludeAllStackTraces
public boolean isIncludeAllStackTraces()
Determines if this report should include the stack traces for all other threads.- Returns:
trueto include it.- Since:
- 10597
-
setIncludeAllStackTraces
public void setIncludeAllStackTraces(boolean includeAllStackTraces)
Sets if this report should include the stack traces for all other threads.- Parameters:
includeAllStackTraces- if all stack traces should be included- Since:
- 10585
-
getReportText
public java.lang.String getReportText(java.lang.String header)
Gets the full string that should be send as error report.- Parameters:
header- header text for the error report- Returns:
- The string.
- Since:
- 10585
-
addChangeListener
public void addChangeListener(BugReport.BugReportListener listener)
Add a new change listener.- Parameters:
listener- The listener- Since:
- 10585
-
removeChangeListener
public void removeChangeListener(BugReport.BugReportListener listener)
Remove a change listener.- Parameters:
listener- The listener- Since:
- 10585
-
fireChange
private void fireChange()
-
intercept
public static ReportedException intercept(java.lang.Throwable t)
This should be called whenever you want to add more information to a given exception.- Parameters:
t- The throwable that was thrown.- Returns:
- A
ReportedExceptionto which you can add additional information.
-
getCallingMethod
public static java.lang.String getCallingMethod(int offset)
Find the method that called us.- Parameters:
offset- How many methods to look back in the stack trace. 1 gives the method calling this method, 0 gives you getCallingMethod().- Returns:
- The method name.
-
getCallingMethod
public static java.lang.StackTraceElement getCallingMethod(int offset, java.lang.String className, java.util.function.Predicate<java.lang.String> methodName)
Find the method that called the given method on the current stack trace.- Parameters:
offset- How many methods to look back in the stack trace. 1 gives the method calling this method, 0 gives you the method with the given name..className- The name of the class to search formethodName- The name of the method to search for- Returns:
- The class and method name or null if it is unknown.
-
-