:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { MakeLogicType, connect, kea, key, path, props, selectors } from 'kea'
import { subscriptions } from 'kea-subscriptions'
import {
ErrorEventId,
ErrorEventProperties,
ErrorTrackingException,
ErrorTrackingRelease,
ErrorTrackingStackFrame,
FingerprintRecordPart,
} from 'lib/components/Errors/types'
import {
getAdditionalProperties,
getExceptionAttributes,
getExceptionList,
getExceptionRelease,
getFingerprintRecords,
getRecordingStatus,
getSessionId,
getSpanId,
getTraceId,
isReleaseIdMissingFromSDK,
} from 'lib/components/Errors/utils'
import { dayjs } from 'lib/dayjs'
import { preflightLogic } from 'lib/logic/preflightLogic'
import { isStoredCrashFirst } from './displayOrder'
import { KeyedStackFrameRecords, stackFrameLogic } from './Frame/stackFrameLogic'
import type { ExceptionAttributes } from './types'
export interface ErrorPropertiesLogicProps {
properties?: ErrorEventProperties
id: ErrorEventId
// Event timestamp; used to detect pre-normalization crash-first storage
// (see displayOrder.ts). Omitting it assumes canonical bottom-up storage.
timestamp?: string
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface errorPropertiesLogicValues {
isCloudOrDev: boolean | undefined // preflightLogic
stackFrameRecords: KeyedStackFrameRecords // stackFrameLogic
stackFrameRecordsLoading: boolean // stackFrameLogic
additionalProperties: Record<string, unknown>
exceptionAttributes: ExceptionAttributes | null
exceptionList: ErrorTrackingException[]
exceptionType: string | null
fingerprintRecords: FingerprintRecordPart[]
frames: ErrorTrackingStackFrame[]
framesStoredCrashFirst: boolean
getExceptionFingerprint: (excId: string) => FingerprintRecordPart | undefined
getFrameFingerprint: (frameRawId: string) => FingerprintRecordPart | undefined
hasStacktrace: boolean
properties: Record<string, any>
recordingStatus: string | undefined
release: ErrorTrackingRelease | null | undefined
releaseIdMissingFromSDK: boolean
sessionId: string | undefined
spanId: string | undefined
traceId: string | undefined
uuid: string
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface errorPropertiesLogicActions {
loadFromRawIds: (rawIds: string[]) => {
rawIds: string[]
} // stackFrameLogic
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface errorPropertiesLogicMeta {
key: string
__keaTypeGenInternalSelectorTypes: {
properties: (arg: ErrorEventProperties) => Record<string, any>
exceptionAttributes: (properties: Record<string, any>) => ExceptionAttributes | null
exceptionList: (properties: Record<string, any>) => ErrorTrackingException[]
exceptionType: (exceptionList: ErrorTrackingException[]) => string | null
additionalProperties: (
properties: Record<string, any>,
isCloudOrDev: boolean | undefined
) => Record<string, unknown>
fingerprintRecords: (properties: Record<string, any>) => FingerprintRecordPart[]
hasStacktrace: (exceptionList: ErrorTrackingException[]) => boolean
sessionId: (properties: Record<string, any>) => string | undefined
traceId: (properties: Record<string, any>) => string | undefined
spanId: (properties: Record<string, any>) => string | undefined
recordingStatus: (properties: Record<string, any>) => string | undefined
getExceptionFingerprint: (
fingerprintRecords: FingerprintRecordPart[]
) => (excId: string) => FingerprintRecordPart | undefined
getFrameFingerprint: (
fingerprintRecords: FingerprintRecordPart[]
) => (frameRawId: string) => FingerprintRecordPart | undefined
frames: (exceptionList: ErrorTrackingException[]) => ErrorTrackingStackFrame[]
uuid: (id: string) => string
framesStoredCrashFirst: (properties: Record<string, any>, arg: string | undefined) => boolean
release: (
properties: Record<string, any>,
frames: ErrorTrackingStackFrame[],
stackFrameRecords: KeyedStackFrameRecords
) => ErrorTrackingRelease | null | undefined
releaseIdMissingFromSDK: (
properties: Record<string, any>,
frames: ErrorTrackingStackFrame[],
stackFrameRecords: KeyedStackFrameRecords,
stackFrameRecordsLoading: boolean
) => boolean
}
}
export type errorPropertiesLogicType = MakeLogicType<
errorPropertiesLogicValues,
errorPropertiesLogicActions,
ErrorPropertiesLogicProps,
errorPropertiesLogicMeta
>
export const errorPropertiesLogic = kea<errorPropertiesLogicType>([
path((key) => ['components', 'Errors', 'errorPropertiesLogic', key]),
props({} as ErrorPropertiesLogicProps),
key((props) => props.id),
connect(() => ({
values: [preflightLogic, ['isCloudOrDev'], stackFrameLogic, ['stackFrameRecords', 'stackFrameRecordsLoading']],
actions: [stackFrameLogic, ['loadFromRawIds']],
})),
selectors({
properties: [
() => [(_, props) => props.properties as ErrorEventProperties],
(properties: ErrorEventProperties) => properties,
],
exceptionAttributes: [
(s) => [s.properties],
(properties: ErrorEventProperties) => (properties ? getExceptionAttributes(properties) : null),
],
exceptionList: [
(s) => [s.properties],
(properties: ErrorEventProperties) => {
return properties ? getExceptionList(properties) : []
},
],
exceptionType: [
(s) => [s.exceptionList],
(excList: ErrorTrackingException[]) => {
return excList[0]?.type || null
},
],
additionalProperties: [
(s) => [s.properties, s.isCloudOrDev],
(properties: ErrorEventProperties, isCloudOrDev: boolean | undefined) =>
properties ? getAdditionalProperties(properties, isCloudOrDev) : {},
],
fingerprintRecords: [
(s) => [s.properties],
(properties: ErrorEventProperties) => (properties ? getFingerprintRecords(properties) : []),
],
hasStacktrace: [(s) => [s.exceptionList], (excList: ErrorTrackingException[]) => hasStacktrace(excList)],
sessionId: [
(s) => [s.properties],
(properties: ErrorEventProperties) => (properties ? getSessionId(properties) : undefined),
],
traceId: [
(s) => [s.properties],
(properties: ErrorEventProperties) => (properties ? getTraceId(properties) : undefined),
],
spanId: [
(s) => [s.properties],
(properties: ErrorEventProperties) => (properties ? getSpanId(properties) : undefined),
],
recordingStatus: [
(s) => [s.properties],
(properties: ErrorEventProperties) => (properties ? getRecordingStatus(properties) : undefined),
],
getExceptionFingerprint: [
(s) => [s.fingerprintRecords],
(records: FingerprintRecordPart[]) => (excId: string) =>
records.find((record) => record.type === 'exception' && record.id === excId),
],
getFrameFingerprint: [
(s) => [s.fingerprintRecords],
(records: FingerprintRecordPart[]) => (frameRawId: string) =>
records.find((record) => record.type === 'frame' && record.raw_id === frameRawId),
],
frames: [
(s) => [s.exceptionList],
(exceptionList: ErrorTrackingException[]) => {
return exceptionList.flatMap((e) => {
const frames = e.stacktrace?.frames
return Array.isArray(frames) ? frames : []
}) as ErrorTrackingStackFrame[]
},
],
uuid: [(_, props) => [props.id], (id: ErrorEventId) => id],
framesStoredCrashFirst: [
(s) => [s.properties, (_: unknown, p: ErrorPropertiesLogicProps) => p.timestamp],
(properties: ErrorEventProperties, timestamp: string | undefined): boolean =>
isStoredCrashFirst(properties?.['$lib'] as string | undefined, timestamp),
],
release: [
(s) => [s.properties, s.frames, s.stackFrameRecords],
(
properties: ErrorEventProperties,
frames: ErrorTrackingStackFrame[],
stackFrameRecords: KeyedStackFrameRecords
) => {
const eventRelease = properties ? getExceptionRelease(properties) : undefined
if (eventRelease) {
return eventRelease
}
// Fall back to frame releases for exception events captured before Cymbal started
// writing the singular event-level release property.
if (!frames.length || Object.keys(stackFrameRecords).length === 0) {
return undefined
}
const rawIds = frames.map((f) => f.raw_id)
const relatedReleases: ErrorTrackingRelease[] = rawIds
.map((id) => stackFrameRecords[id]?.release)
.filter((r) => !!r) as ErrorTrackingRelease[]
const uniqueRelatedReleasesIds = [...new Set(relatedReleases.map((r) => r?.id))]
if (uniqueRelatedReleasesIds.length === 1) {
return relatedReleases[0]
}
const kaboomFrame = frames[frames.length - 1]
if (stackFrameRecords[kaboomFrame?.raw_id]?.release) {
return stackFrameRecords[kaboomFrame.raw_id].release
}
// get most recent release
const sortedReleases = relatedReleases.sort(
(a, b) => dayjs(b.created_at).unix() - dayjs(a.created_at).unix()
)
return sortedReleases[0]
},
],
releaseIdMissingFromSDK: [
(s) => [s.properties, s.frames, s.stackFrameRecords, s.stackFrameRecordsLoading],
(
properties: ErrorEventProperties,
frames: ErrorTrackingStackFrame[],
stackFrameRecords: KeyedStackFrameRecords,
stackFrameRecordsLoading: boolean
) => {
if (stackFrameRecordsLoading) {
return false
}
return isReleaseIdMissingFromSDK(properties, frames, stackFrameRecords)
},
],
}),
subscriptions(({ actions }) => ({
frames: (frames) => {
const rawIds: string[] = frames.map((frame: ErrorTrackingStackFrame) => frame.raw_id)
actions.loadFromRawIds(rawIds)
},
})),
])
function hasStacktrace(exceptionList: ErrorTrackingException[]): boolean {
return exceptionList.some((e) => !!e.stacktrace)
}