Skip to content

Commit

Permalink
catch error when transforming output
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip committed Aug 24, 2018
1 parent 9528ab6 commit 2491f75
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/electrode-react-webapp/lib/render-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,18 @@ class RenderOutput {
}

_finish() {
if (this._resolve) {
this._resolve(this._context.transform(this._result, this));
try {
if (this._resolve) {
this._resolve(this._context.transform(this._result, this));
}
} catch (error) {
if (this._reject) {
this._reject(error);
} else {
throw error;
}
}

this._resolve = this._reject = undefined;
}

Expand Down
27 changes: 27 additions & 0 deletions packages/electrode-react-webapp/test/spec/hapi.index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,33 @@ describe("hapi electrode-react-webapp", () => {
});
});

it("should handle unexpected errors", () => {
const content = {
html: "<div>Hello Electrode</div>",
prefetch: "console.log('Hello');"
};
Object.defineProperty(content, "status", {
get: () => {
throw new Error("unexpected error");
}
});

assign(mainRoutePathOptions, { content: () => Promise.resolve(content) });

return electrodeServer(config).then(server => {
return server
.inject({
method: "GET",
url: "/"
})
.then(res => {
stopServer(server);
expect(res.statusCode).to.equal(500);
expect(res.result).contains("unexpected error");
});
});
});

it("should handle 200 status @noss", () => {
assign(mainRoutePathOptions, {
content: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,13 @@ describe("render-output", function() {
);
});
});

it("should rethrow if no _reject is available in _finish", () => {
const ro = new RenderOutput();
ro._reject = undefined;
ro._resolve = () => {
throw new Error("test error");
};
expect(() => ro._finish()).to.throw("test error");
});
});

0 comments on commit 2491f75

Please sign in to comment.