posthog-node 5.61.0 → 5.61.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +3 -2
- package/dist/client.mjs +3 -2
- package/dist/extensions/error-tracking/autocapture.d.ts +32 -2
- package/dist/extensions/error-tracking/autocapture.d.ts.map +1 -1
- package/dist/extensions/error-tracking/autocapture.js +337 -35
- package/dist/extensions/error-tracking/autocapture.mjs +324 -28
- package/dist/extensions/error-tracking/index.d.ts +16 -1
- package/dist/extensions/error-tracking/index.d.ts.map +1 -1
- package/dist/extensions/error-tracking/index.js +23 -8
- package/dist/extensions/error-tracking/index.mjs +24 -9
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/version.mjs +1 -1
- package/package.json +1 -1
- package/src/client.ts +4 -2
- package/src/extensions/error-tracking/autocapture.ts +579 -57
- package/src/extensions/error-tracking/index.ts +48 -10
- package/src/version.ts +1 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { capturePendingExceptions, claimExplicitCapture, startExceptionAutocapture } from './autocapture'
|
|
2
|
+
import type { AutocaptureClient } from './autocapture'
|
|
2
3
|
import { PostHogBackendClient } from '@/client'
|
|
3
4
|
import type { ContextData } from '../context/types'
|
|
4
5
|
import { snapshotEventContext } from '../context/event-context'
|
|
@@ -15,6 +16,8 @@ export default class ErrorTracking {
|
|
|
15
16
|
private _exceptionAutocaptureEnabled: boolean
|
|
16
17
|
private _rateLimiter: BucketedRateLimiter<string>
|
|
17
18
|
private _logger: Logger
|
|
19
|
+
private _autocaptureClient?: AutocaptureClient
|
|
20
|
+
private _stopAutocapture?: () => void
|
|
18
21
|
|
|
19
22
|
constructor(client: PostHogBackendClient, options: PostHogOptions, _logger: Logger) {
|
|
20
23
|
this.client = client
|
|
@@ -76,10 +79,44 @@ export default class ErrorTracking {
|
|
|
76
79
|
})
|
|
77
80
|
}
|
|
78
81
|
|
|
82
|
+
// Clients with the same project token and host send to the same project.
|
|
83
|
+
private get destinationKey(): string {
|
|
84
|
+
return `${this.client.apiKey}@${this.client.host}`
|
|
85
|
+
}
|
|
86
|
+
|
|
79
87
|
private startAutocaptureIfEnabled(): void {
|
|
80
88
|
if (this.isEnabled()) {
|
|
81
|
-
|
|
82
|
-
|
|
89
|
+
this._autocaptureClient = {
|
|
90
|
+
key: this.destinationKey,
|
|
91
|
+
capture: (exception, hint) => this.onException(exception, hint),
|
|
92
|
+
getContext: () => this.client.getContext(),
|
|
93
|
+
flush: () => this.client.flush(),
|
|
94
|
+
shutdown: () => this.client.shutdown(SHUTDOWN_TIMEOUT),
|
|
95
|
+
}
|
|
96
|
+
this._stopAutocapture = startExceptionAutocapture(this._autocaptureClient)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Whether captureException should capture this error. False when exception autocapture already sent the same error
|
|
102
|
+
* object to this project in this turn of the event loop, for example when a framework passes an error from its own
|
|
103
|
+
* process listener to an error hook that calls captureException.
|
|
104
|
+
*/
|
|
105
|
+
claimExplicitCapture(error: unknown): boolean {
|
|
106
|
+
return claimExplicitCapture(error, this.destinationKey)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Captures at once the exceptions autocapture holds until the end of the turn, so that this client's flush or
|
|
111
|
+
* shutdown sends them: an app's crash handler often flushes or shuts the client down and exits before the turn ends.
|
|
112
|
+
* The capture can land in another client of the project, the one whose request context is active, so this client
|
|
113
|
+
* waits for that client's flush too.
|
|
114
|
+
*/
|
|
115
|
+
capturePendingExceptions(): void {
|
|
116
|
+
for (const owner of capturePendingExceptions()) {
|
|
117
|
+
if (owner !== this._autocaptureClient) {
|
|
118
|
+
void this.client.addPendingPromise(owner.flush().catch(() => undefined))
|
|
119
|
+
}
|
|
83
120
|
}
|
|
84
121
|
}
|
|
85
122
|
|
|
@@ -106,21 +143,22 @@ export default class ErrorTracking {
|
|
|
106
143
|
}
|
|
107
144
|
return this.client._capturePreparedEvent(eventMessage, false)
|
|
108
145
|
}
|
|
109
|
-
})()
|
|
146
|
+
})().catch((error) => {
|
|
147
|
+
// shutdown() waits for every pending promise and stops before it flushes if one rejects, so an error that
|
|
148
|
+
// cannot be turned into an event would take the others down with it.
|
|
149
|
+
this._logger.error('Failed to capture an exception:', error)
|
|
150
|
+
})
|
|
110
151
|
)
|
|
111
152
|
}
|
|
112
153
|
|
|
113
|
-
private async onFatalError(exception: Error): Promise<void> {
|
|
114
|
-
console.error(exception)
|
|
115
|
-
await this.client.shutdown(SHUTDOWN_TIMEOUT)
|
|
116
|
-
globalThis.process.exit(1)
|
|
117
|
-
}
|
|
118
|
-
|
|
119
154
|
isEnabled(): boolean {
|
|
120
155
|
return !this.client.isDisabled && this._exceptionAutocaptureEnabled
|
|
121
156
|
}
|
|
122
157
|
|
|
123
158
|
shutdown(): void {
|
|
124
159
|
this._rateLimiter.stop()
|
|
160
|
+
this.capturePendingExceptions()
|
|
161
|
+
this._stopAutocapture?.()
|
|
162
|
+
this._stopAutocapture = undefined
|
|
125
163
|
}
|
|
126
164
|
}
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "5.61.
|
|
1
|
+
export const version = "5.61.1"
|