Skip to content

Commit

Permalink
fix logging of warnings for new error formats
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthvp committed Aug 26, 2021
1 parent cefcfed commit 22ec8f4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
22 changes: 19 additions & 3 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
}
}
}
Expand Down
33 changes: 32 additions & 1 deletion tests/core.test.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down Expand Up @@ -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.')
Expand Down Expand Up @@ -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();
});

});
});

0 comments on commit 22ec8f4

Please sign in to comment.