ZhuLinsen/daily_stock_analysis · warning · Error

Desktop share images require the configured backend origin

Error message

Desktop share images require the configured backend origin

What it means

Raised by PortfolioService.query_cash_ledger when both date_from and date_to are given and date_from > date_to. Same contract as the trades query: a pre-repository guard, mapped to validation_error, equal dates allowed.

Source

Thrown at apps/dsa-desktop/main.js:1366

  }

  let page;
  try {
    page = new URL(pageUrl);
  } catch (_error) {
    throw new Error('Desktop backend URL is unavailable');
  }

  let expectedOrigin = page.origin;
  if (expectedBackendOrigin) {
    try {
      expectedOrigin = new URL(expectedBackendOrigin).origin;
    } catch (_error) {
      throw new Error('Desktop backend origin is invalid');
    }
  }
  if (page.protocol !== 'http:' || !page.port || page.origin !== expectedOrigin) {
    throw new Error('Desktop share images require the configured backend origin');
  }

  return new URL(
    `/api/v1/history/${recordId}/share-image-html`,
    page.origin
  ).toString();
}

async function renderDesktopShareImage(
  recordId,
  {
    sourceWindow = mainWindow,
    BrowserWindowClass = BrowserWindow,
    backendOrigin = '',
  } = {}
) {
  if (!sourceWindow || sourceWindow.isDestroyed() || !sourceWindow.webContents) {
    throw new Error('Desktop window is unavailable');

View on GitHub (pinned to 5159bd72e8)

Solutions

  1. Swap or reject inverted ranges in the caller before the request
  2. Fix the off-by-order bug in the range computation (start = min, end = max)
  3. Add a client-side assert so inverted ranges fail fast with a clearer message
  4. Add UI validation that disables the query button when from > to

Example fix

# before
svc.query_cash_ledger(date_from=period_end, date_to=period_start)
# after
svc.query_cash_ledger(date_from=min(period_start, period_end), date_to=max(period_start, period_end))
Defensive patterns

Strategy: validation

Validate before calling

def safe_range(date_from, date_to):
    if date_from and date_to and date_from > date_to:
        date_from, date_to = date_to, date_from
    return date_from, date_to

Type guard

from datetime import date

def is_valid_date_range(d_from: date | None, d_to: date | None) -> bool:
    return d_from is None or d_to is None or d_from <= d_to

Try / catch

try:
    svc.query_cash_ledger(date_from=f, date_to=t)
except ValueError as exc:
    if "date_from must be <= date_to" in str(exc):
        raise HTTPBadRequest(detail="inverted date range") from exc
    raise

Prevention

When it happens

Trigger: GET /portfolio/cash-ledger?date_from=2025-12-31&date_to=2025-01-01, or query_cash_ledger(date_from=later, date_to=earlier). Only raises when both bounds are non-None and strictly inverted.

Common situations: Reused date-range widget wired to the cash-ledger endpoint with swapped field names; 'month to date' logic computing start/end backwards after a refactor; manual API testing with hardcoded dates in the wrong order.

Related errors


AI-assisted analysis of ZhuLinsen/daily_stock_analysis@5159bd72e8 (2026-08-15). Data as JSON: /api/errors/021ac01ac590ffc2. Report an issue: GitHub.