github.com/team-ide/go-dialect@v1.9.20/vitess/vterrors/proto3.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package vterrors
    18  
    19  import (
    20  	vtrpcpb "github.com/team-ide/go-dialect/vitess/vtrpc"
    21  )
    22  
    23  // This file contains the necessary methods to send and receive errors
    24  // as payloads of proto3 structures. It converts vtError to and from
    25  // *vtrpcpb.RPCError. Use these methods when a RPC call can return both
    26  // data and an error.
    27  
    28  // FromVTRPC recovers a vtError from a *vtrpcpb.RPCError (which is how vtError
    29  // is transmitted across proto3 RPC boundaries).
    30  func FromVTRPC(rpcErr *vtrpcpb.RPCError) error {
    31  	if rpcErr == nil {
    32  		return nil
    33  	}
    34  	return New(rpcErr.Code, rpcErr.Message)
    35  }
    36  
    37  // ToVTRPC converts from vtError to a vtrpcpb.RPCError.
    38  func ToVTRPC(err error) *vtrpcpb.RPCError {
    39  	if err == nil {
    40  		return nil
    41  	}
    42  	return &vtrpcpb.RPCError{
    43  		Code:    Code(err),
    44  		Message: err.Error(),
    45  	}
    46  }