diff --git a/src/core.ts b/src/core.ts index 3ad6eb7..a7cf419 100644 --- a/src/core.ts +++ b/src/core.ts @@ -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'; @@ -258,10 +258,10 @@ export class Response { } async handleErrors(): Promise { - // 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) { diff --git a/src/error.ts b/src/error.ts index 63ffffa..8b9a112 100644 --- a/src/error.ts +++ b/src/error.ts @@ -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 */ @@ -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 {