我在Android库中使用Sentry,供其他开发人员使用。我从使用我的库的应用程序中获得了很多异常,但这些异常与该库无关,并且真的想忽略这些异常。有什么方法可以过滤异常,因此我只报告那些在堆栈跟踪中某处包含我的库包的异常?
答案 0 :(得分:1)
您可以使用ShouldSendEventCallback
:
public static void example() {
SentryClient client = Sentry.getStoredClient();
client.addShouldSendEventCallback(new ShouldSendEventCallback() {
@Override
public boolean shouldSend(Event event) {
// decide whether to send the event
for (Map.Entry<String, SentryInterface> interfaceEntry : event.getSentryInterfaces().entrySet()) {
if (interfaceEntry.getValue() instanceof ExceptionInterface) {
ExceptionInterface i = (ExceptionInterface) interfaceEntry.getValue();
for (SentryException sentryException : i.getExceptions()) {
// this example checks the exception class
if (sentryException.getExceptionClassName().equals("foo")) {
// don't send the event
return false;
}
}
}
}
// send event
return true;
}
});
}
有一张票可简化此操作:https://github.com/getsentry/sentry-java/issues/575
答案 1 :(得分:0)
对于遇到我确切问题的任何人,我修改了Brett的答案,以便检查整个堆栈跟踪,因为有时可以将异常原因埋在Android中。
SentryClient client = Sentry.getStoredClient();
client.addShouldSendEventCallback(new ShouldSendEventCallback() {
@Override
public boolean shouldSend(Event event) {
for (Map.Entry<String, SentryInterface> interfaceEntry : event.getSentryInterfaces().entrySet()) {
if (interfaceEntry.getValue() instanceof ExceptionInterface) {
ExceptionInterface i = (ExceptionInterface) interfaceEntry.getValue();
for (SentryException sentryException : i.getExceptions()) {
for (SentryStackTraceElement element : sentryException.getStackTraceInterface().getStackTrace()) {
if (element.getModule().contains("com.example.library")) {
return true;
}
}
}
}
}
return false;
}
});