Skip to content

Commit

Permalink
Add 4 new crash types for demonstration with PLCrashReporter, adds vi…
Browse files Browse the repository at this point in the history
…sual feedback to watchOS app when slecting row
  • Loading branch information
cdillard-NewRelic committed Oct 24, 2024
1 parent 8ce2abc commit e60c39f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,26 @@ import SwiftUI
import NewRelic

struct ContentView: View {
var viewModel = UtilViewModel()

@State private var isTapped: UtilOption? = nil
var viewModel = UtilViewModel()

var body: some View {
List(viewModel.options, id: \.title) { option in
Text(option.title)
.background( isTapped == option ? Color.gray.opacity(0.3) : Color.clear)
.onTapGesture {
option.handler()
}
.onLongPressGesture(minimumDuration: 0.1) { _ in
withAnimation {
isTapped = option
}
} perform: {
withAnimation {
isTapped = nil
}
}
}
.NRTrackView(name: "WatchOSContentView")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
@interface triggerException : NSObject

+ (void) testing;
+ (void)invalidPerformSelector;
@end
3 changes: 3 additions & 0 deletions Test Harness/NRTestApp/NRTestApp/Helpers/triggerException.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ + (void) testing {
[NewRelic recordHandledException:e];
}
}
+ (void)invalidPerformSelector {
[self performSelector:@selector(die_die)];
}

@end
45 changes: 43 additions & 2 deletions Test Harness/NRTestApp/NRTestApp/ViewModels/UtilViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
import Foundation
import NewRelic

struct UtilOption {
struct UtilOption: Equatable {
static func == (lhs: UtilOption, rhs: UtilOption) -> Bool {
lhs.title == rhs.title
}

let title:String
let handler:(() -> Void)
}
Expand Down Expand Up @@ -38,6 +42,13 @@ class UtilViewModel {
options.append(UtilOption(title: "Set Attributes", handler: { [self] in setAttributes()}))
options.append(UtilOption(title: "Remove Attributes", handler: { [self] in removeAttributes()}))
options.append(UtilOption(title: "Crash Now!", handler: { [self] in crash()}))

// crash types
options.append(UtilOption(title: "Unhandled Exception Now!", handler: { [self] in generateUncaughtException()}))
options.append(UtilOption(title: "Stack Overflow Now!", handler: { [self] in generateStackOverflow()}))
options.append(UtilOption(title: "Fatal App Hang Now!", handler: { [self] in generateFatalAppHang()}))
options.append(UtilOption(title: "Raise NSException Now!", handler: { [self] in throwNSException()}))

options.append(UtilOption(title: "Record Error", handler: { [self] in makeError()}))
options.append(UtilOption(title: "Record Handled Exception", handler: { triggerException.testing()}))

Expand All @@ -62,11 +73,41 @@ class UtilViewModel {
options.append(UtilOption(title: "Shut down New Relic Agent", handler: { [self] in shutDown()}))
}

// Crash Types
func crash() {
// This will cause a crash to test the crash uploader, crash files may not get recorded if the debugger is running.
NewRelic.crashNow("New Relic intentionally crashed to test Utils")
}


func generateUncaughtException() {
let someJson : Dictionary = ["foo":self]
do {
let data = try JSONSerialization.data(withJSONObject: someJson, options: .prettyPrinted)
print("Received data: %@", data)
} catch {

}
}

func generateStackOverflow() {
let items = ["Hello world"]
// Use if statement to remove warning about calling self through any path
if (items[0] == "Hello world") {
generateStackOverflow()
}
print("items: %@", items)
}

func generateFatalAppHang() {
Thread.sleep(forTimeInterval: 3)
_exit(1)
}

func throwNSException() {
NSException(name: .internalInconsistencyException, reason: "example internalInconsistencyException", userInfo: nil).raise()
}
// End test crashes.

func removeAttributes() {
if(NewRelic.removeAllAttributes()){
attributes = ""
Expand Down

0 comments on commit e60c39f

Please sign in to comment.