errors.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * Custom error class for chat model authentication errors
  3. */
  4. export class ChatModelAuthError extends Error {
  5. /**
  6. * Creates a new ChatModelAuthError
  7. *
  8. * @param message - The error message
  9. * @param cause - The original error that caused this error
  10. */
  11. constructor(
  12. message: string,
  13. public readonly cause?: unknown,
  14. ) {
  15. super(message);
  16. this.name = 'ChatModelAuthError';
  17. // Maintains proper stack trace for where our error was thrown
  18. if (Error.captureStackTrace) {
  19. Error.captureStackTrace(this, ChatModelAuthError);
  20. }
  21. }
  22. /**
  23. * Returns a string representation of the error
  24. */
  25. toString(): string {
  26. return `${this.name}: ${this.message}${this.cause ? ` (Caused by: ${this.cause})` : ''}`;
  27. }
  28. }
  29. export class ChatModelForbiddenError extends Error {
  30. constructor(
  31. message: string,
  32. public readonly cause?: unknown,
  33. ) {
  34. super(message);
  35. this.name = 'ChatModelForbiddenError';
  36. if (Error.captureStackTrace) {
  37. Error.captureStackTrace(this, ChatModelForbiddenError);
  38. }
  39. }
  40. /**
  41. * Returns a string representation of the error
  42. */
  43. toString(): string {
  44. return `${this.name}: ${this.message}${this.cause ? ` (Caused by: ${this.cause})` : ''}`;
  45. }
  46. }
  47. export const LLM_FORBIDDEN_ERROR_MESSAGE =
  48. '访问被拒绝(403禁止)。请检查: n n1。您的API密钥具有所需权限\n\n2。设置OLLAMA_ORIGINS=chrome-extension://* \nsee https://github.com/ollama/ollama/blob/main/docs/faq.md';
  49. /**
  50. * Checks if an error is related to API authentication
  51. *
  52. * @param error - The error to check
  53. * @returns boolean indicating if it's an authentication error
  54. */
  55. export function isAuthenticationError(error: unknown): boolean {
  56. if (!(error instanceof Error)) return false;
  57. // Get the error message
  58. const errorMessage = error.message || '';
  59. // Get error name - sometimes error.name just returns "Error" for custom errors
  60. let errorName = error.name || '';
  61. // Try to extract the constructor name, which often contains the actual error type
  62. // This works better than error.name for many custom errors
  63. const constructorName = error.constructor?.name;
  64. if (constructorName && constructorName !== 'Error') {
  65. errorName = constructorName;
  66. }
  67. // Check if the error name indicates an authentication error
  68. if (errorName === 'AuthenticationError') {
  69. return true;
  70. }
  71. // Fallback: check the message for authentication-related indicators
  72. return (
  73. errorMessage.toLowerCase().includes('authentication') ||
  74. errorMessage.includes(' 401') ||
  75. errorMessage.toLowerCase().includes('api key')
  76. );
  77. }
  78. /**
  79. * Checks if an error is related 403 Forbidden
  80. *
  81. * @param error - The error to check
  82. * @returns boolean indicating if it's an 403 Forbidden error
  83. */
  84. export function isForbiddenError(error: unknown): boolean {
  85. if (!(error instanceof Error)) return false;
  86. return error.message.includes(' 403') && error.message.includes('Forbidden');
  87. }