utils.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. export function getCurrentTimestampStr(): string {
  2. /**
  3. * Get the current timestamp as a string in the format yyyy-MM-dd HH:mm:ss
  4. * using local timezone.
  5. *
  6. * @returns Formatted datetime string in local time
  7. */
  8. return new Date()
  9. .toLocaleString('en-US', {
  10. year: 'numeric',
  11. month: '2-digit',
  12. day: '2-digit',
  13. hour: '2-digit',
  14. minute: '2-digit',
  15. second: '2-digit',
  16. hour12: false,
  17. })
  18. .replace(',', '');
  19. }
  20. /**
  21. * Checks if an error is related to API authentication
  22. *
  23. * @param error - The error to check
  24. * @returns boolean indicating if it's an authentication error
  25. */
  26. export function isAuthenticationError(error: unknown): boolean {
  27. if (!(error instanceof Error)) return false;
  28. // Get the error message
  29. const errorMessage = error.message || '';
  30. // Get error name - sometimes error.name just returns "Error" for custom errors
  31. let errorName = error.name || '';
  32. // Try to extract the constructor name, which often contains the actual error type
  33. // This works better than error.name for many custom errors
  34. const constructorName = error.constructor?.name;
  35. if (constructorName && constructorName !== 'Error') {
  36. errorName = constructorName;
  37. }
  38. // Check if the error name indicates an authentication error
  39. if (errorName === 'AuthenticationError') {
  40. return true;
  41. }
  42. // Fallback: check the message for authentication-related indicators
  43. return (
  44. errorMessage.toLowerCase().includes('authentication') ||
  45. errorMessage.includes('401') ||
  46. errorMessage.toLowerCase().includes('api key')
  47. );
  48. }