Skip to content
This repository was archived by the owner on Jul 17, 2023. It is now read-only.

Commit 83d088a

Browse files
feat(atlas): log validation errors via atlas.log 🔎
This should dramatically improve developer ergonomics by making it easier to track down component validation errors. Previously it was very difficult to understand why exactly and where from a validation error was thrown. Now all the relevant information will be printed to the console (or other configured Pino output). 💪 Fixes #66
1 parent 36d862c commit 83d088a

File tree

7 files changed

+21
-7
lines changed

7 files changed

+21
-7
lines changed

packages/atlas/src/private/component-container.mjs

+9-1
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,20 @@ class ComponentContainer {
8282
const config = defaults(info.config, this.Component.defaults)
8383

8484
if (!info.validator.validate(info.Component.config, config)) {
85-
throw new ValidationError(info.validator.errors, {
85+
const err = new ValidationError(info.validator.errors, {
8686
type: this.type,
8787
alias: this.alias,
8888
schema: info.Component.config,
8989
config,
9090
})
91+
92+
// For better developer ergonomics, also log the error for easy discoverability
93+
atlas.log.error({
94+
err,
95+
errors: err.errors,
96+
context: err.context,
97+
}, 'component validation error')
98+
throw err
9199
}
92100

93101
atlas.log.trace({

packages/atlas/test/action.test.mjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,10 @@ describe('Atlas::action()', () => {
109109
}).to.throw(FrameworkError, /Unneeded aliases for component dummy/u)
110110
})
111111

112-
it('throws when user config fails component config schema', () => {
112+
it('throws when user config fails component config schema', function() {
113113
options.config.actions.dummy = { lol: true }
114114
atlas = new Atlas(options)
115+
this.sandbox.stub(atlas.log, 'error')
115116

116117
const action = sinon.spy()
117118
action.config = {

packages/atlas/test/hook.test.mjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,10 @@ describe('Atlas::hook()', () => {
145145
}).to.throw(FrameworkError, /Missing aliases for component dummy/u)
146146
})
147147

148-
it('throws when user config fails component config schema', () => {
148+
it('throws when user config fails component config schema', function() {
149149
options.config.hooks.dummy = { lol: true }
150150
atlas = new Atlas(options)
151+
this.sandbox.stub(atlas.log, 'error')
151152

152153
const hook = sinon.spy()
153154
hook.config = {

packages/atlas/test/service.test.mjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,10 @@ describe('Atlas::service()', () => {
108108
}).to.throw(FrameworkError, /Unneeded aliases for component dummy/u)
109109
})
110110

111-
it('throws when user config fails component config schema', () => {
111+
it('throws when user config fails component config schema', function() {
112112
options.config.services.dummy = { lol: true }
113113
atlas = new Atlas(options)
114+
this.sandbox.stub(atlas.log, 'error')
114115

115116
const service = sinon.spy()
116117
service.config = {

packages/braintree/test/service/api.test.mjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ describe('Service: Braintree', () => {
1515
expect(Braintree.config).to.be.an('object')
1616
})
1717

18-
it('throws on invalid config', () => {
18+
it('throws on invalid config', function() {
1919
const atlas = new Atlas({ root: __dirname })
20+
this.sandbox.stub(atlas.log, 'error')
2021

2122
expect(() =>
2223
atlas.service('braintree', Braintree)).to.throw(errors.ValidationError)

packages/nodemailer/test/service/api.test.mjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ describe('Service: Nodemailer', () => {
1515
expect(Nodemailer.config).to.be.an('object')
1616
})
1717

18-
it('throws on invalid config', () => {
18+
it('throws on invalid config', function() {
1919
const atlas = new Atlas({ root: __dirname })
20+
this.sandbox.stub(atlas.log, 'error')
2021

2122
expect(() =>
2223
atlas.service('nodemailer', Nodemailer)).to.throw(errors.ValidationError)

packages/objection/test/service/api.test.mjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ describe('Service: Objection', () => {
1515
expect(Objection.config).to.be.an('object')
1616
})
1717

18-
it('throws on invalid config', () => {
18+
it('throws on invalid config', function() {
1919
const atlas = new Atlas({ root: __dirname })
20+
this.sandbox.stub(atlas.log, 'error')
2021

2122
expect(() =>
2223
atlas.service('objection', Objection)).to.throw(errors.ValidationError)

0 commit comments

Comments
 (0)