Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

3.5 KB
Raw Blame
import { TRPCError } from '@trpc/server';

import { InstanceDirectoryOverlapError } from '@/database/models/environmentInstance';

/** The indexes that make a name identify one environment for one member. */
const ENVIRONMENT_NAME_CONSTRAINTS = new Set([
  'environments_user_name_unique',
  'environments_workspace_user_name_unique',
]);

/** The index that keeps two instances out of the same directory. */
const INSTANCE_DIRECTORY_CONSTRAINT = 'environment_instances_provider_path_unique';

/** Postgres surfaces the driver error somewhere down the `cause` chain. */
export const getPostgresErrorField = (error: unknown, field: string): string | undefined => {
  let current: unknown = error;

  while (current && typeof current === 'object') {
    const value = (current as Record<string, unknown>)[field];
    if (typeof value === 'string') return value;

    current = (current as { cause?: unknown }).cause;
  }
};

/**
 * The name is the one thing the person chose, and a collision is fixed by
 * typing a different one β€” so it has to arrive as CONFLICT next to the field,
 * not as the generic failure a raw driver error would produce. A code rather
 * than prose because this one is shown inline and has to be translated.
 */
export const rethrowDuplicateEnvironmentName = (error: unknown): never => {
  if (
    getPostgresErrorField(error, 'code') === '23505' &&
    ENVIRONMENT_NAME_CONSTRAINTS.has(getPostgresErrorField(error, 'constraint') ?? '')
  ) {
    throw new TRPCError({ cause: error, code: 'CONFLICT', message: 'DUPLICATE_ENVIRONMENT_NAME' });
  }

  throw error;
};

/**
 * Two instances in one directory would restore two package sets over each
 * other's files, so the database refuses it and the person picks another
 * directory. Same reasoning as a duplicate name, different fix.
 */
export const isDuplicateInstanceDirectory = (error: unknown): boolean =>
  getPostgresErrorField(error, 'code') === '23505' &&
  getPostgresErrorField(error, 'constraint') === INSTANCE_DIRECTORY_CONSTRAINT;

export const rethrowDuplicateInstanceDirectory = (error: unknown): never => {
  // The same fix for the person β€” pick another folder β€” but a different
  // sentence, since the folder itself is not taken.
  if (error instanceof InstanceDirectoryOverlapError) {
    throw new TRPCError({
      cause: error,
      code: 'CONFLICT',
      message: 'OVERLAPPING_INSTANCE_DIRECTORY',
    });
  }

  if (isDuplicateInstanceDirectory(error)) {
    throw new TRPCError({
      cause: error,
      code: 'CONFLICT',
      message: 'DUPLICATE_INSTANCE_DIRECTORY',
    });
  }

  throw error;
};

/** The restrict reference a project holds on an environment it uses. */
const PROJECT_ENVIRONMENT_CONSTRAINT = 'project_environments_environment_id_environments_id_fk';

/**
 * An environment with instances still on it, or a project still using it. The reference is `restrict`
 * on purpose β€” deleting the specification out from under them would leave
 * directories and captured state nothing describes.
 */
export const rethrowEnvironmentInUse = (error: unknown): never => {
  if (getPostgresErrorField(error, 'code') === '23503') {
    // Said apart, because what to do differs: a project link is removed from
    // the project, while copies go with the environment on the drive route.
    const message =
      getPostgresErrorField(error, 'constraint') === PROJECT_ENVIRONMENT_CONSTRAINT
        ? 'ENVIRONMENT_IN_PROJECT'
        : 'ENVIRONMENT_HAS_INSTANCES';
    throw new TRPCError({ cause: error, code: 'CONFLICT', message });
  }

  throw error;
};