diff --git a/src/errors.ts b/src/errors.ts deleted file mode 100644 index 690f7b8..0000000 --- a/src/errors.ts +++ /dev/null @@ -1,18 +0,0 @@ -export type TinyPgErrorTransformer = (error: TinyPgError) => any - -export class TinyPgError extends Error { - name: string - message: string - queryContext: any - - constructor(message: string, stack?: string, query_context?: any) { - super(message) - - Object.setPrototypeOf(this, new.target.prototype) - - this.stack = stack - this.name = this.constructor.name - this.message = message - this.queryContext = query_context - } -} diff --git a/src/index.ts b/src/index.ts index 222b07b..9c95139 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,9 +4,18 @@ import { TinyPg } from './tiny' export { TinyPg, FormattableDbCall } from './tiny' -export { Result, TinyPgOptions, QueryBeginContext, QueryCompleteContext, SqlParseResult, SqlFile, TinyPgParams, TinyCallContext } from './types' - -export { TinyPgError, TinyPgErrorTransformer } from './errors' +export { + Result, + TinyPgOptions, + QueryBeginContext, + QueryCompleteContext, + SqlParseResult, + SqlFile, + TinyPgParams, + TinyCallContext, + TinyPgError, + TinyPgErrorTransformer, +} from './types' export { parseFiles } from './parser' diff --git a/src/tiny.ts b/src/tiny.ts index 9ccc31a..b56f9bb 100644 --- a/src/tiny.ts +++ b/src/tiny.ts @@ -5,7 +5,6 @@ import * as P from './parser' import * as Util from './util' import * as Uuid from 'uuid' import { EventEmitter } from 'events' -import * as E from './errors' import { parseSql } from 'tinypg-parser' import { createHash } from 'crypto' import { format } from '@scaleleap/pg-format' @@ -24,7 +23,7 @@ export class TinyPg { public sql_db_calls: { [key: string]: DbCall } private hooks: T.TinyHooks[] - private error_transformer: E.TinyPgErrorTransformer + private error_transformer: T.TinyPgErrorTransformer private sql_files: T.SqlFile[] private options: T.TinyPgOptions private transaction_id?: string @@ -468,13 +467,10 @@ export class TinyPg { } try { - const data = await Promise.race([ - connection_failed_promise.then(() => null), - query_promise() - ]) + const data = await Promise.race([connection_failed_promise.then(() => null), query_promise()]) if (_.isNil(data)) { - throw new E.TinyPgError("connection aborted") + throw new T.TinyPgError('connection aborted') } emitQueryComplete(createCompleteContext(null, data)) @@ -486,7 +482,7 @@ export class TinyPg { emitQueryComplete(complete_context) - const tiny_error = new E.TinyPgError(`${e.message}`, tiny_stack, complete_context) + const tiny_error = new T.TinyPgError(`${e.message}`, tiny_stack, complete_context) throw this.error_transformer(tiny_error) } finally { diff --git a/src/types.ts b/src/types.ts index a58b0b8..257d5f0 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,4 @@ import { EventEmitter } from 'events' -import { TinyPgErrorTransformer } from './errors' import { TlsOptions } from 'tls' export type HookCollection = { [P in keyof Required]: TinyHooks[P][] } @@ -142,3 +141,22 @@ export interface TinyPgEvents extends EventEmitter { emit(event: 'query' | 'submit' | 'result', ...args: any[]): boolean } + +export type TinyPgErrorTransformer = (error: TinyPgError) => any + +export class TinyPgError extends Error { + name: string + message: string + queryContext: QuerySubmitContext | QueryBeginContext | QueryCompleteContext | undefined + + constructor(message: string, stack?: string, query_context?: QuerySubmitContext | QueryBeginContext | QueryCompleteContext) { + super(message) + + Object.setPrototypeOf(this, new.target.prototype) + + this.stack = stack + this.name = this.constructor.name + this.message = message + this.queryContext = query_context + } +}