Skip to content

Commit

Permalink
Use db.NotFoundErr to signal not found response
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudomuto committed Apr 13, 2018
1 parent a76ebfa commit 0f1e62b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
name = "github.com/golang/protobuf"
version = "1.0.0"

[[constraint]]
name = "github.com/sirupsen/logrus"
version = "1.0.5"

[[constraint]]
name = "github.com/stretchr/testify"
version = "1.2.1"
Expand Down
20 changes: 0 additions & 20 deletions server/api/errors.go

This file was deleted.

13 changes: 7 additions & 6 deletions server/api/interceptor.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package api

import (
"context"
"time"

"github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"

grpcmiddleware "github.com/grpc-ecosystem/go-grpc-middleware"
"context"
"time"

"github.com/backstopmedia/gRPC-book-example/server/db"
)

// Interceptors implements the grpc.UnaryServerInteceptor function to add
// interceptors around all gRPC calls
func Interceptors() grpc.UnaryServerInterceptor {
return grpcmiddleware.ChainUnaryServer(
return grpc_middleware.ChainUnaryServer(
LoggingInterceptor,
ErrorsInterceptor,
)
Expand All @@ -26,7 +27,7 @@ func ErrorsInterceptor(ctx context.Context, req interface{}, info *grpc.UnarySer
out, err = handler(ctx, req)

switch tErr := err.(type) {
case NotFoundErr:
case db.NotFoundErr:
return out, grpc.Errorf(codes.NotFound, tErr.Error())
}

Expand Down
14 changes: 8 additions & 6 deletions server/api/interceptor_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
package api_test

import (
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"

"bytes"
"context"
"errors"
"os"
"testing"

"github.com/backstopmedia/gRPC-book-example/server/api"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"github.com/backstopmedia/gRPC-book-example/server/db"
)

func TestErrorsInterceptor(t *testing.T) {
t.Run("Errros interceptor returns a codes.NotFound when a DB not found is returned", func(t *testing.T) {
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return nil, api.NotFoundErr{}
return nil, db.NotFoundErr{}
}

out, err := api.ErrorsInterceptor(nil, nil, nil, handler)
Expand Down

0 comments on commit 0f1e62b

Please sign in to comment.