Skip to content

Commit

Permalink
fix unhandled error (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite authored Dec 18, 2023
1 parent 77c1358 commit 5e6b427
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
4 changes: 4 additions & 0 deletions posthog-node/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 3.2.1 - 2023-12-15

1. Fixes issue where a background refresh of feature flags could throw an unhandled error. It now emits to be detected by `.on('error', ...)`

# 3.2.0 - 2023-12-05

1. Fixes issues with Axios imports for non-node environments like Cloudflare workers
Expand Down
2 changes: 1 addition & 1 deletion posthog-node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "posthog-node",
"version": "3.2.0",
"version": "3.2.1",
"description": "PostHog Node.js integration",
"repository": "PostHog/posthog-node",
"scripts": {
Expand Down
7 changes: 5 additions & 2 deletions posthog-node/src/feature-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type FeatureFlagsPollerOptions = {
pollingInterval: number
timeout?: number
fetch?: (url: string, options: PostHogFetchOptions) => Promise<PostHogFetchResponse>
onError?: (error: Error) => void
}

class FeatureFlagsPoller {
Expand All @@ -53,6 +54,7 @@ class FeatureFlagsPoller {
poller?: NodeJS.Timeout
fetch: (url: string, options: PostHogFetchOptions) => Promise<PostHogFetchResponse>
debugMode: boolean = false
onError?: (error: Error) => void

constructor({
pollingInterval,
Expand All @@ -75,6 +77,7 @@ class FeatureFlagsPoller {
this.poller = undefined
// NOTE: as any is required here as the AbortSignal typing is slightly misaligned but works just fine
this.fetch = options.fetch || fetch
this.onError = options.onError

void this.loadFeatureFlags()
}
Expand Down Expand Up @@ -177,7 +180,7 @@ class FeatureFlagsPoller {
if (e instanceof InconclusiveMatchError) {
// do nothing
} else if (e instanceof Error) {
console.error(`Error computing flag locally: ${flag.key}: ${e}`)
this.onError?.(new Error(`Error computing flag locally: ${flag.key}: ${e}`))
}
fallbackToDecide = true
}
Expand Down Expand Up @@ -395,7 +398,7 @@ class FeatureFlagsPoller {
// if an error that is not an instance of ClientError is thrown
// we silently ignore the error when reloading feature flags
if (err instanceof ClientError) {
throw err
this.onError?.(err)
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions posthog-node/src/posthog-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ export class PostHog extends PostHogCoreStateless implements PostHogNodeV1 {
timeout: options.requestTimeout ?? 10000, // 10 seconds
host: this.host,
fetch: options.fetch,
onError: (err: Error) => {
this._events.emit('error', err)
},
})
}
this.distinctIdHasSentFlagCalls = {}
Expand Down

0 comments on commit 5e6b427

Please sign in to comment.