Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw errors from attach when native SDK is not available #572

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(react-native): throw errors from attach when native SDK is not av…
…ailable or has not been started
yousif-bugsnag committed Jan 22, 2025

Verified

This commit was signed with the committer’s verified signature.
tstirrat15 Tanner Stirrat
commit 0f277e179679aaed36e21ef4ae9cb21379de042f
5 changes: 2 additions & 3 deletions packages/platforms/react-native/lib/client.ts
Original file line number Diff line number Diff line change
@@ -37,7 +37,6 @@ const backgroundingListener = createBrowserBackgroundingListener(AppState)
const xhrRequestTracker = createXmlHttpRequestTracker(XMLHttpRequest, clock)

const idGenerator = createIdGenerator(isDebuggingRemotely)
const schema = createSchema()
const BugsnagPerformance = createClient({
backgroundingListener,
clock,
@@ -49,11 +48,11 @@ const BugsnagPerformance = createClient({
new NetworkRequestPlugin(spanFactory, spanContextStorage, xhrRequestTracker)
],
resourceAttributesSource,
schema,
schema: createSchema(),
spanAttributesSource,
retryQueueFactory: createRetryQueueFactory(FileSystem),
spanFactory: ReactNativeSpanFactory,
platformExtensions: (spanFactory, spanContextStorage) => platformExtensions(appStartTime, clock, schema, spanFactory as ReactNativeSpanFactory, spanContextStorage)
platformExtensions: (spanFactory, spanContextStorage) => platformExtensions(appStartTime, clock, spanFactory as ReactNativeSpanFactory, spanContextStorage)
})

export default BugsnagPerformance
11 changes: 4 additions & 7 deletions packages/platforms/react-native/lib/platform-extensions.tsx
Original file line number Diff line number Diff line change
@@ -2,13 +2,13 @@ import type { Client, Clock, SpanContextStorage, SpanOptions } from '@bugsnag/co
import React from 'react'
import { Platform } from 'react-native'
import NativeBugsnagPerformance from './native'
import type { ReactNativeAttachConfiguration, ReactNativeConfiguration, ReactNativeSchema } from './config'
import type { ReactNativeAttachConfiguration, ReactNativeConfiguration } from './config'
import { createAppStartSpan } from './create-app-start-span'
import type { ReactNativeSpanFactory } from './span-factory'

type NavigationSpanOptions = Omit<SpanOptions, 'isFirstClass'>

export const platformExtensions = (appStartTime: number, clock: Clock, schema: ReactNativeSchema, spanFactory: ReactNativeSpanFactory, spanContextStorage: SpanContextStorage) => ({
export const platformExtensions = (appStartTime: number, clock: Clock, spanFactory: ReactNativeSpanFactory, spanContextStorage: SpanContextStorage) => ({
startNavigationSpan: function (routeName: string, spanOptions?: NavigationSpanOptions) {
const cleanOptions = spanFactory.validateSpanOptions(routeName, spanOptions)
const span = spanFactory.startNavigationSpan(cleanOptions.name, cleanOptions.options)
@@ -28,18 +28,15 @@ export const platformExtensions = (appStartTime: number, clock: Clock, schema: R
}
},
attach: function (config?: ReactNativeAttachConfiguration) {
const logger = schema.logger.validate(config?.logger) ? config.logger : schema.logger.defaultValue
const platform = Platform.OS === 'ios' ? 'Cocoa' : 'Android'
const isNativePerformanceAvailable = NativeBugsnagPerformance?.isNativePerformanceAvailable()
if (!isNativePerformanceAvailable) {
logger.warn(`Could not attach to native SDK. No compatible version of Bugsnag ${platform} Performance was found.`)
return
throw new Error(`Could not attach to native SDK. No compatible version of Bugsnag ${platform} Performance was found.`)
}

const nativeConfig = NativeBugsnagPerformance?.attachToNativeSDK()
if (!nativeConfig) {
logger.warn(`Could not attach to native SDK. Bugsnag ${platform} Performance has not been started.`)
return
throw new Error(`Could not attach to native SDK. Bugsnag ${platform} Performance has not been started.`)
}

const finalConfig: ReactNativeConfiguration = {
16 changes: 4 additions & 12 deletions packages/platforms/react-native/tests/client.test.ts
Original file line number Diff line number Diff line change
@@ -70,24 +70,16 @@ beforeEach(() => {

describe('React Native client tests', () => {
describe('attach()', () => {
it('logs a warning and noops if native performance is not available', () => {
it('throws an error if native performance is not available', () => {
turboModule.isNativePerformanceAvailable = jest.fn().mockReturnValue(false)

client = require('../lib/client').default
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {})

client.attach({})
expect(warnSpy).toHaveBeenCalledWith('Could not attach to native SDK. No compatible version of Bugsnag Cocoa Performance was found.')
expect(client.attach).toThrow('Could not attach to native SDK. No compatible version of Bugsnag Cocoa Performance was found.')
})

it('logs a warning and noops if native performance has not been started', () => {
it('throws an error if native performance has not been started', () => {
turboModule.attachToNativeSDK = jest.fn().mockReturnValue(null)

client = require('../lib/client').default
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {})

client.attach()
expect(warnSpy).toHaveBeenCalledWith('Could not attach to native SDK. Bugsnag Cocoa Performance has not been started.')
expect(client.attach).toThrow('Could not attach to native SDK. Bugsnag Cocoa Performance has not been started.')
})

it('starts the client using the native configuration', () => {
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ReactNativeSpanFactory } from '../lib/span-factory'
import createSchema from '../lib/config'
import { platformExtensions } from '../lib/platform-extensions'
import { createTestClient, IncrementingClock, InMemoryDelivery, VALID_API_KEY } from '@bugsnag/js-performance-test-utilities'

@@ -12,7 +11,7 @@ describe('startNavigationSpan', () => {
const testClient = createTestClient({
deliveryFactory: () => delivery,
spanFactory: ReactNativeSpanFactory,
platformExtensions: (spanFactory, spanContextStorage) => platformExtensions(0, clock, createSchema(), spanFactory as ReactNativeSpanFactory, spanContextStorage)
platformExtensions: (spanFactory, spanContextStorage) => platformExtensions(0, clock, spanFactory as ReactNativeSpanFactory, spanContextStorage)
})

testClient.start({ apiKey: VALID_API_KEY })