Skip to content

Commit 6203c20

Browse files
Improved error message logging in console (#573)
* More verbose error logging * Test updates * Slightly up pacakge size limit * Changeset * Update tests to use factory functions * Throw an error rather than returning extraneous info in response
1 parent 13f7c71 commit 6203c20

File tree

9 files changed

+27
-22
lines changed

9 files changed

+27
-22
lines changed

.changeset/spotty-files-wave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-next': minor
3+
---
4+
5+
Enhances console error logging when requests to settings api fail

packages/browser/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"size-limit": [
4444
{
4545
"path": "dist/umd/index.js",
46-
"limit": "25.9 KB"
46+
"limit": "25.95 KB"
4747
}
4848
],
4949
"dependencies": {

packages/browser/src/browser/__tests__/analytics-pre-init.integration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ describe('Pre-initialization', () => {
126126
}
127127
mockFetchSettingsErrorResponse(err)
128128
const consoleSpy = jest
129-
.spyOn(console, 'warn')
129+
.spyOn(console, 'error')
130130
.mockImplementationOnce(() => {})
131131
AnalyticsBrowser.load({ writeKey: 'abc' })
132132
await sleep(500)

packages/browser/src/browser/__tests__/csp-detection.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { LegacySettings } from '..'
44
import { onCSPError } from '../../lib/csp-detection'
55
import { pWhile } from '../../lib/p-while'
66
import { snippet } from '../../tester/__fixtures__/segment-snippet'
7+
import * as Factory from '../../test-helpers/factories'
78

89
const cdnResponse: LegacySettings = {
910
integrations: {
@@ -24,9 +25,7 @@ const cdnResponse: LegacySettings = {
2425
},
2526
}
2627

27-
const fetchSettings = Promise.resolve({
28-
json: () => Promise.resolve(cdnResponse),
29-
})
28+
const fetchSettings = Factory.createSuccess(cdnResponse)
3029

3130
jest.mock('unfetch', () => {
3231
return jest.fn()

packages/browser/src/browser/__tests__/standalone-analytics.test.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { install, AnalyticsSnippet } from '../standalone-analytics'
66
import unfetch from 'unfetch'
77
import { PersistedPriorityQueue } from '../../lib/priority-queue/persisted'
88
import { sleep } from '../../test-helpers/sleep'
9+
import * as Factory from '../../test-helpers/factories'
910

1011
const track = jest.fn()
1112
const identify = jest.fn()
@@ -32,12 +33,7 @@ jest.mock('@/core/analytics', () => ({
3233
}),
3334
}))
3435

35-
const fetchSettings = Promise.resolve({
36-
json: () =>
37-
Promise.resolve({
38-
integrations: {},
39-
}),
40-
})
36+
const fetchSettings = Factory.createSuccess({ integrations: {} })
4137

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

8379
jest.spyOn(console, 'warn').mockImplementationOnce(() => {})
80+
jest.spyOn(console, 'error').mockImplementationOnce(() => {})
8481

8582
windowSpy.mockImplementation(() => {
8683
return jsd.window as unknown as Window & typeof globalThis

packages/browser/src/browser/__tests__/standalone-errors.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { snippet } from '../../tester/__fixtures__/segment-snippet'
44
import { pWhile } from '../../lib/p-while'
55
import unfetch from 'unfetch'
66
import { RemoteMetrics } from '../../core/stats/remote-metrics'
7+
import * as Factory from '../../test-helpers/factories'
78

89
const cdnResponse: LegacySettings = {
910
integrations: {
@@ -24,9 +25,7 @@ const cdnResponse: LegacySettings = {
2425
},
2526
}
2627

27-
const fetchSettings = Promise.resolve({
28-
json: () => Promise.resolve(cdnResponse),
29-
})
28+
const fetchSettings = Factory.createSuccess(cdnResponse)
3029

3130
jest.mock('unfetch', () => {
3231
return jest.fn()

packages/browser/src/browser/__tests__/standalone.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import unfetch from 'unfetch'
33
import { LegacySettings } from '..'
44
import { pWhile } from '../../lib/p-while'
55
import { snippet } from '../../tester/__fixtures__/segment-snippet'
6+
import * as Factory from '../../test-helpers/factories'
67

78
const cdnResponse: LegacySettings = {
89
integrations: {
@@ -23,9 +24,7 @@ const cdnResponse: LegacySettings = {
2324
},
2425
}
2526

26-
const fetchSettings = Promise.resolve({
27-
json: () => Promise.resolve(cdnResponse),
28-
})
27+
const fetchSettings = Factory.createSuccess(cdnResponse)
2928

3029
jest.mock('unfetch', () => {
3130
return jest.fn()

packages/browser/src/browser/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,16 @@ export function loadLegacySettings(
8383
const baseUrl = cdnURL ?? getCDN()
8484

8585
return fetch(`${baseUrl}/v1/projects/${writeKey}/settings`)
86-
.then((res) => res.json())
86+
.then((res) => {
87+
if (!res.ok) {
88+
return res.text().then((errorResponseMessage) => {
89+
throw new Error(errorResponseMessage)
90+
})
91+
}
92+
return res.json()
93+
})
8794
.catch((err) => {
88-
console.warn('Failed to load settings', err)
95+
console.error(err.message)
8996
throw err
9097
})
9198
}

packages/browser/src/plugins/ajs-destination/__tests__/index.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Plan } from '../../../core/events'
99
import { tsubMiddleware } from '../../routing-middleware'
1010
import { AMPLITUDE_WRITEKEY } from '../../../test-helpers/test-writekeys'
1111
import { PersistedPriorityQueue } from '../../../lib/priority-queue/persisted'
12+
import * as Factory from '../../../test-helpers/factories'
1213

1314
const cdnResponse: LegacySettings = {
1415
integrations: {
@@ -66,9 +67,7 @@ const cdnResponse: LegacySettings = {
6667
},
6768
}
6869

69-
const fetchSettings = Promise.resolve({
70-
json: () => Promise.resolve(cdnResponse),
71-
})
70+
const fetchSettings = Factory.createSuccess(cdnResponse)
7271

7372
jest.mock('unfetch', () => {
7473
return jest.fn()

0 commit comments

Comments
 (0)