Skip to content

Commit

Permalink
[pinpoint-apm#117] adaptor error case
Browse files Browse the repository at this point in the history
  • Loading branch information
feelform committed Oct 18, 2022
1 parent 692eb72 commit 639f0ca
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
2 changes: 2 additions & 0 deletions lib/supports.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

'use strict'

const Logger = require('./utils/logger')

function getLog() {
return {}
}
Expand Down
12 changes: 6 additions & 6 deletions lib/utils/log/logger-output-adaptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ class LoggerOutputAdaptor {
}

debug(message) {
if (!this.output || typeof this.output.debug != 'function') {
console.error("The Adaptor doesn't has the debug function.")
if (!this.output || typeof this.output.debug != 'function' || typeof this.output.error != 'function') {
this.output.error("The Adaptor doesn't has the debug function.")
return
}
this.output.debug(message)
}

info(message) {
if (!this.output || typeof this.output.info != 'function') {
console.error("The Adaptor doesn't has the info function.")
if (!this.output || typeof this.output.info != 'function' || typeof this.output.error != 'function') {
this.output.error("The Adaptor doesn't has the info function.")
return
}
this.output.info(message)
}

warn(message) {
if (!this.output || typeof this.output.warn != 'function') {
console.error("The Adaptor doesn't has the warn function.")
if (!this.output || typeof this.output.warn != 'function' || typeof this.output.error != 'function') {
this.output.error("The Adaptor doesn't has the warn function.")
return
}
this.output.warn(message)
Expand Down
40 changes: 26 additions & 14 deletions test/utils/log/logger-output-adaptor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,63 @@ const test = require('tape')
const LoggerOutputAdaptor = require('../../../lib/utils/log/logger-output-adaptor')

test('Adaptor method duck typing validation', (t) => {
let actual = new LoggerOutputAdaptor({
const error = {
error: function () {
}
}

let cloneError = Object.assign({}, error)
let actual = new LoggerOutputAdaptor(Object.assign(cloneError, {
debug: function (message) {
this.actualMessage = message
}
})
}))
actual.debug('message')
t.equal(actual.output.actualMessage, 'message')

let actualUnsafe = new LoggerOutputAdaptor({
})
cloneError = Object.assign({}, error)
let actualUnsafe = new LoggerOutputAdaptor(Object.assign(cloneError, {
}))
actualUnsafe.debug('message')
t.false(actualUnsafe.output.debug, 'this.output.debug is undefined')


actual = new LoggerOutputAdaptor({
cloneError = Object.assign({}, error)
actual = new LoggerOutputAdaptor(Object.assign(cloneError, {
info: function (message) {
this.actualMessage = message
}
})
}))
actual.info('message2')
t.equal(actual.output.actualMessage, 'message2')

actualUnsafe = new LoggerOutputAdaptor({
})
cloneError = Object.assign({}, error)
actualUnsafe = new LoggerOutputAdaptor(Object.assign(cloneError, {
}))
actualUnsafe.info('message')
t.false(actualUnsafe.output.info, 'this.output.info is undefined')

actual = new LoggerOutputAdaptor({
cloneError = Object.assign({}, error)
actual = new LoggerOutputAdaptor(Object.assign(cloneError, {
warn: function (message) {
this.actualMessage = message
}
})
}))
actual.warn('message3')
t.equal(actual.output.actualMessage, 'message3')

actualUnsafe = new LoggerOutputAdaptor({
})
cloneError = Object.assign({}, error)
actualUnsafe = new LoggerOutputAdaptor(Object.assign(cloneError, {
}))
actualUnsafe.warn('message')
t.false(actualUnsafe.output.warn, 'this.output.warn is undefined')

actual = new LoggerOutputAdaptor({
cloneError = Object.assign({}, error)
actual = new LoggerOutputAdaptor(Object.assign(cloneError, {
error: function (message) {
this.actualMessage = message
}
})
}))
actual.error('message4')
t.equal(actual.output.actualMessage, 'message4')

Expand Down

0 comments on commit 639f0ca

Please sign in to comment.