LCOV - code coverage report
Current view: top level - src/platform/remote/comfyui - errors.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 14.3 % 28 4
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { isPlainObject } from 'es-toolkit'
       2              : 
       3              : import type { ErrorResponse } from '@comfyorg/ingest-types'
       4              : 
       5              : /** Code reported when an error payload carries no machine-readable code. */
       6         2356 : export const UNKNOWN_ERROR_CODE = 'UNKNOWN_ERROR'
       7              : 
       8              : /**
       9              :  * Upper bound on a raw non-JSON body surfaced as the user-facing message.
      10              :  * Short text/plain proxy errors (e.g. "upstream connect error") stay useful;
      11              :  * oversized bodies (e.g. a full HTML gateway page) degrade to the clean
      12              :  * status-derived fallback instead of dumping markup into a toast.
      13              :  */
      14         2356 : const MAX_RAW_MESSAGE_LENGTH = 500
      15              : 
      16              : /**
      17              :  * A body opening with a tag is a markup document, never a usable message —
      18              :  * however short the page happens to be. Anchored so prose that merely
      19              :  * mentions a bracketed token (e.g. "connection to <backend-01> refused")
      20              :  * still surfaces.
      21              :  */
      22         2356 : const MARKUP_DOCUMENT = /^<[a-z!/]/i
      23              : 
      24         2356 : const CODE_SHAPE = /^[A-Z][A-Z0-9_]*$/
      25              : 
      26              : /**
      27              :  * Flat `{ error, message }` bodies (the /customers/* shape) carry the code in
      28              :  * `error`; a prose value there (e.g. "Forbidden") is not a code.
      29              :  */
      30            0 : function codeFromRecord(record: Record<PropertyKey, unknown>): string {
      31            0 :   const raw =
      32              :     record.code ??
      33              :     (typeof record.error === 'string' && CODE_SHAPE.test(record.error)
      34              :       ? record.error
      35              :       : undefined)
      36            0 :   return typeof raw === 'string' && raw !== '' ? raw : UNKNOWN_ERROR_CODE
      37              : }
      38              : 
      39            0 : function usableText(body: string, fallbackMessage: string): string {
      40            0 :   const trimmed = body.trim()
      41              :   const usable =
      42              :     trimmed !== '' &&
      43              :     trimmed.length <= MAX_RAW_MESSAGE_LENGTH &&
      44              :     !MARKUP_DOCUMENT.test(trimmed)
      45            0 :   return usable ? trimmed : fallbackMessage
      46              : }
      47              : 
      48              : /**
      49              :  * Coerce an already-parsed error body into the canonical
      50              :  * `ErrorResponse { code, message, details? }` shape.
      51              :  *
      52              :  * The API emits this shape for all error responses; this helper is the
      53              :  * single place that tolerates legacy/partial flat `ErrorResponse` payloads
      54              :  * (missing `code`, missing `message`, non-object bodies) so call sites never
      55              :  * shape-sniff. Nested/domain error envelopes (e.g. `PromptExecutionError`)
      56              :  * are out of scope.
      57              :  *
      58              :  * @param body - The parsed response body (any JSON value, or `undefined`)
      59              :  * @param fallbackMessage - Used when the body carries no usable message
      60              :  */
      61            0 : export function errorResponseFromBody(
      62              :   body: unknown,
      63              :   fallbackMessage: string
      64              : ): ErrorResponse {
      65            0 :   if (typeof body === 'string') {
      66            0 :     return {
      67              :       code: UNKNOWN_ERROR_CODE,
      68              :       message: usableText(body, fallbackMessage)
      69              :     }
      70              :   }
      71            0 :   const record: Record<PropertyKey, unknown> = isPlainObject(body) ? body : {}
      72            0 :   const code = codeFromRecord(record)
      73            0 :   const message =
      74              :     typeof record.message === 'string' && record.message !== ''
      75              :       ? record.message
      76              :       : fallbackMessage
      77            0 :   const details = isPlainObject(record.details) ? record.details : undefined
      78            0 :   return details !== undefined ? { code, message, details } : { code, message }
      79              : }
      80              : 
      81              : /**
      82              :  * Parse JSON when possible, otherwise surface the raw text. A blank or
      83              :  * whitespace-only body yields `undefined` so callers fall through to a
      84              :  * status-derived fallback.
      85              :  */
      86            0 : function parseJsonOrText(text: string): unknown {
      87            0 :   if (text.trim() === '') return undefined
      88            0 :   try {
      89            0 :     return JSON.parse(text)
      90              :   } catch {
      91            0 :     return text
      92              :   }
      93              : }
      94              : 
      95              : /**
      96              :  * Parse a failed HTTP `Response` into the canonical
      97              :  * `ErrorResponse { code, message, details? }` shape.
      98              :  *
      99              :  * Never throws: the body is read as text and JSON-parsed when possible, so
     100              :  * plain-text error bodies (e.g. from a proxy) survive as the message. Empty
     101              :  * or unreadable bodies degrade to a status-derived message and the
     102              :  * `UNKNOWN_ERROR` code.
     103              :  */
     104            0 : export async function parseErrorResponse(
     105              :   response: Response
     106              : ): Promise<ErrorResponse> {
     107            0 :   const fallbackMessage = response.statusText || `HTTP ${response.status}`
     108              :   const text = await response.text().catch((err: unknown) => {
     109            0 :     console.warn('parseErrorResponse: failed to read response body', err)
     110            0 :     return ''
     111              :   })
     112            0 :   return errorResponseFromBody(parseJsonOrText(text), fallbackMessage)
     113              : }
        

Generated by: LCOV version 2.0-1