Skip to content

Commit

Permalink
merge errors into types to prevent cyclical import. Update error quer…
Browse files Browse the repository at this point in the history
…y context type
  • Loading branch information
stephenreddek committed Jun 12, 2023
1 parent 8168593 commit e2db031
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 30 deletions.
18 changes: 0 additions & 18 deletions src/errors.ts

This file was deleted.

15 changes: 12 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
12 changes: 4 additions & 8 deletions src/tiny.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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 {
Expand Down
20 changes: 19 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { EventEmitter } from 'events'
import { TinyPgErrorTransformer } from './errors'
import { TlsOptions } from 'tls'

export type HookCollection = { [P in keyof Required<TinyHooks>]: TinyHooks[P][] }
Expand Down Expand Up @@ -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
}
}

0 comments on commit e2db031

Please sign in to comment.