You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
5.6 KiB

  1. import { Uri } from 'vscode';
  2. import { isSubscriptionPaidPlan, RequiredSubscriptionPlans, Subscription } from './subscription';
  3. export class AccessDeniedError extends Error {
  4. public readonly subscription: Subscription;
  5. public readonly required: RequiredSubscriptionPlans | undefined;
  6. constructor(subscription: Subscription, required: RequiredSubscriptionPlans | undefined) {
  7. let message;
  8. if (subscription.account?.verified === false) {
  9. message = 'Email verification required';
  10. } else if (required != null && isSubscriptionPaidPlan(required)) {
  11. message = 'Paid subscription required';
  12. } else {
  13. message = 'Subscription required';
  14. }
  15. super(message);
  16. this.subscription = subscription;
  17. this.required = required;
  18. Error.captureStackTrace?.(this, AccessDeniedError);
  19. }
  20. }
  21. export class AccountValidationError extends Error {
  22. readonly original?: Error;
  23. readonly statusCode?: number;
  24. readonly statusText?: string;
  25. constructor(message: string, original?: Error, statusCode?: number, statusText?: string) {
  26. message += `; status=${statusCode}: ${statusText}`;
  27. super(message);
  28. this.original = original;
  29. this.statusCode = statusCode;
  30. this.statusText = statusText;
  31. Error.captureStackTrace?.(this, AccountValidationError);
  32. }
  33. }
  34. export const enum AuthenticationErrorReason {
  35. UserDidNotConsent = 1,
  36. Unauthorized = 2,
  37. Forbidden = 3,
  38. }
  39. export class AuthenticationError extends Error {
  40. readonly id: string;
  41. readonly original?: Error;
  42. readonly reason: AuthenticationErrorReason | undefined;
  43. constructor(id: string, reason?: AuthenticationErrorReason, original?: Error);
  44. constructor(id: string, message?: string, original?: Error);
  45. constructor(id: string, messageOrReason: string | AuthenticationErrorReason | undefined, original?: Error) {
  46. let message;
  47. let reason: AuthenticationErrorReason | undefined;
  48. if (messageOrReason == null) {
  49. message = `Unable to get required authentication session for '${id}'`;
  50. } else if (typeof messageOrReason === 'string') {
  51. message = messageOrReason;
  52. reason = undefined;
  53. } else {
  54. reason = messageOrReason;
  55. switch (reason) {
  56. case AuthenticationErrorReason.UserDidNotConsent:
  57. message = `'${id}' authentication is required for this operation`;
  58. break;
  59. case AuthenticationErrorReason.Unauthorized:
  60. message = `Your '${id}' credentials are either invalid or expired`;
  61. break;
  62. case AuthenticationErrorReason.Forbidden:
  63. message = `Your '${id}' credentials do not have the required access`;
  64. break;
  65. }
  66. }
  67. super(message);
  68. this.id = id;
  69. this.original = original;
  70. this.reason = reason;
  71. Error.captureStackTrace?.(this, AuthenticationError);
  72. }
  73. }
  74. export class ExtensionNotFoundError extends Error {
  75. constructor(public readonly extensionId: string, public readonly extensionName: string) {
  76. super(
  77. `Unable to find the ${extensionName} extension (${extensionId}). Please ensure it is installed and enabled.`,
  78. );
  79. Error.captureStackTrace?.(this, ExtensionNotFoundError);
  80. }
  81. }
  82. export const enum OpenVirtualRepositoryErrorReason {
  83. RemoteHubApiNotFound = 1,
  84. NotAGitHubRepository = 2,
  85. GitHubAuthenticationNotFound = 3,
  86. GitHubAuthenticationDenied = 4,
  87. }
  88. export class OpenVirtualRepositoryError extends Error {
  89. readonly original?: Error;
  90. readonly reason: OpenVirtualRepositoryErrorReason | undefined;
  91. readonly repoPath: string;
  92. constructor(repoPath: string, reason?: OpenVirtualRepositoryErrorReason, original?: Error);
  93. constructor(repoPath: string, message?: string, original?: Error);
  94. constructor(
  95. repoPath: string,
  96. messageOrReason: string | OpenVirtualRepositoryErrorReason | undefined,
  97. original?: Error,
  98. ) {
  99. let message;
  100. let reason: OpenVirtualRepositoryErrorReason | undefined;
  101. if (messageOrReason == null) {
  102. message = `Unable to open the virtual repository: ${repoPath}`;
  103. } else if (typeof messageOrReason === 'string') {
  104. message = messageOrReason;
  105. reason = undefined;
  106. } else {
  107. reason = messageOrReason;
  108. message = `Unable to open the virtual repository: ${repoPath}; `;
  109. switch (reason) {
  110. case OpenVirtualRepositoryErrorReason.RemoteHubApiNotFound:
  111. message +=
  112. 'Unable to get required api from the GitHub Repositories extension. Please ensure that the GitHub Repositories extension is installed and enabled';
  113. break;
  114. case OpenVirtualRepositoryErrorReason.NotAGitHubRepository:
  115. message += 'Only GitHub repositories are supported currently';
  116. break;
  117. case OpenVirtualRepositoryErrorReason.GitHubAuthenticationNotFound:
  118. message += 'Unable to get required GitHub authentication';
  119. break;
  120. case OpenVirtualRepositoryErrorReason.GitHubAuthenticationDenied:
  121. message += 'GitHub authentication is required';
  122. break;
  123. }
  124. }
  125. super(message);
  126. this.original = original;
  127. this.reason = reason;
  128. this.repoPath = repoPath;
  129. Error.captureStackTrace?.(this, OpenVirtualRepositoryError);
  130. }
  131. }
  132. export class ProviderNotFoundError extends Error {
  133. constructor(pathOrUri: string | Uri | undefined) {
  134. super(
  135. `No provider registered for '${
  136. pathOrUri == null
  137. ? String(pathOrUri)
  138. : typeof pathOrUri === 'string'
  139. ? pathOrUri
  140. : pathOrUri.toString(true)
  141. }'`,
  142. );
  143. Error.captureStackTrace?.(this, ProviderNotFoundError);
  144. }
  145. }
  146. export class ProviderRequestClientError extends Error {
  147. constructor(public readonly original: Error) {
  148. super(original.message);
  149. Error.captureStackTrace?.(this, ProviderRequestClientError);
  150. }
  151. }
  152. export class ProviderRequestNotFoundError extends Error {
  153. constructor(public readonly original: Error) {
  154. super(original.message);
  155. Error.captureStackTrace?.(this, ProviderRequestNotFoundError);
  156. }
  157. }