All files / api/src/ai-providers/ollama utils.ts

100% Statements 27/27
87.5% Branches 7/8
100% Functions 1/1
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 391x   1x         1x 5x 5x 5x 5x         5x 5x 5x   5x 2x 5x 2x 2x 2x 2x   5x 5x 5x 5x 5x 5x 5x 5x 5x 5x  
import { generateErrorResponse } from '@api/utils/ai-provider';
import type { ErrorResponseBody } from '@shared/types/api/response/body';
import { AIProvider } from '@shared/types/constants';
 
/**
 * Transform Ollama error response to standardized error format
 */
export const ollamaErrorResponseTransform = (
  aiProviderResponseBody: Record<string, unknown>,
  aiProviderResponseStatus?: number,
): ErrorResponseBody => {
  const errorObj = aiProviderResponseBody.error as
    | { message?: string; type?: string; code?: string }
    | string
    | undefined;
 
  let message = 'Unknown error';
  let type: string | undefined;
  let code: string | undefined;
 
  if (typeof errorObj === 'string') {
    message = errorObj;
  } else if (errorObj && typeof errorObj === 'object') {
    message = errorObj.message || 'Unknown error';
    type = errorObj.type;
    code = errorObj.code;
  }
 
  return generateErrorResponse(
    {
      message,
      type,
      param: undefined,
      code: code || aiProviderResponseStatus?.toString(),
    },
    AIProvider.OLLAMA,
  );
};