diff --git a/checks/_helpers/observability.mjs b/checks/_helpers/observability.mjs index 7c293879..d1206517 100644 --- a/checks/_helpers/observability.mjs +++ b/checks/_helpers/observability.mjs @@ -69,13 +69,20 @@ export function workersWindow(sinceIso, untilIso) { const FAILURE_DETAIL_LIMIT = 300 function failure(service, status, detail) { - const text = detail.trim() - if (!text) + // Cut by code point, so the limit never splits a surrogate pair. + const characters = Array.from(detail.trim()) + if (!characters.length) return { _tag: 'Err', reason: `${service} returned HTTP ${status}.` } - const bounded = text.length > FAILURE_DETAIL_LIMIT ? `${text.slice(0, FAILURE_DETAIL_LIMIT)}…` : text + const bounded = characters.length > FAILURE_DETAIL_LIMIT ? `${characters.slice(0, FAILURE_DETAIL_LIMIT).join('')}…` : characters.join('') return { _tag: 'Err', reason: `${service} returned HTTP ${status}: ${bounded}` } } +function errorMessages(errors) { + return Array.isArray(errors) + ? errors.map(error => error?.message).filter(message => typeof message === 'string') + : [] +} + function parseJson(text) { try { return { _tag: 'Ok', value: JSON.parse(text) } @@ -98,8 +105,7 @@ export function parseWorkersResponse(status, text) { const errors = json._tag === 'Ok' ? json.value?.errors : undefined const hasErrors = errors != null && (!Array.isArray(errors) || errors.length > 0) if (!isSuccess(status) || json._tag === 'Err' || hasErrors) { - const messages = Array.isArray(errors) ? errors.map(error => error?.message).filter(message => typeof message === 'string') : [] - return failure('Cloudflare GraphQL', status, messages.join('; ')) + return failure('Cloudflare GraphQL', status, errorMessages(errors).join('; ')) } const rows = json.value?.data?.viewer?.accounts?.[0]?.workersInvocationsAdaptive return Array.isArray(rows) ? { _tag: 'Ok', rows } : { _tag: 'Err', reason: 'Cloudflare Worker evidence is unavailable.' } @@ -107,10 +113,15 @@ export function parseWorkersResponse(status, text) { /** Parse an Analytics Engine SQL response into its rows, or the reason it failed. */ export function parseAnalyticsEngineResponse(status, text) { - // The SQL API answers a rejected query with a plain-text message. - if (!isSuccess(status)) - return failure('Analytics Engine', status, text) const json = parseJson(text) + if (!isSuccess(status)) { + // A rejected SQL query answers in plain text, the API gateway in a JSON + // envelope, and a proxy in HTML, whose markup says nothing useful. + const detail = json._tag === 'Ok' + ? errorMessages(json.value?.errors).join('; ') + : text.trimStart().startsWith('<') ? '' : text + return failure('Analytics Engine', status, detail) + } const rows = json._tag === 'Ok' ? json.value?.data : undefined return Array.isArray(rows) ? { _tag: 'Ok', rows } : { _tag: 'Err', reason: 'Analytics Engine copy evidence is unavailable.' } } diff --git a/test/unit/daily-checkin-observability.test.ts b/test/unit/daily-checkin-observability.test.ts index f105589f..134e41dd 100644 --- a/test/unit/daily-checkin-observability.test.ts +++ b/test/unit/daily-checkin-observability.test.ts @@ -156,11 +156,29 @@ describe('daily check-in observability', () => { .toEqual({ _tag: 'Ok', rows }) }) + // GraphQL sends `errors: null` beside data when a query succeeds. + it('returns the Worker rows of a response whose errors are null', () => { + const rows = [{ dimensions: { scriptName: 'skilld-dev', status: 'success' }, sum: { requests: 3 } }] + expect(parseWorkersResponse(200, JSON.stringify({ data: { viewer: { accounts: [{ workersInvocationsAdaptive: rows }] } }, errors: null }))) + .toEqual({ _tag: 'Ok', rows }) + }) + it('names the Analytics Engine message of a failed query', () => { expect(parseAnalyticsEngineResponse(422, 'unknown column: blob9')) .toEqual({ _tag: 'Err', reason: 'Analytics Engine returned HTTP 422: unknown column: blob9' }) }) + it('names the API error message of a rejected Analytics Engine request', () => { + const body = JSON.stringify({ success: false, errors: [{ code: 10000, message: 'Authentication error' }], messages: [], result: null }) + expect(parseAnalyticsEngineResponse(401, body)) + .toEqual({ _tag: 'Err', reason: 'Analytics Engine returned HTTP 401: Authentication error' }) + }) + + it('keeps only the status of an Analytics Engine error page', () => { + expect(parseAnalyticsEngineResponse(502, 'Bad gateway')) + .toEqual({ _tag: 'Err', reason: 'Analytics Engine returned HTTP 502.' }) + }) + it('returns the Analytics Engine rows of a successful query', () => { expect(parseAnalyticsEngineResponse(200, JSON.stringify({ data: [{ copies: 2 }] }))) .toEqual({ _tag: 'Ok', rows: [{ copies: 2 }] })