Skip to content

Commit

Permalink
Support non-200 response status codes
Browse files Browse the repository at this point in the history
These are used by a few API modules, notably some WikiLambda APIs (or
Wikifunctions APIs, as they’re apparently starting to be called now). I
hope that checking for the MediaWiki-API-Error response header is enough
to distinguish between such responses and genuine internal errors that
we shouldn’t even try to decode.

Only unit-tested, because this feature is so rare that none of the APIs
using it seem suitable for an integration test against a real MediaWiki.

Fixes #30.
  • Loading branch information
lucaswerkmeister committed Apr 9, 2024
1 parent cee693d commit e067c9c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ but this file may sometimes contain later improvements (e.g. typo fixes).

## next (not yet released)

- Error responses with non-200 HTTP status codes are now supported,
as long as the `MediaWiki-API-Error` response header is present
to indicate that the response represents a regular error and not an internal error.
(The status code is ignored;
the response body is expected to contain a response with one or more regular errors.)
- Updated dependencies.

## v0.8.1 (2023-11-12)
Expand Down
2 changes: 1 addition & 1 deletion core.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ class Session {
body,
} = internalResponse;

if ( status !== 200 ) {
if ( status !== 200 && !( 'mediawiki-api-error' in responseHeaders ) ) {
throw new Error( `API request returned non-200 HTTP status code: ${ status }` );
}

Expand Down
12 changes: 11 additions & 1 deletion test/unit/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,17 @@ describe( 'Session', () => {
await session.request( params );
} );

it( 'throws on non-200 status', async () => {
it( 'supports non-200 status with mediawiki-api-error response header', async () => {
const session = singleRequestSession( { action: 'query' }, {
status: 404,
headers: { 'mediawiki-api-error': 'some-not-found-error' },
body: { error: { code: 'some-not-found-error' } },
} );
await expect( session.request( { action: 'query' } ) )
.to.be.rejectedWith( ApiErrors );
} );

it( 'throws on non-200 status without mediawiki-api-error response header', async () => {
const session = singleRequestSession( { action: 'query' }, {
status: 502,
body: 'irrelevant',
Expand Down

0 comments on commit e067c9c

Please sign in to comment.