-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: return end-user error message in gRPC endpoints
The middleware that maps package errors to gRPC status codes is updated to return a human-friendly error message if it is present in the error. The package https://github.com/Southclaws/fault/fmsg is used to add such error messages.
- Loading branch information
Showing
4 changed files
with
133 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package middleware | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/Southclaws/fault" | ||
"github.com/Southclaws/fault/fmsg" | ||
qt "github.com/frankban/quicktest" | ||
"github.com/jackc/pgconn" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func TestAsGRPCError(t *testing.T) { | ||
c := qt.New(t) | ||
|
||
c.Run("nil", func(c *qt.C) { | ||
c.Assert(AsGRPCError(nil), qt.IsNil) | ||
}) | ||
|
||
c.Run("unknown", func(c *qt.C) { | ||
in := &pgconn.PgError{ | ||
Severity: "FATAL", | ||
Code: "08006", | ||
Message: "connection_failure", | ||
Detail: "connection_failure", | ||
} | ||
|
||
got := AsGRPCError(in) | ||
c.Assert(got, qt.IsNotNil) | ||
c.Assert(got, qt.Equals, in) | ||
}) | ||
|
||
testcases := []struct { | ||
name string | ||
in error | ||
wantCode codes.Code | ||
wantMessage string | ||
}{ | ||
{ | ||
name: "pq unique constraint", | ||
in: &pgconn.PgError{ | ||
Severity: "FATAL", | ||
Code: "23505", | ||
Message: "unique_violation", | ||
Detail: "unique_violation", | ||
ConstraintName: "idx_mytable_mycolumn", | ||
}, | ||
wantCode: codes.AlreadyExists, | ||
}, | ||
{ | ||
name: "with end-user message", | ||
in: fault.Wrap( | ||
gorm.ErrDuplicatedKey, | ||
fmsg.WithDesc("already exists", "Resource already exists."), | ||
), | ||
wantCode: codes.AlreadyExists, | ||
wantMessage: "Resource already exists.", | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
c.Run(tc.name, func(c *qt.C) { | ||
err := fmt.Errorf("new err: %w", tc.in) | ||
got := AsGRPCError(err) | ||
c.Assert(got, qt.IsNotNil) | ||
|
||
st, ok := status.FromError(got) | ||
c.Assert(ok, qt.IsTrue) | ||
c.Assert(st.Code(), qt.Equals, tc.wantCode) | ||
if tc.wantMessage == "" { | ||
c.Assert(st.Message(), qt.Equals, err.Error()) | ||
return | ||
} | ||
|
||
c.Assert(st.Message(), qt.Equals, tc.wantMessage) | ||
}) | ||
} | ||
} |