Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

7.4 KB
Raw Blame
/* eslint-disable max-classes-per-file */

export class DatabaseError extends Error {
  name: string;

  error: unknown;

  constructor({ name, error, message }: { message?: string; name?: string; error: unknown }) {
    super(message || "Failed to execute db ops");
    this.name = name || "DatabaseError";
    this.error = error;
  }
}

export class InternalServerError extends Error {
  name: string;

  error: unknown;

  constructor({ name, error, message }: { message?: string; name?: string; error?: unknown }) {
    super(message || "Something went wrong");
    this.name = name || "InternalServerError";
    this.error = error;
  }
}

export class GatewayTimeoutError extends Error {
  name: string;

  error: unknown;

  constructor({ name, error, message }: { message?: string; name?: string; error?: unknown }) {
    super(message || "Timeout error");
    this.name = name || "GatewayTimeoutError";
    this.error = error;
  }
}

// Thrown to stop work for a request whose client has already disconnected. No one receives the response.
export class ClientClosedRequestError extends Error {
  name: string;

  constructor({ message }: { message?: string } = {}) {
    super(message || "The client closed the connection before the request completed");
    this.name = "ClientClosedRequestError";
  }
}

export const throwIfClientDisconnected = (signal?: AbortSignal) => {
  if (signal?.aborted) {
    throw new ClientClosedRequestError();
  }
};

// For Promise.allSettled fan-outs, which swallow rejections: if any task stopped on a disconnect the combined
// result is partial and must not be used. Checking the results rather than the signal keeps a result that
// completed just before the client left.
export const throwIfAnySettledClientClosed = (results: PromiseSettledResult<unknown>[]) => {
  const closed = results.find(
    (result): result is PromiseRejectedResult =>
      result.status === "rejected" && result.reason instanceof ClientClosedRequestError
  );
  if (closed) throw closed.reason;
};

export class UnauthorizedError extends Error {
  name: string;

  error: unknown;

  detail?: Record<string, unknown>;

  constructor({
    name,
    error,
    message,
    detail
  }: { message?: string; name?: string; error?: unknown; detail?: Record<string, unknown> } = {}) {
    super(message ?? "You are not allowed to access this resource");
    this.name = name || "UnauthorizedError";
    this.error = error;
    this.detail = detail;
  }
}

export class ForbiddenRequestError extends Error {
  name: string;

  error: unknown;

  details?: unknown;

  constructor({
    name,
    error,
    message,
    details
  }: { message?: string; name?: string; error?: unknown; details?: unknown } = {}) {
    super(message ?? "You are not allowed to access this resource");
    this.name = name || "ForbiddenError";
    this.error = error;
    this.details = details;
  }
}

export class PermissionBoundaryError extends ForbiddenRequestError {
  constructor({
    message,
    name,
    error,
    details
  }: {
    message?: string;
    name?: string;
    error?: unknown;
    details?: unknown;
  }) {
    super({ message, name, error, details });
    this.name = "PermissionBoundaryError";
  }
}

export class BadRequestError extends Error {
  name: string;

  error: unknown;

  details?: unknown;

  constructor({
    name,
    error,
    message,
    details
  }: {
    message?: string;
    name?: string;
    error?: unknown;
    details?: unknown;
  }) {
    super(message ?? "The request is invalid");
    this.name = name || "BadRequest";
    this.error = error;
    this.details = details;
  }
}

/**
 * The tunnel to the gateway could never be established, so nothing reached the target and the
 * operation is safe to retry against a different pool member. Any failure raised once the tunnel is
 * up must NOT use this class: the target may have already applied a partial change.
 */
export class GatewayTransportError extends BadRequestError {
  gatewayId?: string;

  constructor({ message, gatewayId }: { message?: string; gatewayId?: string }) {
    // Deliberately keeps the BadRequest name so the serialised `error` field is unchanged for
    // callers. Retry decisions use instanceof, never the name.
    super({ message: message ?? "Failed to reach the gateway" });
    this.gatewayId = gatewayId;
  }
}

export class RateLimitError extends Error {
  constructor({ message }: { message?: string }) {
    super(message || "Rate limit exceeded");
    this.name = "RateLimitExceeded";
  }
}

// A usage quota for the day is used up. A 429 so a client retries later, kept apart from RateLimitError, which counts
// toward the API rate-limit metric.
export class DailyQuotaExceededError extends Error {
  constructor({ message }: { message?: string }) {
    super(message || "The daily quota has been used up. Try again after 00:00 UTC.");
    this.name = "DailyQuotaExceeded";
  }
}

export class NotFoundError extends Error {
  name: string;

  error: unknown;

  constructor({ name, error, message }: { message?: string; name?: string; error?: unknown }) {
    super(message ?? "The requested entity is not found");
    this.name = name || "NotFound";
    this.error = error;
  }
}

export class ConflictError extends Error {
  name: string;

  error: unknown;

  constructor({ name, error, message }: { message?: string; name?: string; error?: unknown }) {
    super(message ?? "The request conflicts with the current state of the resource");
    this.name = name || "Conflict";
    this.error = error;
  }
}

export class DisableRotationErrors extends Error {
  name: string;

  error: unknown;

  constructor({ name, error, message }: { message: string; name?: string; error?: unknown }) {
    super(message);
    this.name = name || "DisableRotationErrors";
    this.error = error;
  }
}

export class ScimRequestError extends Error {
  name: string;

  schemas: string[];

  detail: string;

  status: number;

  error: unknown;

  mutability?: string;

  scimType?: string;

  constructor({
    name,
    error,
    detail,
    status,
    mutability,
    scimType
  }: {
    message?: string;
    name?: string;
    mutability?: string;
    scimType?: string;
    error?: unknown;
    detail: string;
    status: number;
  }) {
    super(detail ?? "The request is invalid");
    this.name = name || "ScimRequestError";
    this.schemas = ["urn:ietf:params:scim:api:messages:2.0:Error"];
    this.error = error;
    this.detail = detail;
    this.status = status;
    this.mutability = mutability;
    this.scimType = scimType;
  }
}

export class OidcAuthError extends Error {
  name: string;

  error: unknown;

  constructor({ name, error, message }: { message?: string; name?: string; error?: unknown }) {
    super(message || "Something went wrong");
    this.name = name || "OidcAuthError";
    this.error = error;
  }
}

export class CryptographyError extends Error {
  name: string;

  error: unknown;

  constructor({ name, error, message }: { message?: string; name?: string; error?: unknown }) {
    super(message || "Cryptographic operation failed");
    this.name = name || "CryptographyError";
    this.error = error;
  }
}

export class PolicyViolationError extends Error {
  name: string;

  error: unknown;

  details?: unknown;

  constructor({
    name,
    error,
    message,
    details
  }: { message?: string; name?: string; error?: unknown; details?: unknown } = {}) {
    super(message || "A policy is in place for this resource");
    this.name = name || "PolicyViolationError";
    this.error = error;
    this.details = details;
  }
}