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 all 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 @@ -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) => {
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 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