Skip to content

Commit

Permalink
Throw error object on HTTP error
Browse files Browse the repository at this point in the history
  • Loading branch information
remko committed May 24, 2021
1 parent e2b40a5 commit 4010144
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 71 deletions.
5 changes: 4 additions & 1 deletion generator/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ export function fetchReq<I, O>(path: string, init?: InitReq): Promise<O> {
const url = pathPrefix ? ` + "`${pathPrefix}${path}`" + ` : path
return fetch(url, req).then(r => r.json()) as Promise<O>
return fetch(url, req).then(r => r.json().then((body: O) => {
if (!r.ok) { throw body; }
return body;
})) as Promise<O>
}
// NotifyStreamEntityArrival is a callback that will be called on streaming entity arrival
Expand Down
10 changes: 10 additions & 0 deletions integration_tests/integration_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ describe("test grpc-gateway-ts communication", () => {
expect(result.result).to.equal(200)
})

it("failing unary request", async () => {
try {
await CounterService.FailingIncrement({ counter: 199 }, { pathPrefix: "http://localhost:8081" });
expect.fail("expected call to throw");
} catch (e) {
expect(e).to.have.property("message", "this increment does not work")
expect(e).to.have.property("code", 14);
}
})

it('streaming request', async () => {
const response = [] as number[]
await CounterService.StreamingIncrements({ counter: 1 }, (resp) => response.push(resp.result), { pathPrefix: "http://localhost:8081" })
Expand Down
6 changes: 6 additions & 0 deletions integration_tests/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"time"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
)

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

func (r *RealCounterService) FailingIncrement(c context.Context, req *UnaryRequest) (*UnaryResponse, error) {
return nil, status.Errorf(codes.Unavailable, "this increment does not work")
}

func (r *RealCounterService) StreamingIncrements(req *StreamingRequest, service CounterService_StreamingIncrementsServer) error {
times := 5
counter := req.Counter
Expand Down
Loading

0 comments on commit 4010144

Please sign in to comment.