From 6203c20cd0673f55a29c546440e0c02f6998df5a Mon Sep 17 00:00:00 2001 From: Ariel Silvestri Date: Thu, 11 Aug 2022 16:08:04 -0400 Subject: [PATCH] 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 --- .changeset/spotty-files-wave.md | 5 +++++ packages/browser/package.json | 2 +- .../__tests__/analytics-pre-init.integration.test.ts | 2 +- .../src/browser/__tests__/csp-detection.test.ts | 5 ++--- .../browser/__tests__/standalone-analytics.test.ts | 9 +++------ .../src/browser/__tests__/standalone-errors.test.ts | 5 ++--- .../browser/src/browser/__tests__/standalone.test.ts | 5 ++--- packages/browser/src/browser/index.ts | 11 +++++++++-- .../plugins/ajs-destination/__tests__/index.test.ts | 5 ++--- 9 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 .changeset/spotty-files-wave.md diff --git a/.changeset/spotty-files-wave.md b/.changeset/spotty-files-wave.md new file mode 100644 index 000000000..e0f858155 --- /dev/null +++ b/.changeset/spotty-files-wave.md @@ -0,0 +1,5 @@ +--- +'@segment/analytics-next': minor +--- + +Enhances console error logging when requests to settings api fail diff --git a/packages/browser/package.json b/packages/browser/package.json index 21e434362..5c985d94a 100644 --- a/packages/browser/package.json +++ b/packages/browser/package.json @@ -43,7 +43,7 @@ "size-limit": [ { "path": "dist/umd/index.js", - "limit": "25.9 KB" + "limit": "25.95 KB" } ], "dependencies": { diff --git a/packages/browser/src/browser/__tests__/analytics-pre-init.integration.test.ts b/packages/browser/src/browser/__tests__/analytics-pre-init.integration.test.ts index 1b92b2547..34ec737a0 100644 --- a/packages/browser/src/browser/__tests__/analytics-pre-init.integration.test.ts +++ b/packages/browser/src/browser/__tests__/analytics-pre-init.integration.test.ts @@ -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) diff --git a/packages/browser/src/browser/__tests__/csp-detection.test.ts b/packages/browser/src/browser/__tests__/csp-detection.test.ts index e2abe78f9..ffee9a09b 100644 --- a/packages/browser/src/browser/__tests__/csp-detection.test.ts +++ b/packages/browser/src/browser/__tests__/csp-detection.test.ts @@ -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: { @@ -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() diff --git a/packages/browser/src/browser/__tests__/standalone-analytics.test.ts b/packages/browser/src/browser/__tests__/standalone-analytics.test.ts index 91a6e1a6e..de6c63a19 100644 --- a/packages/browser/src/browser/__tests__/standalone-analytics.test.ts +++ b/packages/browser/src/browser/__tests__/standalone-analytics.test.ts @@ -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() @@ -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() @@ -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 diff --git a/packages/browser/src/browser/__tests__/standalone-errors.test.ts b/packages/browser/src/browser/__tests__/standalone-errors.test.ts index e6804bbf9..b402b26b3 100644 --- a/packages/browser/src/browser/__tests__/standalone-errors.test.ts +++ b/packages/browser/src/browser/__tests__/standalone-errors.test.ts @@ -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: { @@ -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() diff --git a/packages/browser/src/browser/__tests__/standalone.test.ts b/packages/browser/src/browser/__tests__/standalone.test.ts index abaec23a9..a163ad8fb 100644 --- a/packages/browser/src/browser/__tests__/standalone.test.ts +++ b/packages/browser/src/browser/__tests__/standalone.test.ts @@ -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: { @@ -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() diff --git a/packages/browser/src/browser/index.ts b/packages/browser/src/browser/index.ts index dea724d56..cce51a445 100644 --- a/packages/browser/src/browser/index.ts +++ b/packages/browser/src/browser/index.ts @@ -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 }) } diff --git a/packages/browser/src/plugins/ajs-destination/__tests__/index.test.ts b/packages/browser/src/plugins/ajs-destination/__tests__/index.test.ts index 40c625394..ad1510cb0 100644 --- a/packages/browser/src/plugins/ajs-destination/__tests__/index.test.ts +++ b/packages/browser/src/plugins/ajs-destination/__tests__/index.test.ts @@ -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: { @@ -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()