Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/superagent-wrapper/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ const patchRequest = <Req extends SuperAgentRequest, Route extends h.HttpRoute>(
return res as ExpectedDecodedResponse<Route, StatusCode>;
}
});

// Stop superagent from throwing on non-2xx status codes
patchedReq.ok(() => true);

return patchedReq;
};

Expand Down
32 changes: 31 additions & 1 deletion packages/superagent-wrapper/test/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import express from 'express';
import * as E from 'fp-ts/Either';
import * as t from 'io-ts';
import { NumberFromString } from 'io-ts-types';
import superagent from 'superagent';
import supertest from 'supertest';
import { URL } from 'url';

import { supertestRequestFactory } from '../src/request';
import { superagentRequestFactory, supertestRequestFactory } from '../src/request';
import { buildApiClient } from '../src/routes';

const PostTestRoute = h.httpRoute({
Expand Down Expand Up @@ -184,4 +186,32 @@ describe('request', () => {
assert.isTrue(result);
});
});

describe('superagent', async () => {
it('does not throw on non-2xx status codes', async () => {
// Figure out what host/port supertest set up (the response is just thrown away on purpose)
const superTestReq = apiClient['api.v1.test'].post({
id: 1337,
foo: 'test',
bar: 42,
});

// Construct an api client that uses superagent, with the base url extracted from the supertest
// request above.
const url = new URL(superTestReq.url);
url.pathname = '/';
const superagentClient = buildApiClient(
superagentRequestFactory(superagent, url.toString()),
TestRoutes,
);

const req = await superagentClient['api.v1.test']
.post({ id: 1337, foo: 'test', bar: 42 })
.set('x-send-unexpected-status-code', 'true')
.decode();

assert.equal(req.status, 'decodeError');
assert.equal(req.original.status, 400);
Comment on lines +213 to +214
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, so here's how we get the value behind the non-standard 'decodeError'. I still feel weird about having numeric status codes except for 'decodeError' but still don't have a great improvement in mind

});
});
});