BabylonJS/Babylon.js · error

String(error)

Error message

String(error)

What it means

In addAnchorPointUsingHitTestResultAsync, once the native createAnchor call exists, any failure inside it (e.g. the anchor could not be created by the runtime) rejects and is rethrown as a plain Error whose message is String(error), preserving the original as cause. This is a pass-through wrapper: the actual diagnostic lives in the cause and in the native error text.

Source

Thrown at packages/dev/core/src/XR/features/WebXRAnchorSystem.pure.ts:230

        );
        if (!hitTestResult.xrHitResult.createAnchor) {
            this.detach();
            throw new Error("Anchors not enabled in this environment/browser");
        } else {
            try {
                const nativeAnchor = await hitTestResult.xrHitResult.createAnchor(m);
                return await new Promise<IWebXRAnchor>((resolve, reject) => {
                    this._futureAnchors.push({
                        nativeAnchor,
                        resolved: false,
                        submitted: true,
                        xrTransformation: m,
                        resolve,
                        reject,
                    });
                });
            } catch (error) {
                throw new Error(String(error), { cause: error });
            }
        }
    }

    /**
     * Add a new anchor at a specific position and rotation
     * This function will add a new anchor per default in the next available frame. Unless forced, the createAnchor function
     * will be called in the next xrFrame loop to make sure that the anchor can be created correctly.
     * An anchor is tracked only after it is added to the trackerAnchors in xrFrame. The promise returned here does not yet guaranty that.
     * Use onAnchorAddedObservable to get newly added anchors if you require tracking guaranty.
     *
     * @param position the position in which to add an anchor
     * @param rotationQuaternion an optional rotation for the anchor transformation
     * @param forceCreateInCurrentFrame force the creation of this anchor in the current frame. Must be called inside xrFrame loop!
     * @returns A promise that fulfills when babylon has created the corresponding WebXRAnchor object and tracking has begun
     */
    public async addAnchorAtPositionAndRotationAsync(
        position: Vector3,

View on GitHub (pinned to 0592b347b8)

Solutions

  1. Inspect error.cause for the original native rejection (message, name)
  2. Retry creation at a slightly adjusted pose or after the next frame
  3. Check that the XR session is still active before creating anchors
  4. Cap the number of live anchors and delete unused ones before creating new ones

Example fix

// before
try {
  const anchor = await anchorSystem.addAnchorPointUsingHitTestResultAsync(hitTestResult);
} catch (e) { console.log(e); }
// after
try {
  const anchor = await anchorSystem.addAnchorPointUsingHitTestResultAsync(hitTestResult);
} catch (e) {
  console.error('anchor creation failed:', (e as Error).cause ?? e);
}
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure session alive and anchors enabled before creating
if (!xrSessionManager.session || typeof hitTestResult.xrHitResult.createAnchor !== 'function') {
  throw new Error('anchors not usable in current session');
}

Type guard

function isAnchorError(e: unknown): e is Error & { cause: unknown } {
  return e instanceof Error && 'cause' in e;
}

Try / catch

try {
  const anchor = await anchorSystem.addAnchorPointUsingHitTestResultAsync(hitTestResult);
} catch (e) {
  const cause = (e as Error & { cause?: unknown }).cause;
  console.error('native anchor creation failed:', cause);
  // retry next frame or fall back
}

Prevention

When it happens

Trigger: Calling addAnchorPointUsingHitTestResultAsync when hitTestResult.xrHitResult.createAnchor(m) throws or rejects — for example the runtime refuses anchor creation at that pose, the session ended concurrently, or the native implementation returned an error (DOMException/NotAllowedError etc.).

Common situations: Session terminating while creating anchors, creating too many anchors and hitting a runtime limit, device trackers unavailable, or runtimes that implement createAnchor but fail at call time.

Related errors


AI-assisted analysis of BabylonJS/Babylon.js@0592b347b8 (2026-08-30). Data as JSON: /api/errors/89aff862753c9d05. Report an issue: GitHub.