{"version":3,"file":"internal.mjs","names":[],"sources":["../src/query/errors.ts"],"sourcesContent":["import { RecordUnknown } from '../utils';\nimport { PickQueryShape } from './pick-query-types';\nimport { IsQuery, Query } from './query';\nimport { queryColumnNameToKey } from './query-columns/query-columns';\n\nexport abstract class OrchidOrmError extends Error {}\n\n/**\n * When we search for a single record, and it is not found, it can either throw an error, or return `undefined`.\n *\n * Unlike other database libraries, `Orchid ORM` decided to throw errors by default when using methods `take`, `find`, `findBy`, `get` and the record is not found.\n * It is a [good practice](https://github.com/goldbergyoni/nodebestpractices/blob/master/sections/errorhandling/centralizedhandling.md) to catch common errors in a centralized place (see [global error handling](https://orchid-orm.netlify.app/guide/error-handling.html#global-error-handling)), and this allows for a more concise code.\n *\n * If it's more suitable to get the `undefined` value instead of throwing, use `takeOptional`, `findOptional`, `findByOptional`, `getOptional` instead.\n */\nexport class NotFoundError extends OrchidOrmError {\n  // `#query` is private to prevent it from serializing to not cause problems to test runner reports\n  // it is exposed with `getQuery` method which prevents this problem from both Vitest and Jest.\n  // Exposing it with `get query()` still leaves the issue in Vitest.\n  readonly #query: Query;\n\n  constructor(query: IsQuery, message = 'Record is not found') {\n    super(message);\n    this.#query = query as Query;\n  }\n\n  getQuery() {\n    return this.#query;\n  }\n}\n\nexport class OrchidOrmInternalError extends Error {\n  // `#query` is private to prevent it from serializing to not cause problems to test runner reports\n  // it is exposed with `getQuery` method which prevents this problem from both Vitest and Jest.\n  // Exposing it with `get query()` still leaves the issue in Vitest.\n  readonly #query: Query;\n\n  constructor(\n    query: IsQuery,\n    message?: string,\n    public data?: RecordUnknown,\n  ) {\n    super(message);\n    this.#query = query as Query;\n  }\n\n  getQuery() {\n    return this.#query;\n  }\n}\n\nexport type QueryErrorName =\n  | 'parseComplete'\n  | 'bindComplete'\n  | 'closeComplete'\n  | 'noData'\n  | 'portalSuspended'\n  | 'replicationStart'\n  | 'emptyQuery'\n  | 'copyDone'\n  | 'copyData'\n  | 'rowDescription'\n  | 'parameterDescription'\n  | 'parameterStatus'\n  | 'backendKeyData'\n  | 'notification'\n  | 'readyForQuery'\n  | 'commandComplete'\n  | 'dataRow'\n  | 'copyInResponse'\n  | 'copyOutResponse'\n  | 'authenticationOk'\n  | 'authenticationMD5Password'\n  | 'authenticationCleartextPassword'\n  | 'authenticationSASL'\n  | 'authenticationSASLContinue'\n  | 'authenticationSASLFinal'\n  | 'error'\n  | 'notice';\n\nexport abstract class QueryError<\n  T extends PickQueryShape = PickQueryShape,\n> extends OrchidOrmInternalError {\n  declare message: string;\n  declare length?: number;\n  declare name: QueryErrorName;\n  declare stack: string | undefined;\n  code: string | undefined;\n  detail: string | undefined;\n  severity: string | undefined;\n  hint: string | undefined;\n  position: string | undefined;\n  internalPosition: string | undefined;\n  internalQuery: string | undefined;\n  where: string | undefined;\n  schema: string | undefined;\n  table: string | undefined;\n  column: string | undefined;\n  dataType: string | undefined;\n  constraint: string | undefined;\n  file: string | undefined;\n  line: string | undefined;\n  routine: string | undefined;\n\n  get isUnique() {\n    return this.code === '23505';\n  }\n\n  #columnsCache?: { [K in keyof T['shape']]?: true };\n  get columns() {\n    if (this.#columnsCache) return this.#columnsCache;\n\n    const columns: { [K in keyof T['shape']]?: true } = {};\n\n    if (this.detail) {\n      const list = this.detail.match(/\\((.*)\\)=/)?.[1];\n      if (list) {\n        list.split(', ').forEach((item) => {\n          const column = (\n            item.startsWith('\"') ? item.slice(1, -1) : item\n          ) as keyof T['shape'];\n\n          const key =\n            queryColumnNameToKey(this.getQuery(), column as string) ?? column;\n          columns[key as keyof T['shape']] = true;\n        });\n      }\n    }\n\n    return (this.#columnsCache = columns);\n  }\n}\n\nexport class MoreThanOneRowError extends OrchidOrmInternalError {\n  constructor(query: IsQuery, message?: string) {\n    super(query, message);\n  }\n}\n\nexport class UnhandledTypeError extends OrchidOrmInternalError {\n  constructor(query: IsQuery, value: never) {\n    super(query, `Unhandled type: ${JSON.stringify(value)} received`);\n  }\n}\n\n/**\n * Error thrown when attempting to nest SQL session scopes.\n * Nested withOptions/$withOptions calls that supply role or setConfig while an outer\n * scope already has SQL session state defined will throw this error.\n */\nexport class NestedSqlSessionError extends OrchidOrmInternalError {\n  constructor(query: IsQuery) {\n    super(\n      query,\n      'Cannot nest SQL session scopes. Outer scope already has role or setConfig defined.',\n    );\n  }\n}\n"],"mappings":";AAKA,IAAsB,iBAAtB,cAA6C,MAAM"}