Skip to content

Commit

Permalink
refactor: create named exceptions and write provider tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jul 21, 2020
1 parent b38a34b commit 3fb63ab
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 20 deletions.
11 changes: 11 additions & 0 deletions exceptions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"E_INVALID_LOGGER_CONFIG": {
"message": "Missing required properties in logger config",
"status": 500,
"code": "E_INVALID_LOGGER_CONFIG",
"help": [
"The logger config must have \"name\", \"level\" and \"enabled\" properties.",
"Double check the \"config/app\" file to ensure the config is correct."
]
}
}
4 changes: 2 additions & 2 deletions npm-audit.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ <h5 class="card-title">
<div class="card">
<div class="card-body">
<h5 class="card-title">
13
22
</h5>
<p class="card-text">Dependencies</p>
</div>
</div>
<div class="card">
<div class="card-body">
<h5 class="card-title">
July 20th 2020, 5:50:34 pm
July 21st 2020, 10:14:11 am
</h5>
<p class="card-text">Last updated</p>
</div>
Expand Down
73 changes: 60 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"build/providers",
"build/src",
"build/standalone.d.ts",
"build/standalone.js"
"build/standalone.js",
"build/exceptions.json"
],
"scripts": {
"mrm": "mrm --preset=@adonisjs/mrm-preset",
Expand All @@ -34,6 +35,7 @@
"author": "virk,adonisjs",
"license": "MIT",
"devDependencies": {
"@adonisjs/config": "^1.1.0",
"@adonisjs/fold": "^6.3.5",
"@adonisjs/mrm-preset": "^2.4.0",
"@types/node": "^14.0.23",
Expand Down Expand Up @@ -79,6 +81,7 @@
"anyBranch": false
},
"dependencies": {
"@poppinss/utils": "^2.3.0",
"@types/pino": "^6.3.0",
"abstract-logging": "^2.0.0",
"pino": "^6.4.0"
Expand All @@ -94,5 +97,8 @@
"bugs": {
"url": "https://github.com/adonisjs/logger/issues"
},
"homepage": "https://github.com/adonisjs/logger#readme"
"homepage": "https://github.com/adonisjs/logger#readme",
"adonisjs": {
"exceptions": "./build/exceptions.json"
}
}
20 changes: 18 additions & 2 deletions providers/LoggerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,30 @@
*/

import { IocContract } from '@adonisjs/fold'
import { Logger } from '../src/Logger'

export default class LoggerProvider {
constructor(protected container: IocContract) {}

/**
* Ensure config is valid
*/
private validateConfig(config: any) {
if (config.name === undefined || config.enabled === undefined || config.level === undefined) {
const { InvalidConfigException } = require('../src/Exceptions/InvalidConfigException')
throw InvalidConfigException.invoke()
}
}

/**
* Register logger
*/
public register() {
this.container.singleton('Adonis/Core/Logger', () => {
return new Logger(this.container.use('Adonis/Core/Config').get('app.logger', {}))
const config = this.container.use('Adonis/Core/Config').get('app.logger', {})
this.validateConfig(config)

const { Logger } = require('../src/Logger')
return new Logger(config)
})
}
}
20 changes: 20 additions & 0 deletions src/Exceptions/InvalidConfigException.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* @adonisjs/logger
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { Exception } from '@poppinss/utils'
import exceptions from '../../exceptions.json'

export class InvalidConfigException extends Exception {
public static invoke(): InvalidConfigException {
const exception = exceptions.E_INVALID_LOGGER_CONFIG
const error = new this(exception.message, exception.status, exception.code)
error.help = exception.help.join('\n')
return error
}
}
51 changes: 51 additions & 0 deletions test/logger-provider.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* @adonisjs/events
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import test from 'japa'
import { join } from 'path'
import { Registrar, Ioc } from '@adonisjs/fold'
import { Config } from '@adonisjs/config/build/standalone'

import { Logger } from '../src/Logger'

test.group('Logger Provider', () => {
test('register event provider', async (assert) => {
const ioc = new Ioc()
ioc.bind('Adonis/Core/Config', () => {
return new Config({
app: {
logger: {
name: 'adonis',
level: 30,
enabled: true,
},
},
})
})

const registrar = new Registrar(ioc, join(__dirname, '..'))
await registrar.useProviders(['./providers/LoggerProvider']).registerAndBoot()

assert.instanceOf(ioc.use('Adonis/Core/Logger'), Logger)
assert.deepEqual(ioc.use('Adonis/Core/Logger'), ioc.use('Adonis/Core/Logger'))
})

test('raise exception when config is invalid', async (assert) => {
const ioc = new Ioc()
ioc.bind('Adonis/Core/Config', () => {
return new Config({})
})

const registrar = new Registrar(ioc, join(__dirname, '..'))
await registrar.useProviders(['./providers/LoggerProvider']).registerAndBoot()

const fn = () => ioc.use('Adonis/Core/Logger')
assert.throw(fn, 'E_INVALID_LOGGER_CONFIG: Missing required properties in logger config')
})
})
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "./node_modules/@adonisjs/mrm-preset/_tsconfig"
"extends": "./node_modules/@adonisjs/mrm-preset/_tsconfig",
"compilerOptions": {
"resolveJsonModule": true
}
}

0 comments on commit 3fb63ab

Please sign in to comment.