hcengineering/platform · error · PlatformError

core.status.ObjectNotFound

core.status.ObjectNotFound

Error message

ObjectNotFound

What it means

MemDb.getObject performs a synchronous in-memory lookup by document _id and throws a PlatformError with status core.status.ObjectNotFound when no object with that id exists in the database. Unlike findObject (which returns undefined), getObject treats a missing object as a hard failure.

Source

Thrown at foundations/core/packages/core/src/memdb.ts:83

  private getByIdQuery<T extends Doc>(query: DocumentQuery<T>, _class: Ref<Class<T>>): T[] {
    const result: T[] = []
    if (typeof query._id === 'string') {
      const obj = this.objectById.get(query._id) as T
      if (obj !== undefined && this.hierarchy.isDerived(obj._class, _class)) result.push(obj)
    } else if (query._id?.$in !== undefined) {
      const ids = new Set(query._id.$in)
      for (const id of ids) {
        const obj = this.objectById.get(id) as T
        if (obj !== undefined && this.hierarchy.isDerived(obj._class, _class)) result.push(obj)
      }
    }
    return result
  }

  getObject<T extends Doc>(_id: Ref<T>): T {
    const doc = this.objectById.get(_id)
    if (doc === undefined) {
      throw new PlatformError(new Status(Severity.ERROR, core.status.ObjectNotFound, { _id }))
    }
    return doc as T
  }

  findObject<T extends Doc>(_id: Ref<T>): T | undefined {
    const doc = this.objectById.get(_id)
    return doc as T
  }

  private async getLookupValue<T extends Doc>(
    _class: Ref<Class<T>>,
    doc: T,
    lookup: Lookup<T>,
    result: LookupData<T>
  ): Promise<void> {
    for (const key in lookup) {
      if (key === '_id') {
        await this.getReverseLookupValue(doc, lookup, result)

View on GitHub (pinned to 63e28dc964)

Solutions

  1. Use findObject(...) and handle undefined before calling getObject
  2. Validate that the id exists (e.g. client.findOne) before dereferencing
  3. Check you are querying the same db/client instance where the doc was created
  4. Catch the PlatformError and test err.status?.code === core.status.ObjectNotFound to handle the missing case

Example fix

// before
const doc = db.getObject(id as Ref<Doc>)
// after
const doc = db.findObject(id as Ref<Doc>)
if (doc === undefined) {
  throw new Error(`Document ${id} not found or deleted`)
}
Defensive patterns

Strategy: type-guard

Validate before calling

const doc = db.findObject(id)
if (doc === undefined) {
  // handle missing document without throwing
  return null
}
// safe: doc is defined

Type guard

function exists<T extends Doc>(db: MemDb, id: Ref<T>): boolean {
  return db.findObject(id) !== undefined
}

Try / catch

try {
  const doc = db.getObject(_id)
  // use doc
} catch (err: any) {
  if (err?.status?.code === core.status.ObjectNotFound) {
    return null // treat as missing
  }
  throw err
}

Prevention

When it happens

Trigger: Calling getObject (or the doc() convenience wrapper) with an _id that was never created, was already removed, or belongs to a different db instance (e.g. fetching by a raw string id from a URL before validating existence).

Common situations: Stale references after a document was deleted by another user; passing query params/URL ids straight into getObject; reading from a filtered or model-only memdb that lacks the document; id typo.

Related errors


AI-assisted analysis of hcengineering/platform@63e28dc964 (2026-08-29). Data as JSON: /api/errors/16b758e0a283366f. Report an issue: GitHub.