ipc: Replace usage of verror1 with verror2.

Change-Id: I690be58ad4878a33cc7e98d00f4387c9ac86b8d4
diff --git a/runtimes/google/ipc/errors.vdl b/runtimes/google/ipc/errors.vdl
index 8eff5f7..8f36256 100644
--- a/runtimes/google/ipc/errors.vdl
+++ b/runtimes/google/ipc/errors.vdl
@@ -3,6 +3,36 @@
 import "v.io/core/veyron2/security"
 
 error (
-	InvalidBlessings(remote []string, remoteErr []security.RejectedBlessing, local []string, localErr []security.RejectedBlessing) {"en":"All valid blessings for this request: {remote} (rejected {remoteErr}) are disallowed by the policy {local} (rejected {localErr})"}
+	InvalidBlessings(remote []string, remoteErr []security.RejectedBlessing, local []string, localErr []security.RejectedBlessing) {
+		"en":"All valid blessings for this request: {remote} (rejected {remoteErr}) are disallowed by the policy {local} (rejected {localErr})",
+	}
 
+	// Internal errors.
+	badRequest(err error) {
+		"en": "failed to decode request: {err}",
+	}
+	badNumInputArgs(suffix, method string, numCalled, numWanted uint64) {
+		"en": "wrong number of input arguments for {suffix}.{method} (called with {numCalled} args, want {numWanted})",
+	}
+	badInputArg(suffix, method string, index uint64, err error) {
+		"en": "failed to decode request {suffix}.{method} arg #{index}: {err}",
+	}
+	badBlessings(err error) {
+		"en": "failed to decode blessings: {err}",
+	}
+	badBlessingsCache(err error) {
+		"en": "failed to find blessings in cache: {err}",
+	}
+	badDischarge(index uint64, err error) {
+		"en": "failed to decode discharge #{index}: {err}",
+	}
+	badDischargeType(index uint64, t typeobject) {
+		"en": "discharge #{index} type {t} isn't registered",
+	}
+	badDischargeImpl(index uint64, t string) {
+		"en": "discharge #{index} type {t} doesn't implement security.Discharge",
+	}
+	badAuth(suffix, method string, err error) {
+		"en": "not authorized to call {suffix}.{method}: {err}",
+	}
 )
diff --git a/runtimes/google/ipc/errors.vdl.go b/runtimes/google/ipc/errors.vdl.go
index 81ff4f8..a6b3b7c 100644
--- a/runtimes/google/ipc/errors.vdl.go
+++ b/runtimes/google/ipc/errors.vdl.go
@@ -7,6 +7,7 @@
 	// VDL system imports
 	"v.io/core/veyron2/context"
 	"v.io/core/veyron2/i18n"
+	"v.io/core/veyron2/vdl"
 	"v.io/core/veyron2/verror2"
 
 	// VDL user imports
@@ -15,13 +16,77 @@
 
 var (
 	InvalidBlessings = verror2.Register("v.io/core/veyron/runtimes/google/ipc.InvalidBlessings", verror2.NoRetry, "{1:}{2:} All valid blessings for this request: {3} (rejected {4}) are disallowed by the policy {5} (rejected {6})")
+	// Internal errors.
+	badRequest        = verror2.Register("v.io/core/veyron/runtimes/google/ipc.badRequest", verror2.NoRetry, "{1:}{2:} failed to decode request: {3}")
+	badNumInputArgs   = verror2.Register("v.io/core/veyron/runtimes/google/ipc.badNumInputArgs", verror2.NoRetry, "{1:}{2:} wrong number of input arguments for {3}.{4} (called with {5} args, want {6})")
+	badInputArg       = verror2.Register("v.io/core/veyron/runtimes/google/ipc.badInputArg", verror2.NoRetry, "{1:}{2:} failed to decode request {3}.{4} arg #{5}: {6}")
+	badBlessings      = verror2.Register("v.io/core/veyron/runtimes/google/ipc.badBlessings", verror2.NoRetry, "{1:}{2:} failed to decode blessings: {3}")
+	badBlessingsCache = verror2.Register("v.io/core/veyron/runtimes/google/ipc.badBlessingsCache", verror2.NoRetry, "{1:}{2:} failed to find blessings in cache: {3}")
+	badDischarge      = verror2.Register("v.io/core/veyron/runtimes/google/ipc.badDischarge", verror2.NoRetry, "{1:}{2:} failed to decode discharge #{3}: {4}")
+	badDischargeType  = verror2.Register("v.io/core/veyron/runtimes/google/ipc.badDischargeType", verror2.NoRetry, "{1:}{2:} discharge #{3} type {4} isn't registered")
+	badDischargeImpl  = verror2.Register("v.io/core/veyron/runtimes/google/ipc.badDischargeImpl", verror2.NoRetry, "{1:}{2:} discharge #{3} type {4} doesn't implement security.Discharge")
+	badAuth           = verror2.Register("v.io/core/veyron/runtimes/google/ipc.badAuth", verror2.NoRetry, "{1:}{2:} not authorized to call {3}.{4}: {5}")
 )
 
 func init() {
 	i18n.Cat().SetWithBase(i18n.LangID("en"), i18n.MsgID(InvalidBlessings.ID), "{1:}{2:} All valid blessings for this request: {3} (rejected {4}) are disallowed by the policy {5} (rejected {6})")
+	i18n.Cat().SetWithBase(i18n.LangID("en"), i18n.MsgID(badRequest.ID), "{1:}{2:} failed to decode request: {3}")
+	i18n.Cat().SetWithBase(i18n.LangID("en"), i18n.MsgID(badNumInputArgs.ID), "{1:}{2:} wrong number of input arguments for {3}.{4} (called with {5} args, want {6})")
+	i18n.Cat().SetWithBase(i18n.LangID("en"), i18n.MsgID(badInputArg.ID), "{1:}{2:} failed to decode request {3}.{4} arg #{5}: {6}")
+	i18n.Cat().SetWithBase(i18n.LangID("en"), i18n.MsgID(badBlessings.ID), "{1:}{2:} failed to decode blessings: {3}")
+	i18n.Cat().SetWithBase(i18n.LangID("en"), i18n.MsgID(badBlessingsCache.ID), "{1:}{2:} failed to find blessings in cache: {3}")
+	i18n.Cat().SetWithBase(i18n.LangID("en"), i18n.MsgID(badDischarge.ID), "{1:}{2:} failed to decode discharge #{3}: {4}")
+	i18n.Cat().SetWithBase(i18n.LangID("en"), i18n.MsgID(badDischargeType.ID), "{1:}{2:} discharge #{3} type {4} isn't registered")
+	i18n.Cat().SetWithBase(i18n.LangID("en"), i18n.MsgID(badDischargeImpl.ID), "{1:}{2:} discharge #{3} type {4} doesn't implement security.Discharge")
+	i18n.Cat().SetWithBase(i18n.LangID("en"), i18n.MsgID(badAuth.ID), "{1:}{2:} not authorized to call {3}.{4}: {5}")
 }
 
 // MakeInvalidBlessings returns an error with the InvalidBlessings ID.
 func MakeInvalidBlessings(ctx *context.T, remote []string, remoteErr []security.RejectedBlessing, local []string, localErr []security.RejectedBlessing) error {
 	return verror2.Make(InvalidBlessings, ctx, remote, remoteErr, local, localErr)
 }
+
+// makeBadRequest returns an error with the badRequest ID.
+func makeBadRequest(ctx *context.T, err error) error {
+	return verror2.Make(badRequest, ctx, err)
+}
+
+// makeBadNumInputArgs returns an error with the badNumInputArgs ID.
+func makeBadNumInputArgs(ctx *context.T, suffix string, method string, numCalled uint64, numWanted uint64) error {
+	return verror2.Make(badNumInputArgs, ctx, suffix, method, numCalled, numWanted)
+}
+
+// makeBadInputArg returns an error with the badInputArg ID.
+func makeBadInputArg(ctx *context.T, suffix string, method string, index uint64, err error) error {
+	return verror2.Make(badInputArg, ctx, suffix, method, index, err)
+}
+
+// makeBadBlessings returns an error with the badBlessings ID.
+func makeBadBlessings(ctx *context.T, err error) error {
+	return verror2.Make(badBlessings, ctx, err)
+}
+
+// makeBadBlessingsCache returns an error with the badBlessingsCache ID.
+func makeBadBlessingsCache(ctx *context.T, err error) error {
+	return verror2.Make(badBlessingsCache, ctx, err)
+}
+
+// makeBadDischarge returns an error with the badDischarge ID.
+func makeBadDischarge(ctx *context.T, index uint64, err error) error {
+	return verror2.Make(badDischarge, ctx, index, err)
+}
+
+// makeBadDischargeType returns an error with the badDischargeType ID.
+func makeBadDischargeType(ctx *context.T, index uint64, t *vdl.Type) error {
+	return verror2.Make(badDischargeType, ctx, index, t)
+}
+
+// makeBadDischargeImpl returns an error with the badDischargeImpl ID.
+func makeBadDischargeImpl(ctx *context.T, index uint64, t string) error {
+	return verror2.Make(badDischargeImpl, ctx, index, t)
+}
+
+// makeBadAuth returns an error with the badAuth ID.
+func makeBadAuth(ctx *context.T, suffix string, method string, err error) error {
+	return verror2.Make(badAuth, ctx, suffix, method, err)
+}
diff --git a/runtimes/google/ipc/server.go b/runtimes/google/ipc/server.go
index 473c224..30a86b0 100644
--- a/runtimes/google/ipc/server.go
+++ b/runtimes/google/ipc/server.go
@@ -19,7 +19,6 @@
 	"v.io/core/veyron2/security"
 	"v.io/core/veyron2/services/security/access"
 	"v.io/core/veyron2/vdl"
-	old_verror "v.io/core/veyron2/verror"
 	verror "v.io/core/veyron2/verror2"
 	"v.io/core/veyron2/vlog"
 	"v.io/core/veyron2/vom"
@@ -978,7 +977,7 @@
 		if err == io.EOF {
 			return err
 		}
-		return old_verror.BadProtocolf("ipc: response encoding failed: %v", err)
+		return fmt.Errorf("ipc: response encoding failed: %v", err)
 	}
 	if response.Error != nil {
 		return response.Error
@@ -988,7 +987,7 @@
 			if err == io.EOF {
 				return err
 			}
-			return old_verror.BadProtocolf("ipc: result #%d [%T=%v] encoding failed: %v", ix, res, res, err)
+			return fmt.Errorf("ipc: result #%d [%T=%v] encoding failed: %v", ix, res, res, err)
 		}
 	}
 	// TODO(ashankar): Should unread data from the flow be drained?
@@ -1007,7 +1006,7 @@
 	return nil
 }
 
-func (fs *flowServer) readIPCRequest() (*ipc.Request, old_verror.E) {
+func (fs *flowServer) readIPCRequest() (*ipc.Request, error) {
 	// Set a default timeout before reading from the flow. Without this timeout,
 	// a client that sends no request or a partial request will retain the flow
 	// indefinitely (and lock up server resources).
@@ -1018,19 +1017,19 @@
 	// Decode the initial request.
 	var req ipc.Request
 	if err := fs.dec.Decode(&req); err != nil {
-		return nil, old_verror.BadProtocolf("ipc: request decoding failed: %v", err)
+		return nil, verror.Make(verror.BadProtocol, fs.T, makeBadRequest(fs.T, err))
 	}
 	return &req, nil
 }
 
-func (fs *flowServer) processRequest() ([]interface{}, old_verror.E) {
+func (fs *flowServer) processRequest() ([]interface{}, error) {
 	fs.starttime = time.Now()
-	req, verr := fs.readIPCRequest()
-	if verr != nil {
+	req, err := fs.readIPCRequest()
+	if err != nil {
 		// We don't know what the ipc call was supposed to be, but we'll create
 		// a placeholder span so we can capture annotations.
 		fs.T, _ = vtrace.SetNewSpan(fs.T, fmt.Sprintf("\"%s\".UNKNOWN", fs.Name()))
-		return nil, verr
+		return nil, err
 	}
 	fs.method = req.Method
 	fs.suffix = strings.TrimLeft(req.Suffix, "/")
@@ -1051,32 +1050,32 @@
 	go fs.cancelContextOnClose(cancel)
 
 	// Initialize security: blessings, discharges, etc.
-	if verr := fs.initSecurity(req); verr != nil {
-		return nil, verr
+	if err := fs.initSecurity(req); err != nil {
+		return nil, err
 	}
 	// Lookup the invoker.
 	invoker, auth, err := fs.lookup(fs.suffix, &fs.method)
 	if err != nil {
-		return nil, old_verror.Convert(err)
+		return nil, err
 	}
 	// Prepare invoker and decode args.
 	numArgs := int(req.NumPosArgs)
 	argptrs, tags, err := invoker.Prepare(fs.method, numArgs)
 	fs.tags = tags
 	if err != nil {
-		return nil, old_verror.Makef(verror.ErrorID(err), "%s: name: %q", err, fs.suffix)
+		return nil, err
 	}
-	if len(argptrs) != numArgs {
-		return nil, old_verror.BadProtocolf(fmt.Sprintf("ipc: wrong number of input arguments for method %q, name %q (called with %d args, expected %d)", fs.method, fs.suffix, numArgs, len(argptrs)))
+	if called, want := req.NumPosArgs, uint64(len(argptrs)); called != want {
+		return nil, verror.Make(verror.BadProtocol, fs.T, makeBadNumInputArgs(fs.T, fs.suffix, fs.method, called, want))
 	}
 	for ix, argptr := range argptrs {
 		if err := fs.dec.Decode(argptr); err != nil {
-			return nil, old_verror.BadProtocolf("ipc: arg %d decoding failed: %v", ix, err)
+			return nil, verror.Make(verror.BadProtocol, fs.T, makeBadInputArg(fs.T, fs.suffix, fs.method, uint64(ix), err))
 		}
 	}
 	// Check application's authorization policy.
-	if verr := authorize(fs, auth); verr != nil {
-		return nil, verr
+	if err := authorize(fs, auth); err != nil {
+		return nil, err
 	}
 	// Check if the caller is permitted to view debug information.
 	// TODO(mattr): Is access.Debug the right thing to check?
@@ -1084,7 +1083,7 @@
 	// Invoke the method.
 	results, err := invoker.Invoke(fs.method, fs, argptrs)
 	fs.server.stats.record(fs.method, time.Since(fs.starttime))
-	return results, old_verror.Convert(err)
+	return results, err
 }
 
 func (fs *flowServer) cancelContextOnClose(cancel context.CancelFunc) {
@@ -1123,11 +1122,11 @@
 		obj, auth, err := disp.Lookup(suffix)
 		switch {
 		case err != nil:
-			return nil, nil, old_verror.Convert(err)
+			return nil, nil, err
 		case obj != nil:
 			invoker, err := objectToInvoker(obj)
 			if err != nil {
-				return nil, nil, old_verror.Internalf("ipc: invalid received object: %v", err)
+				return nil, nil, verror.Make(verror.Internal, fs.T, "invalid received object", err)
 			}
 			return invoker, auth, nil
 		}
@@ -1145,11 +1144,11 @@
 	return ipc.ReflectInvoker(obj)
 }
 
-func (fs *flowServer) initSecurity(req *ipc.Request) old_verror.E {
+func (fs *flowServer) initSecurity(req *ipc.Request) error {
 	// If additional credentials are provided, make them available in the context
 	blessings, err := security.NewBlessings(req.GrantedBlessings)
 	if err != nil {
-		return old_verror.BadProtocolf("ipc: failed to decode granted blessings: %v", err)
+		return verror.Make(verror.BadProtocol, fs.T, makeBadBlessings(fs.T, err))
 	}
 	fs.blessings = blessings
 	// Detect unusable blessings now, rather then discovering they are unusable on
@@ -1160,7 +1159,7 @@
 	// this - should servers be able to assume that a blessing is something that
 	// does not have the authorizations that the server's own identity has?
 	if blessings != nil && !reflect.DeepEqual(blessings.PublicKey(), fs.flow.LocalPrincipal().PublicKey()) {
-		return old_verror.NoAccessf("ipc: blessing granted not bound to this server(%v vs %v)", blessings.PublicKey(), fs.flow.LocalPrincipal().PublicKey())
+		return verror.Make(verror.NoAccess, fs.T, fmt.Sprintf("blessing granted not bound to this server(%v vs %v)", blessings.PublicKey(), fs.flow.LocalPrincipal().PublicKey()))
 	}
 	fs.clientBlessings, err = serverDecodeBlessings(fs.flow.VCDataCache(), req.Blessings, fs.server.stats)
 	if err != nil {
@@ -1169,7 +1168,7 @@
 		// TODO(suharshs,toddw): Figure out a way to only shutdown the current VC, instead
 		// of all VCs connected to the RemoteEndpoint.
 		fs.server.streamMgr.ShutdownEndpoint(fs.RemoteEndpoint())
-		return old_verror.BadProtocolf("ipc: blessings cache failed: %v", err)
+		return verror.Make(verror.BadProtocol, fs.T, makeBadBlessingsCache(fs.T, err))
 	}
 	if fs.clientBlessings != nil {
 		fs.ackBlessings = true
@@ -1179,7 +1178,7 @@
 		if w, ok := d.(security.WireDischarge); ok {
 			dis, err := security.NewDischarge(w)
 			if err != nil {
-				return old_verror.BadProtocolf("ipc: discharge #%d: %v", i, err)
+				return verror.Make(verror.BadProtocol, fs.T, makeBadDischarge(fs.T, uint64(i), err))
 			}
 			fs.discharges[dis.ID()] = dis
 			continue
@@ -1191,9 +1190,9 @@
 			continue
 		}
 		if v, ok := d.(*vdl.Value); ok {
-			return old_verror.BadProtocolf("ipc: discharge #%d of type %s isn't registered", i, v.Type())
+			return verror.Make(verror.BadProtocol, fs.T, makeBadDischargeType(fs.T, uint64(i), v.Type()))
 		}
-		return old_verror.BadProtocolf("ipc: discharge #%d of type %T doesn't implement security.Discharge", i, d)
+		return verror.Make(verror.BadProtocol, fs.T, makeBadDischargeImpl(fs.T, uint64(i), fmt.Sprintf("%T", d)))
 	}
 	return nil
 }
@@ -1204,7 +1203,7 @@
 	return nil
 }
 
-func authorize(ctx security.Context, auth security.Authorizer) old_verror.E {
+func authorize(ctx ipc.ServerContext, auth security.Authorizer) error {
 	if ctx.LocalPrincipal() == nil {
 		// LocalPrincipal is nil means that the server wanted to avoid
 		// authentication, and thus wanted to skip authorization as well.
@@ -1215,7 +1214,7 @@
 	}
 	if err := auth.Authorize(ctx); err != nil {
 		// TODO(ataly, ashankar): For privacy reasons, should we hide the authorizer error?
-		return old_verror.NoAccessf("ipc: not authorized to call %q.%q (%v)", ctx.Suffix(), ctx.Method(), err)
+		return verror.Make(verror.NoAccess, ctx.Context(), makeBadAuth(ctx.Context(), ctx.Suffix(), ctx.Method(), err))
 	}
 	return nil
 }
@@ -1223,7 +1222,7 @@
 // debugContext is a context which wraps another context but always returns
 // the debug tag.
 type debugContext struct {
-	security.Context
+	ipc.ServerContext
 }
 
 func (debugContext) MethodTags() []interface{} {