Skip to content

Commit

Permalink
fix bug in handling of OAuth authentication failure for non-legacy er…
Browse files Browse the repository at this point in the history
…ror format

By always populating `info` prop in MwnError with the error reason.
  • Loading branch information
siddharthvp committed Sep 10, 2021
1 parent 4b1c8bc commit 387a980
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
* the Request and Response classes defined in this file.
*/

import axios, { AxiosResponse, AxiosRequestConfig } from 'axios';
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
import axiosCookieJarSupport from 'axios-cookiejar-support';
import * as formData from 'form-data';
import * as OAuth from 'oauth-1.0a';

import type { mwn, ApiParams } from './bot';
import type { ApiParams, mwn } from './bot';
import { log } from './log';
import { rejectWithError } from './error';
import { MwnError, rejectWithError } from './error';
import { merge, mergeDeep1, sleep } from './utils';
import { ApiResponse } from './api_response_types';

Expand Down Expand Up @@ -258,10 +258,10 @@ export class Response {
}

async handleErrors(): Promise<void | ApiResponse> {
// TODO: support non-legacy error formats
let error =
let error = new MwnError(
this.response.error || // errorformat=bc (default)
this.response.errors?.[0]; // other error formats
this.response.errors?.[0], // other error formats
);
if (error) {
if (this.requestOptions.retryNumber < this.bot.options.maxRetries) {
switch (error.code) {
Expand Down
7 changes: 7 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export type MwnErrorConfig = {
};

export class MwnError extends Error {
// The error object can have arbitrary properties
[key: string]: any;
code?: string;
info?: string;

/**
* @param {Object} config
*/
Expand All @@ -24,7 +29,9 @@ export class MwnError extends Error {
const code = !config.code || config.code.startsWith('mwn') ? '' : config.code + ': ';
const info = config.info || config.text || config.html || '';
super(code + info);

Object.assign(this, config);
this.info = info; // not already present in case of html/wikitext/plaintext error formats
}

static MissingPage = class MwnErrorMissingPage extends MwnError {
Expand Down

0 comments on commit 387a980

Please sign in to comment.