export function safeInputError(error: unknown, fallback: string): Error {
  const message = error instanceof Error ? error.message : '';
  const normalized = /^[a-z0-9_.:-]{3,160}$/i.test(message) ? message : fallback;
  return new Error(normalized);
}

export function safeHttpError(error: unknown, fallback: string): Error {
  const source = error && typeof error === 'object' ? error as Record<string, unknown> : {};
  const response = source.response && typeof source.response === 'object'
    ? source.response as Record<string, unknown>
    : {};
  const candidate = Number(source.statusCode ?? source.status ?? response.statusCode ?? response.status);
  const suffix = Number.isInteger(candidate) && candidate >= 100 && candidate <= 599
    ? `_http_${candidate}`
    : '';
  return new Error(`${fallback}${suffix}`);
}
