Skip to content

v0.11.0 schema errors (breaking API changes)

Compare
Choose a tag to compare
@VojtechVitek VojtechVitek released this 23 Mar 11:51
· 70 commits to master since this release
4fb8b9d

See full [email protected] release.

[email protected] breaking changes

Note: You can turn on -legacyErrors=true flag on golang generator (ie. webrpc-gen -target=golang -legacyErrors=true -pkg=proto) in order to preserve the deprecated functions and sentinel errors (see below). This will allow you to migrate your codebase to the new custom schema errors gradually.

The following werbrpc error functions and sentinel errors are now deprecated or removed:

  • proto.WrapError() // Deprecated.
  • proto.Errorf() // Deprecated.
  • proto.HTTPStatusFromErrorCode()
  • proto.IsErrorCode()
  • proto.ErrCanceled // Deprecated.
  • proto.ErrUnknown // Deprecated.
  • proto.ErrFail // Deprecated.
  • proto.ErrInvalidArgument // Deprecated.
  • proto.ErrDeadlineExceeded // Deprecated.
  • proto.ErrNotFound // Deprecated.
  • proto.ErrBadRoute // Deprecated.
  • proto.ErrAlreadyExists // Deprecated.
  • proto.ErrPermissionDenied // Deprecated.
  • proto.ErrUnauthenticated // Deprecated.
  • proto.ErrResourceExhausted // Deprecated.
  • proto.ErrFailedPrecondition // Deprecated.
  • proto.ErrAborted // Deprecated.
  • proto.ErrOutOfRange // Deprecated.
  • proto.ErrUnimplemented // Deprecated.
  • proto.ErrInternal // Deprecated.
  • proto.ErrUnavailable // Deprecated.
  • proto.ErrDataLoss // Deprecated.
  • proto.ErrNone // Deprecated.

The schema errors can now be returned from the RPC endpoints via:

func (s *RPC) RemoveUser(ctx context.Context, userID int64) (bool, error) {
 	r, _ := ctx.Value(proto.HTTPRequestCtxKey).(*http.Request)
 	if s.IsRateLimited(r) {
-		return false, proto.Errorf(proto.ErrUnavailable, "rate limited")
+		return false, proto.ErrRateLimited // HTTP 429 per RIDL schema
 	}
 
 	_, err := s.DB.RemoveUser(ctx, userID)
 	if err != nil {
 		if errors.Is(err, pgx.ErrNoRows) {
-			return false, proto.Errorf(proto.ErrNotFound, "no such user(%v)", userID)
+			return false, proto.ErrUserNotFound
 		}
-		return false, proto.WrapError(proto.ErrInternal, err, "")
+		return false, proto.ErrorWithCause(proto.ErrDatabaseDown, err)
 	}
 
 	return true, nil
}

You can also return any other Go error and webrpc will render generic proto.ErrWebrpcEndpoint error automatically along with HTTP 400 status code.

return fmt.Errorf("some error")

The RPC client(s) can now assert the schema error type by their unique error code:

if err, ok := rpc.RemoveUser(ctx, userID); err != nil {
	if errors.Is(proto.ErrRateLimited) {
		// slow down; retry with back-off strategy
	}
	if errors.Is(proto.ErrUserNotFound) {
		// handle 
	}
	// etc.
}