Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/langbot/pkg/persistence/tenant_uow.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class _ScopedSessionGuardState:
'now': sqlalchemy.sql.functions.now,
'sum': sqlalchemy.sql.functions.sum,
}
_ALLOWED_SCOPED_GENERIC_FUNCTIONS = frozenset({'date_trunc', 'length', 'nullif'})
_ALLOWED_SCOPED_GENERIC_FUNCTIONS = frozenset({'date_trunc', 'length', 'nullif', 'strftime'})
_ALLOWED_SCOPED_CUSTOM_OPERATORS = frozenset({'<=>'})
_ALLOWED_SCOPED_STATEMENT_TYPES = (
sqlalchemy.sql.dml.UpdateBase,
Expand Down
1 change: 1 addition & 0 deletions tests/unit_tests/persistence/test_tenant_uow.py
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,7 @@ async def test_scoped_session_rejects_raw_or_unapproved_sql(
sa.func.date_trunc('hour', sa.column('timestamp')),
sa.func.length(sa.literal('value')),
sa.func.nullif(sa.literal('value'), sa.literal('')),
sa.func.strftime('%Y-%m-%d %H:00', sa.column('timestamp')),
),
sa.select(sa.column('embedding').op('<=>')(sa.literal([0.1]))),
sa.select(sa.cast(sa.column('embedding'), Vector(384))),
Expand Down
10 changes: 9 additions & 1 deletion web/src/app/home/monitoring/components/TokenMonitoring.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ function formatNumber(n: number): string {
return n.toLocaleString();
}

function getErrorMessage(error: unknown): string {
if (error instanceof Error) return error.message;
if (typeof error === 'object' && error !== null && 'msg' in error) {
return String((error as { msg?: unknown }).msg ?? '');
}
return String(error);
}

const TOOLTIP_STYLE: React.CSSProperties = {
backgroundColor: 'var(--card)',
border: '1px solid var(--border)',
Expand Down Expand Up @@ -152,7 +160,7 @@ export default function TokenMonitoring({
});
setStats(result);
} catch (e) {
setError(e instanceof Error ? e.message : String(e));
setError(getErrorMessage(e));
} finally {
setLoading(false);
}
Expand Down
26 changes: 26 additions & 0 deletions web/tests/unit/token-monitoring-error.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import test from 'node:test';
import { fileURLToPath } from 'node:url';

const source = fs.readFileSync(
path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'../../src/app/home/monitoring/components/TokenMonitoring.tsx',
),
'utf8',
);

test('TokenMonitoring prefers the API error message for plain error objects', () => {
const helperStart = source.indexOf('function getErrorMessage');
const helperEnd = source.indexOf('\n}\n', helperStart) + 2;
const helper = source.slice(helperStart, helperEnd);

assert.ok(helperStart >= 0, 'TokenMonitoring error helper is missing');
assert.match(helper, /error instanceof Error/);
assert.match(helper, /typeof error === 'object'/);
assert.match(helper, /'msg' in error/);
assert.match(helper, /error as \{ msg\?: unknown \}/);
assert.match(source, /setError\(getErrorMessage\(e\)\)/);
});
Loading