From e9ebc53361c8fff794f7041bead8e1e81348de40 Mon Sep 17 00:00:00 2001 From: Ives van Hoorne Date: Thu, 28 Jun 2018 06:39:24 -0700 Subject: [PATCH] Move Http Error logic to HttpGetStore Reviewed By: rafeca Differential Revision: D8661425 fbshipit-source-id: 6d59a6a4e50d2621703c86a82a139526f4b1f76a --- packages/metro-cache/src/stores/HttpError.js | 23 +++++++++++++++++++ packages/metro-cache/src/stores/HttpStore.js | 12 ++++++---- .../src/stores/__tests__/HttpStore-test.js | 18 +++------------ 3 files changed, 33 insertions(+), 20 deletions(-) create mode 100644 packages/metro-cache/src/stores/HttpError.js diff --git a/packages/metro-cache/src/stores/HttpError.js b/packages/metro-cache/src/stores/HttpError.js new file mode 100644 index 0000000000..8cefbd60c8 --- /dev/null +++ b/packages/metro-cache/src/stores/HttpError.js @@ -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; diff --git a/packages/metro-cache/src/stores/HttpStore.js b/packages/metro-cache/src/stores/HttpStore.js index e0792dd1f7..0de85460c6 100644 --- a/packages/metro-cache/src/stores/HttpStore.js +++ b/packages/metro-cache/src/stores/HttpStore.js @@ -10,6 +10,8 @@ 'use strict'; +const HttpError = require('./HttpError'); + const http = require('http'); const https = require('https'); const url = require('url'); @@ -26,6 +28,8 @@ const ZLIB_OPTIONS = { }; class HttpStore { + static HttpError = HttpError; + _module: typeof http | typeof https; _timeout: number; @@ -81,11 +85,9 @@ class HttpStore { // 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; } diff --git a/packages/metro-cache/src/stores/__tests__/HttpStore-test.js b/packages/metro-cache/src/stores/__tests__/HttpStore-test.js index 7e82cb4efa..c235728930 100644 --- a/packages/metro-cache/src/stores/__tests__/HttpStore-test.js +++ b/packages/metro-cache/src/stores/__tests__/HttpStore-test.js @@ -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]; @@ -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(); }); });