Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into metric-error-prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisradek committed Aug 11, 2022
2 parents c876aa1 + 6203c20 commit 9474a25
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 22 deletions.
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 @@ -126,7 +126,7 @@ describe('Pre-initialization', () => {
}
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) => {
if (!res.ok) {
return res.text().then((errorResponseMessage) => {
throw new Error(errorResponseMessage)
})
}
return res.json()
})
.catch((err) => {
console.warn('Failed to load settings', err)
console.error(err.message)
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

0 comments on commit 9474a25

Please sign in to comment.