Skip to content

Commit

Permalink
fix(errors): throw exception on error of connections
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Sep 1, 2022
1 parent 154eb95 commit 90bfbc4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
28 changes: 28 additions & 0 deletions src/Exceptions/CloseConnectionFailedException.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @athenna/database
*
* (c) João Lenon <lenon@athenna.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { Exception } from '@secjs/utils'

export class CloseConnectionFailedException extends Exception {
/**
* Creates a new instance of CloseConnectionFailedException.
*
* @return {CloseConnectionFailedException}
*/
constructor(driverName, error) {
const content = `Error occurred when trying to close the connection with database for ${driverName} driver.`

super(
content,
500,
'E_CLOSE_CONNECTION_FAILED_ERROR',
`${JSON.stringify(error, null, 2)}`,
)
}
}
28 changes: 28 additions & 0 deletions src/Exceptions/ConnectionFailedException.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @athenna/database
*
* (c) João Lenon <lenon@athenna.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { Exception } from '@secjs/utils'

export class ConnectionFailedException extends Exception {
/**
* Creates a new instance of ConnectionFailedException.
*
* @return {ConnectionFailedException}
*/
constructor(conName, driverName, error) {
const content = `Error occurred when trying to connect to database using ${conName} connection and ${driverName} driver.`

super(
content,
500,
'E_CONNECTION_FAILED_ERROR',
`${JSON.stringify(error, null, 2)}`,
)
}
}
10 changes: 4 additions & 6 deletions src/Factories/DriverFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { DriverExistException } from '#src/Exceptions/DriverExistException'
import { NotFoundDriverException } from '#src/Exceptions/NotFoundDriverException'
import { NotImplementedConfigException } from '#src/Exceptions/NotImplementedConfigException'
import { Log } from '@athenna/logger'
import { ConnectionFailedException } from '#src/Exceptions/ConnectionFailedException'
import { CloseConnectionFailedException } from '#src/Exceptions/CloseConnectionFailedException'

export class DriverFactory {
/**
Expand Down Expand Up @@ -132,9 +134,7 @@ export class DriverFactory {

return { runner, dataSource }
} catch (error) {
Log.error(
`Error occurred when trying to connect to database using ${conName} connection and ${driverName} driver: ${error}`,
)
throw ConnectionFailedException(conName, driverName, error)
}
}

Expand Down Expand Up @@ -165,9 +165,7 @@ export class DriverFactory {
`Successfully closed connection with database for ${driverName} driver`,
)
} catch (error) {
Log.success(
`Error occurred when trying to close the connection with database for ${driverName} driver: ${error}`,
)
throw new CloseConnectionFailedException(driverName, error)
}
}

Expand Down

0 comments on commit 90bfbc4

Please sign in to comment.