export interface SayahApiResponse { ReturnValue?: boolean; HasError?: boolean; IsSucceed?: boolean; isSucceed?: boolean; Errors?: Record | Array<{ Code?: string; Message?: string }> | null; errors?: Record | Array<{ Code?: string; Message?: string }> | null; Result?: { ErrorMessage?: string | null; [key: string]: unknown; }; } export function formatSayahErrors( errors?: Record | Array<{ Code?: string; Message?: string }> | null, ): string { if (!errors) { return 'Request failed'; } if (Array.isArray(errors)) { if (errors.length === 0) { return 'Request failed'; } return errors.map((error) => `${error.Code}: ${error.Message}`).join(', '); } const entries = Object.entries(errors); if (entries.length === 0) { return 'Request failed'; } return entries.map(([code, field]) => `${field} (code: ${code})`).join(', '); } export function getSayahProviderError( body: SayahApiResponse, ): { message: string; code: string } | null { if ('ReturnValue' in body || 'HasError' in body) { if (body.HasError) { return { message: formatSayahErrors(body.Errors), code: 'SAYAH_ERROR', }; } if (body.ReturnValue === false) { return { message: formatSayahErrors(body.Errors), code: 'SHEBA_MISMATCH', }; } return null; } const isSucceed = body.IsSucceed ?? body.isSucceed; if (isSucceed === false) { const errors = body.Errors ?? body.errors; return { message: (Array.isArray(errors) || (errors && Object.keys(errors).length > 0) ? formatSayahErrors(errors) : body.Result?.ErrorMessage) ?? 'Request failed', code: 'API_ERROR', }; } return null; }