Skip to content

Commit c871ec3

Browse files
committed
Migrate example to the new webrpc errors
1 parent 57b34ea commit c871ec3

File tree

5 files changed

+21
-9
lines changed

5 files changed

+21
-9
lines changed

_examples/petStore/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ proto: generate
66
generate:
77
go generate ./...
88

9-
run-server:
9+
run:
1010
@echo "Running server. You can test it with:"
1111
@echo "$$ go test --serverUrl=http://localhost:8080 ./..."
1212
@echo

_examples/petStore/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
```
88
3. Run server
99
```
10-
$ make run-server
10+
$ make run
1111
```
1212
4. Run client tests
1313
```

_examples/petStore/proto/api.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ import (
99
"github.com/google/uuid"
1010
)
1111

12-
// -- debug: go:webrpc json -out=./petstore.gen.json
13-
// -- debug: go:webrpc debug -out=./petstore.debug.gen.txt
14-
//
1512
//go:webrpc golang -server -pkg=proto -json=stdlib -types=false -out=./server.gen.go
1613
//go:webrpc golang -client -pkg=client -json=stdlib -out=./client/petstore.gen.go
1714
//go:webrpc typescript -client -out=./client/petstore.gen.ts
1815
//go:webrpc openapi -out=./petstore.gen.yaml
16+
//go:disabled json -out=./petstore.gen.json
17+
//go:disabled debug -out=./petstore.debug.gen.txt
1918
type PetStore interface {
2019
GetPet(ctx context.Context, ID int64) (pet *Pet, err error)
2120
ListPets(ctx context.Context) (pets []*Pet, err error)

_examples/petStore/proto/errors.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package proto
2+
3+
var (
4+
// General.
5+
ErrTODO = WebRPCError{Code: 1000, Name: "NotImplemented", Message: "TODO: Not implemented", HTTPStatus: 500}
6+
ErrDeprecated = WebRPCError{Code: 1001, Name: "Deprecated", Message: "Endpoint is deprecated", HTTPStatus: 410}
7+
ErrRateLimited = WebRPCError{Code: 1002, Name: "RateLimited", Message: "Rate limited. Please, slow down", HTTPStatus: 429}
8+
ErrInvalidRequest = WebRPCError{Code: 1003, Name: "InvalidRequest", Message: "Invalid request", HTTPStatus: 400}
9+
ErrUnexpected = WebRPCError{Code: 1004, Name: "Unexpected", Message: "Unexpected server error", HTTPStatus: 500}
10+
11+
// Pets.
12+
ErrPetNotFound = WebRPCError{Code: 2000, Name: "PetNotFound", Message: "Pet not found", HTTPStatus: 400}
13+
)

_examples/petStore/server/pets.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
func (s *API) GetPet(ctx context.Context, ID int64) (pet *proto.Pet, err error) {
1010
pet, ok := s.PetStore[ID]
1111
if !ok {
12-
return nil, proto.ErrorNotFound("pet(%v) not found", ID)
12+
return nil, proto.ErrPetNotFound.WithCausef("pet id(%v) not found", ID)
1313
}
1414
return pet, nil
1515
}
@@ -25,7 +25,7 @@ func (s *API) ListPets(ctx context.Context) (pets []*proto.Pet, err error) {
2525

2626
func (s *API) CreatePet(ctx context.Context, pet *proto.Pet) (*proto.Pet, error) {
2727
if pet == nil {
28-
return nil, proto.ErrorInvalidArgument("pet", "pet is required")
28+
return nil, proto.ErrInvalidRequest.WithCausef("pet is required")
2929
}
3030

3131
s.mu.Lock()
@@ -40,15 +40,15 @@ func (s *API) CreatePet(ctx context.Context, pet *proto.Pet) (*proto.Pet, error)
4040

4141
func (s *API) UpdatePet(ctx context.Context, ID int64, pet *proto.Pet) (*proto.Pet, error) {
4242
if pet == nil {
43-
return nil, proto.ErrorInvalidArgument("pet", "pet is required")
43+
return nil, proto.ErrInvalidRequest.WithCausef("pet is required")
4444
}
4545

4646
s.mu.Lock()
4747
defer s.mu.Unlock()
4848

4949
_, ok := s.PetStore[pet.ID]
5050
if !ok {
51-
return nil, proto.ErrorNotFound("pet(%v) not found", ID)
51+
return nil, proto.ErrPetNotFound.WithCausef("pet id(%v) not found", ID)
5252
}
5353

5454
s.PetStore[pet.ID] = pet

0 commit comments

Comments
 (0)