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

Improved error message logging in console #573

Merged
merged 7 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
5 changes: 5 additions & 0 deletions .changeset/spotty-files-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-next': minor
---

Enhances console error logging when requests to settings api fail
2 changes: 1 addition & 1 deletion packages/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"size-limit": [
{
"path": "dist/umd/index.js",
"limit": "25.9 KB"
"limit": "25.95 KB"
}
],
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Context } from '../../core/context'
import * as Factory from '../../test-helpers/factories'
import { sleep } from '../../test-helpers/sleep'
import { setGlobalCDNUrl } from '../../lib/parse-cdn'
import { User } from '../../core/user'

jest.mock('unfetch')

Expand Down Expand Up @@ -60,38 +59,19 @@ describe('Pre-initialization', () => {
expect(trackSpy).toBeCalledTimes(1)
})

test('"return types should not change over the lifecycle for async methods', async () => {
test('"return types should not change over the lifecycle for ordinary methods', async () => {
const ajsBrowser = AnalyticsBrowser.load({ writeKey })

const trackCtxPromise1 = ajsBrowser.track('foo', { name: 'john' })
expect(trackCtxPromise1).toBeInstanceOf(Promise)
await ajsBrowser
const ctx1 = await trackCtxPromise1
expect(ctx1).toBeInstanceOf(Context)

// loaded
const trackCtxPromise2 = ajsBrowser.track('foo', { name: 'john' })
expect(trackCtxPromise2).toBeInstanceOf(Promise)

expect(await trackCtxPromise1).toBeInstanceOf(Context)
expect(await trackCtxPromise2).toBeInstanceOf(Context)
})

test('return types should not change over lifecycle for sync methods', async () => {
const ajsBrowser = AnalyticsBrowser.load({ writeKey })
const user = ajsBrowser.user()
expect(user).toBeInstanceOf(Promise)
await ajsBrowser

// loaded
const user2 = ajsBrowser.user()
expect(user2).toBeInstanceOf(Promise)

expect(await user).toBeInstanceOf(User)
expect(await user2).toBeInstanceOf(User)
})

test('version should return version', async () => {
const ajsBrowser = AnalyticsBrowser.load({ writeKey })
expect(typeof ajsBrowser.VERSION).toBe('string')
const ctx2 = await trackCtxPromise2
arielsilvestri marked this conversation as resolved.
Show resolved Hide resolved
expect(ctx2).toBeInstanceOf(Context)
})

test('If a user sends multiple events, all of those event gets flushed', async () => {
Expand Down Expand Up @@ -123,10 +103,11 @@ describe('Pre-initialization', () => {
status: 403,
statusText: 'Forbidden',
json: undefined,
text: () => Promise.resolve('text'),
}
mockFetchSettingsErrorResponse(err)
const consoleSpy = jest
.spyOn(console, 'warn')
.spyOn(console, 'error')
.mockImplementationOnce(() => {})
AnalyticsBrowser.load({ writeKey: 'abc' })
await sleep(500)
Expand Down
5 changes: 2 additions & 3 deletions packages/browser/src/browser/__tests__/csp-detection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { LegacySettings } from '..'
import { onCSPError } from '../../lib/csp-detection'
import { pWhile } from '../../lib/p-while'
import { snippet } from '../../tester/__fixtures__/segment-snippet'
import * as Factory from '../../test-helpers/factories'

const cdnResponse: LegacySettings = {
integrations: {
Expand All @@ -24,9 +25,7 @@ const cdnResponse: LegacySettings = {
},
}

const fetchSettings = Promise.resolve({
json: () => Promise.resolve(cdnResponse),
})
const fetchSettings = Factory.createSuccess(cdnResponse)

jest.mock('unfetch', () => {
return jest.fn()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { install, AnalyticsSnippet } from '../standalone-analytics'
import unfetch from 'unfetch'
import { PersistedPriorityQueue } from '../../lib/priority-queue/persisted'
import { sleep } from '../../test-helpers/sleep'
import * as Factory from '../../test-helpers/factories'

const track = jest.fn()
const identify = jest.fn()
Expand All @@ -32,12 +33,7 @@ jest.mock('@/core/analytics', () => ({
}),
}))

const fetchSettings = Promise.resolve({
json: () =>
Promise.resolve({
integrations: {},
}),
})
const fetchSettings = Factory.createSuccess({ integrations: {} })

jest.mock('unfetch', () => {
return jest.fn()
Expand Down Expand Up @@ -81,6 +77,7 @@ describe('standalone bundle', () => {
const documentSpy = jest.spyOn(global, 'document', 'get')

jest.spyOn(console, 'warn').mockImplementationOnce(() => {})
jest.spyOn(console, 'error').mockImplementationOnce(() => {})

windowSpy.mockImplementation(() => {
return jsd.window as unknown as Window & typeof globalThis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { snippet } from '../../tester/__fixtures__/segment-snippet'
import { pWhile } from '../../lib/p-while'
import unfetch from 'unfetch'
import { RemoteMetrics } from '../../core/stats/remote-metrics'
import * as Factory from '../../test-helpers/factories'

const cdnResponse: LegacySettings = {
integrations: {
Expand All @@ -24,9 +25,7 @@ const cdnResponse: LegacySettings = {
},
}

const fetchSettings = Promise.resolve({
json: () => Promise.resolve(cdnResponse),
})
const fetchSettings = Factory.createSuccess(cdnResponse)

jest.mock('unfetch', () => {
return jest.fn()
Expand Down
5 changes: 2 additions & 3 deletions packages/browser/src/browser/__tests__/standalone.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import unfetch from 'unfetch'
import { LegacySettings } from '..'
import { pWhile } from '../../lib/p-while'
import { snippet } from '../../tester/__fixtures__/segment-snippet'
import * as Factory from '../../test-helpers/factories'

const cdnResponse: LegacySettings = {
integrations: {
Expand All @@ -23,9 +24,7 @@ const cdnResponse: LegacySettings = {
},
}

const fetchSettings = Promise.resolve({
json: () => Promise.resolve(cdnResponse),
})
const fetchSettings = Factory.createSuccess(cdnResponse)

jest.mock('unfetch', () => {
return jest.fn()
Expand Down
11 changes: 9 additions & 2 deletions packages/browser/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,16 @@ export function loadLegacySettings(
const baseUrl = cdnURL ?? getCDN()

return fetch(`${baseUrl}/v1/projects/${writeKey}/settings`)
.then((res) => res.json())
.then((res) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Can we get some client tests?

if (!res.ok) {
return res.text().then((errorResponseMessage) => {
chrisradek marked this conversation as resolved.
Show resolved Hide resolved
throw { ...res, errorResponseMessage }
arielsilvestri marked this conversation as resolved.
Show resolved Hide resolved
})
}
return res.json()
})
.catch((err) => {
console.warn('Failed to load settings', err)
console.error(err.errorResponseMessage)
throw err
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Plan } from '../../../core/events'
import { tsubMiddleware } from '../../routing-middleware'
import { AMPLITUDE_WRITEKEY } from '../../../test-helpers/test-writekeys'
import { PersistedPriorityQueue } from '../../../lib/priority-queue/persisted'
import * as Factory from '../../../test-helpers/factories'

const cdnResponse: LegacySettings = {
integrations: {
Expand Down Expand Up @@ -66,9 +67,7 @@ const cdnResponse: LegacySettings = {
},
}

const fetchSettings = Promise.resolve({
json: () => Promise.resolve(cdnResponse),
})
const fetchSettings = Factory.createSuccess(cdnResponse)

jest.mock('unfetch', () => {
return jest.fn()
Expand Down