diff --git a/packages/react-server/core/ReactServerAgent/Cache.js b/packages/react-server/core/ReactServerAgent/Cache.js index c6b413b77..5c3d6e9b0 100644 --- a/packages/react-server/core/ReactServerAgent/Cache.js +++ b/packages/react-server/core/ReactServerAgent/Cache.js @@ -117,12 +117,17 @@ class CacheEntry { // once initially, when not loaded, and once again when // the request arrives + var err = state.err; + if (err) { + err.response = this._rehydrateResponse(err.response); + } + this.url = state.url; this.requestData = state.requestData; this.requesters = state.requesters; this.loaded = state.loaded; this.res = this._rehydrateResponse(state.res); - this.err = state.err; + this.err = err; // TODO FIXME: these won't work if the response from the server was an error diff --git a/packages/react-server/core/ReactServerAgent/__tests__/ReactServerAgentSpec.js b/packages/react-server/core/ReactServerAgent/__tests__/ReactServerAgentSpec.js index b4cb0333d..ecc1e67ee 100644 --- a/packages/react-server/core/ReactServerAgent/__tests__/ReactServerAgentSpec.js +++ b/packages/react-server/core/ReactServerAgent/__tests__/ReactServerAgentSpec.js @@ -635,6 +635,43 @@ describe("ReactServerAgent", () => { .done(); })); + + it("rehydrates response body for non-200 requests", withRlsContext(done => { + var URL = "/error"; + + ReactServerAgent.get(URL) + .then(res => { + // this is a failure + expect(res).toBeUndefined(); + done(); + }) + .catch(err => { + var cache = ReactServerAgent.cache(); + var dehydrated = cache.dehydrate(); + + // make sure that the cache is empty + ReactServerAgent._clearCache(); + + cache = ReactServerAgent.cache(); + + // verify that cache can be rehydrated + cache.rehydrate(dehydrated); + + // only one entry; just grab it via index + var entry = cache.dataCache[URL][0]; + console.log(entry); + expect(entry).toBeDefined(); + expect(entry.err).toBeDefined(); + expect(entry.err.response).toBeDefined(); + + // this will only be defined if rehydrate is working + // properly + expect(entry.err.response.body).toBeDefined(); + + done(); + }) + .done(); + })); }); function getSingleDehydratedCacheEntry(dehydratedCache, url) {