Skip to content

Commit 4a93113

Browse files
authored
Merge pull request grpc-ecosystem#17 from remko/master
Throw error object on HTTP error
2 parents bd4c72b + 4010144 commit 4a93113

File tree

6 files changed

+214
-71
lines changed

6 files changed

+214
-71
lines changed

generator/template.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@ export function fetchReq<I, O>(path: string, init?: InitReq): Promise<O> {
107107
108108
const url = pathPrefix ? ` + "`${pathPrefix}${path}`" + ` : path
109109
110-
return fetch(url, req).then(r => r.json()) as Promise<O>
110+
return fetch(url, req).then(r => r.json().then((body: O) => {
111+
if (!r.ok) { throw body; }
112+
return body;
113+
})) as Promise<O>
111114
}
112115
113116
// NotifyStreamEntityArrival is a callback that will be called on streaming entity arrival

integration_tests/integration_test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ describe("test grpc-gateway-ts communication", () => {
1919
expect(result.result).to.equal(200)
2020
})
2121

22+
it("failing unary request", async () => {
23+
try {
24+
await CounterService.FailingIncrement({ counter: 199 }, { pathPrefix: "http://localhost:8081" });
25+
expect.fail("expected call to throw");
26+
} catch (e) {
27+
expect(e).to.have.property("message", "this increment does not work")
28+
expect(e).to.have.property("code", 14);
29+
}
30+
})
31+
2232
it('streaming request', async () => {
2333
const response = [] as number[]
2434
await CounterService.StreamingIncrements({ counter: 1 }, (resp) => response.push(resp.result), { pathPrefix: "http://localhost:8081" })

integration_tests/service.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"time"
66

7+
"google.golang.org/grpc/codes"
8+
"google.golang.org/grpc/status"
79
"google.golang.org/protobuf/types/known/emptypb"
810
)
911

@@ -21,6 +23,10 @@ func (r *RealCounterService) Increment(c context.Context, req *UnaryRequest) (*U
2123
}, nil
2224
}
2325

26+
func (r *RealCounterService) FailingIncrement(c context.Context, req *UnaryRequest) (*UnaryResponse, error) {
27+
return nil, status.Errorf(codes.Unavailable, "this increment does not work")
28+
}
29+
2430
func (r *RealCounterService) StreamingIncrements(req *StreamingRequest, service CounterService_StreamingIncrementsServer) error {
2531
times := 5
2632
counter := req.Counter

0 commit comments

Comments
 (0)