Skip to content

Commit

Permalink
Move Http Error logic to HttpGetStore
Browse files Browse the repository at this point in the history
Reviewed By: rafeca

Differential Revision: D8661425

fbshipit-source-id: 6d59a6a4e50d2621703c86a82a139526f4b1f76a
  • Loading branch information
Ives van Hoorne authored and facebook-github-bot committed Jun 28, 2018
1 parent 46eb785 commit e9ebc53
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
23 changes: 23 additions & 0 deletions packages/metro-cache/src/stores/HttpError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/

'use strict';

class HttpError extends Error {
statusCode: number;

constructor(message: string, statusCode: number) {
super(message);

this.statusCode = statusCode;
}
}

module.exports = HttpError;
12 changes: 7 additions & 5 deletions packages/metro-cache/src/stores/HttpStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

'use strict';

const HttpError = require('./HttpError');

const http = require('http');
const https = require('https');
const url = require('url');
Expand All @@ -26,6 +28,8 @@ const ZLIB_OPTIONS = {
};

class HttpStore<T> {
static HttpError = HttpError;

_module: typeof http | typeof https;
_timeout: number;

Expand Down Expand Up @@ -81,11 +85,9 @@ class HttpStore<T> {
// Consume all the data from the response without processing it.
res.resume();

if (res.statusCode === 404) {
resolve(null);
} else {
reject(new Error('HTTP error: ' + res.statusCode));
}
reject(
new HttpError('HTTP error: ' + res.statusCode, res.statusCode),
);

return;
}
Expand Down
18 changes: 3 additions & 15 deletions packages/metro-cache/src/stores/__tests__/HttpStore-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,7 @@ describe('HttpStore', () => {
expect(await promise).toEqual({foo: 42});
});

it('resolves with "null" when HTTP 404 is returned', async () => {
const store = new HttpStore({endpoint: 'http://example.com'});
const promise = store.get(Buffer.from('key'));
const [opts, callback] = require('http').request.mock.calls[0];

expect(opts.method).toEqual('GET');

callback(responseHttpError(404));
jest.runAllTimers();

expect(await promise).toEqual(null);
});

it('rejects when an HTTP different of 200/404 is returned', done => {
it('rejects when an HTTP different from 200 is returned', done => {
const store = new HttpStore({endpoint: 'http://example.com'});
const promise = store.get(Buffer.from('key'));
const [opts, callback] = require('http').request.mock.calls[0];
Expand All @@ -119,8 +106,9 @@ describe('HttpStore', () => {
jest.runAllTimers();

promise.catch(err => {
expect(err).toBeInstanceOf(Error);
expect(err).toBeInstanceOf(HttpStore.HttpError);
expect(err.message).toMatch(/HTTP error: 503/);
expect(err.statusCode).toBe(503);
done();
});
});
Expand Down

0 comments on commit e9ebc53

Please sign in to comment.