Skip to content

Commit

Permalink
error refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ivawzh committed Jul 11, 2020
1 parent 1adc9f4 commit 059b80d
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 21 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ EXAMPLES
$ pg-god db-create --databaseName=bank-db --password=123 --port=5433 --host=a.example.com --userName=beer
```

_See code: [src/commands/db-create.ts](https://github.com/ivawzh/pg-god/blob/v1.0.2/src/commands/db-create.ts)_

## `pg-god db-drop`

drop a database
Expand All @@ -122,7 +120,7 @@ USAGE
$ pg-god db-drop
OPTIONS
-e, --errorIfNonExist [default: false] whether throw error if DB doesn't exists
-e, --errorIfNonExist [default: false] whether throw error if DB doesn't exist
-h, --help show CLI help
-h, --host=host [default: localhost] DB host
-i, --initialDb=initialDb [default: postgres] Initial DB name
Expand All @@ -140,8 +138,6 @@ EXAMPLES
$ pg-god db-drop --databaseName=bank-db --password=123 --port=5433 --host=a.example.com --userName=beer
```

_See code: [src/commands/db-drop.ts](https://github.com/ivawzh/pg-god/blob/v1.0.2/src/commands/db-drop.ts)_

## `pg-god help [COMMAND]`

display help for pg-god
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "pg-god",
"description": "Tiny library that helps create and kill PostgreSQL database.",
"version": "1.0.3",
"version": "1.0.5",
"author": "ivan.wang @ivawzh",
"bin": {
"pg-god": "./bin/run"
Expand Down
2 changes: 1 addition & 1 deletion src/commands/db-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class DbCreate extends Command {
} = this.parse(DbCreate)

try {
cli.action.start(`🦾 Start to create database '${databaseName}'`)
cli.action.start(`😇 Start to create database '${databaseName}'`)

createDatabase(
{ databaseName, errorIfExist },
Expand Down
4 changes: 2 additions & 2 deletions src/commands/db-drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class DbDrop extends Command {
static flags = {
help: flags.help({char: 'h'}),
databaseName: flags.string({char: 'n', required: true, description: 'name of DB attempt to drop', env: 'DB_NAME'}),
errorIfNonExist: flags.boolean({char: 'e', default: false, description: "[default: false] whether throw error if DB doesn't exists", env: 'DB_ERROR_IF_NON_EXIST'}),
errorIfNonExist: flags.boolean({char: 'e', default: false, description: "[default: false] whether throw error if DB doesn't exist", env: 'DB_ERROR_IF_NON_EXIST'}),
userName: flags.string({char: 'u', default: 'postgres', description: 'DB user name', env: 'DB_USERNAME'}),
initialDb: flags.string({char: 'i', default: 'postgres', description: 'Initial DB name', env: 'DB_INITIAL'}),
port: flags.integer({char: 'p', default: 5432, description: 'DB port, default `5432`', env: 'DB_PORT'}),
Expand All @@ -37,7 +37,7 @@ export default class DbDrop extends Command {
} = this.parse(DbDrop)

try {
cli.action.start(`🦾 Start to drop database '${databaseName}'`)
cli.action.start(`😇 Start to drop database '${databaseName}'`)

dropDatabase(
{ databaseName, errorIfNonExist },
Expand Down
14 changes: 7 additions & 7 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ export const errorProtocol = {
},
}

export class PgDbGodError implements Error {
export class PgGodError implements Error {
constructor(
readonly name: string,
readonly message: string,
readonly code: string,
readonly stack?: string
) {}

public static fromPgError(pgError: Error & { code?: string }): PgDbGodError {
return new PgDbGodError(
public static fromPgError(pgError: Error & { code?: string }): PgGodError {
return new PgGodError(
// Until resolution of index issue: https://github.com/Microsoft/TypeScript/issues/14951#issuecomment-291617624
// @ts-ignore
errorProtocol[pgError.code]?.name || 'PDG_ERR::UnexpectedError',
Expand All @@ -43,19 +43,19 @@ export class PgDbGodError implements Error {
)
}

public static dbAlreadyExist(): PgDbGodError {
public static dbAlreadyExist(): PgGodError {
const code = '42P04'
return new PgDbGodError(
return new PgGodError(
errorProtocol[code].name,
errorProtocol[code].message,
code,
Error().stack,
)
}

public static dbDoesNotExist(): PgDbGodError {
public static dbDoesNotExist(): PgGodError {
const code = '3D000'
return new PgDbGodError(
return new PgGodError(
errorProtocol[code].name,
errorProtocol[code].message,
code,
Expand Down
10 changes: 5 additions & 5 deletions src/god-stuff.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Client } from 'pg'
import { PgDbGodError } from './error'
import { PgGodError } from './error'

export type DbCredential = {
user: string
Expand Down Expand Up @@ -43,12 +43,12 @@ export async function createDatabase(newDbConfig: NewDbConfig, dbCredential: Par
WHERE lower(datname) = lower('${newDbConfig.databaseName}');
`)

if (existingDb.rowCount > 0 && newDbConfig.errorIfExist) throw PgDbGodError.dbAlreadyExist()
if (existingDb.rowCount > 0 && newDbConfig.errorIfExist) throw PgGodError.dbAlreadyExist()
if (existingDb.rowCount > 0 && !newDbConfig.errorIfExist) return

await client.query(`CREATE DATABASE "${newDbConfig.databaseName}";`)
} catch (error) {
throw PgDbGodError.fromPgError(error)
throw PgGodError.fromPgError(error)
} finally {
await client.end()
}
Expand Down Expand Up @@ -78,12 +78,12 @@ export async function dropDatabase(dropDbConfig: DropDbConfig, dbCredential: DbC
WHERE lower(datname) = lower('${dropDbConfig.databaseName}');
`)

if (existingDb.rowCount === 0 && dropDbConfig.errorIfNonExist) throw PgDbGodError.dbDoesNotExist()
if (existingDb.rowCount === 0 && dropDbConfig.errorIfNonExist) throw PgGodError.dbDoesNotExist()
if (existingDb.rowCount === 0 && !dropDbConfig.errorIfNonExist) return

await client.query(`DROP DATABASE "${dropDbConfig.databaseName}";`)
} catch (error) {
throw PgDbGodError.fromPgError(error)
throw PgGodError.fromPgError(error)
} finally {
await client.end()
}
Expand Down

0 comments on commit 059b80d

Please sign in to comment.