Comments (5)
Since we don't provide any APIs to create or notify those kind of events, it's going to be difficult to try and write tests for an onSendError
handler.
While it is possible to create a "proper" event by calling Bugsnag.notify
and capturing the event, that requires Bugsnag.start
to have been called beforehand, which could be awkward for unit testing.
func mockAppHang(unhandled: Bool) -> BugsnagEvent {
var event: BugsnagEvent!
// Warning: The block will not be called if Bugsnag has not been started
Bugsnag.notifyError(NSError(domain: "", code: 0, userInfo: nil)) {
event = $0
return false
}
event.errors[0].errorClass = "App Hang"
event.errors[0].errorMessage = """
The app's main thread failed to respond to an event within 2000 milliseconds
"""
event.unhandled = unhandled
return event
}
A possible workaround is to break out some of the onSendError
logic into a more testable function, e.g.
func onSendError(event: BugsnagEvent) -> Bool {
shouldSendError(
errorClass: event.errors.first?.errorClass,
unhandled: event.unhandled)
}
// This is more testable since it does not need a BugsnagEvent
func shouldSendError(errorClass: String?, unhandled: Bool) -> Bool {
if unhandled, errorClass == "App Hang" {
// TODO: notify without sending to Bugsnag
return false
}
return true
}
from bugsnag-cocoa.
Hi @cwalo this issue is due to BugsnagEvent
not implementing a default initializer.
The event created with let event = BugsnagEvent()
is missing an internal property that results in the unhandled
getter always returning false.
The only supported way to create a BugsnagEvent
using our public API is via Bugsnag.notify()
or Bugsnag.notifyError()
:
/// The following test passes
func testUnhandledOverridden() {
Bugsnag.notifyError(NSError(domain: "", code: 0, userInfo: nil)) { event in
XCTAssertFalse(event.unhandled)
event.unhandled = true
XCTAssertTrue(event.unhandled)
return false
}
}
from bugsnag-cocoa.
Ah, that makes sense. In our case we're interested in being notified on non-fatal (handled) app hang events without reporting them to Bugsnag, so I'm trying to write a unit test that validates the return value of the onSendError
block we pass to the BugsnagConfiguration
using different BugsnagEvent
"types". Even with the public API, I'm not seeing a straightforward way to notify Bugsnag of the same errors that it would report itself (e.g. OOMs, app hangs, thermal kills, etc.), since BugsnagError
is not convertible to Error
. Ideally, I could do something like this:
func mockAppHang(unhandled: Bool) -> BugsnagEvent {
let error = BugsnagError()
error.errorClass = "App Hang"
let event = BugsnagEvent()
event.errors = [error]
// set event.unhandled
return event
}
Do you have any suggestions for writing tests for the onSendError
handler utilizing different error events?
from bugsnag-cocoa.
Nice! That's a decent workaround and I appreciate the suggestion. Obvious in hindsight 😅
from bugsnag-cocoa.
@cwalo I've closed this issue for now, but if you have any other questions just let us know 👍
from bugsnag-cocoa.
Related Issues (20)
- Calling NSProcessInfo.thermalState from an app with bugsnag installed crashes with _os_unfair_lock_recursive_abort HOT 3
- Bugsnag build phase script to auto upload dsym with Xcode 14 failed to upload HOT 12
- Will this pop up infinitely? HOT 1
- Swift Command-Line Failure to Upload HOT 3
- App builds in Xcode 14.3 beta fail with Use of undeclared identifier '_PTHREAD_MUTEX_SIG_init' HOT 3
- Loadconfig memory leak HOT 3
- Uploading dSYM seems to not working using Build phase script manually HOT 3
- [BSGAppHangDetector startWithDelegate:] crash HOT 6
- Bugsnag does not compile for visionOS with Xcode 15 Beta 2 HOT 2
- Duplicate library warning in Xcode 15 Beta HOT 7
- Support unhandled Kotlin Native Exceptions HOT 1
- App crashes when trying to start Bugsnag on Watch app HOT 1
- Bugsnag keep user data after logout in crash reports HOT 2
- Apple's update to their API policy about Privacy Manifest HOT 6
- OWASP: Buffer Overflow Vulnerabilities HOT 1
- Privacy manifest causing collisions through CocoaPods HOT 2
- Apple Report Missing API declaration HOT 2
- StartWithConfig memory leak HOT 6
- NSPrivacyAccessedAPICategoryDiskSpace : Missing API declaration HOT 11
- Thread warnings HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bugsnag-cocoa.