diff --git a/src/core.ts b/src/core.ts index 73d51e8..3ad6eb7 100644 --- a/src/core.ts +++ b/src/core.ts @@ -234,9 +234,25 @@ export class Response { showWarnings() { if (this.response.warnings && !this.bot.options.suppressAPIWarnings) { - for (let [key, info] of Object.entries(this.response.warnings)) { - // @ts-ignore - log(`[W] Warning received from API: ${key}: ${info.warnings}`); + if (Array.isArray(this.response.warnings)) { + // new error formats + for (let { code, module, info, html, text } of this.response.warnings) { + if (code === 'deprecation-help') { + // skip + continue; + } + const msg = + info || // errorformat=bc + text || // errorformat=wikitext/plaintext + html; // errorformat=html + log(`[W] Warning received from API: ${module}: ${msg}`); + } + } else { + // legacy error format (bc) + for (let [key, info] of Object.entries(this.response.warnings)) { + // @ts-ignore + log(`[W] Warning received from API: ${key}: ${info.warnings}`); + } } } } diff --git a/tests/core.test.js b/tests/core.test.js index 572c366..2ed8149 100644 --- a/tests/core.test.js +++ b/tests/core.test.js @@ -1,8 +1,9 @@ const { Request } = require('../build/core'); +const logger = require('../build/log'); const { MwnError } = require('../build/error'); const { expect, mwn } = require('./test_base'); -const { bot } = require('./local_wiki'); +const { bot, setup, teardown, sinon } = require('./local_wiki'); describe('core', function () { describe('Request', function () { @@ -44,6 +45,11 @@ describe('core', function () { describe('Response', function () { + before('logs in and gets token & namespaceInfo', setup); + after('teardown', teardown); + + // Errors + it('default legacy error format (bc)', async () => { await expect(bot.request({ action: 'qwertyuiop' })) .to.be.eventually.rejectedWith('badvalue: Unrecognized value for parameter "action": qwertyuiop.') @@ -84,5 +90,30 @@ describe('core', function () { }); }); + // Warnings + + it('shows warnings', async () => { + sinon.spy(logger, 'log'); + await bot.request({ action: 'query', titles: 'Main Page', prop: 'revisions', rvprop: 'content' }); + expect(logger.log).to.have.been.calledTwice; + sinon.restore(); + }); + + it('shows warnings (new errorformats)', async () => { + sinon.spy(logger, 'log'); + await bot.request({ errorformat: 'html', action: 'query', titles: 'Main Page', prop: 'revisions', rvprop: 'content' }); + expect(logger.log).to.have.been.calledOnce; + expect(logger.log.firstCall.firstArg).to.include("[W] Warning received from API: query+revisions: Because"); + + await bot.request({ errorformat: 'wikitext', action: 'query', titles: 'Main Page', prop: 'revisions', rvprop: 'content' }); + expect(logger.log).to.have.been.calledTwice; + expect(logger.log.secondCall.firstArg).to.include("[W] Warning received from API: query+revisions: Because"); + + await bot.request({ errorformat: 'plaintext', action: 'query', titles: 'Main Page', prop: 'revisions', rvprop: 'content' }); + expect(logger.log).to.have.been.calledThrice; + expect(logger.log.thirdCall.firstArg).to.include("[W] Warning received from API: query+revisions: Because"); + sinon.restore(); + }); + }); });