go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/grpc/appstatus/doc.go (about)

     1  // Copyright 2019 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package appstatus can attach/reterieve an application-specific response
    16  // status to/from an error. It designed to prevent accidental exposure of
    17  // internal statuses to RPC clients, for example Spanner's statuses.
    18  //
    19  // # Attaching a status
    20  //
    21  // Use ToError, Error and Errorf to create new status-annotated errors.
    22  // Use Attach and Attachf to annotate existing errors with a status.
    23  //
    24  //	if req.PageSize < 0  {
    25  //	   return appstatus.Errorf(codes.InvalidArgument, "page size cannot be negative")
    26  //	}
    27  //	if err := checkState(); err != nil {
    28  //	  return appstatus.Attachf(err, codes.PreconditionFailed, "invalid state")
    29  //	}
    30  //
    31  // This may be done deep in the function call hierarchy.
    32  //
    33  // Do not use appstatus in code where you don't explicitly intend to return a
    34  // specific status. This is unnecessary because any unrecognized error is
    35  // treated as internal and because it is explicitly prohibited to attach a
    36  // status to an error chain multiple times. When a status is attached, the
    37  // package supports its propagation all the way to the requester
    38  // unless there is code that explicitly throws it away.
    39  //
    40  // # Returning a status
    41  //
    42  // Use GRPCifyAndLog right before returning the error from a gRPC method
    43  // handler. Usually it is done in a Postlude of a service decorator, see
    44  // ../cmd/svcdec.
    45  //
    46  //	func NewMyServer() pb.MyServer {
    47  //	  return &pb.DecoratedMyServer{
    48  //	    Service:  &actualImpl{},
    49  //	    Postlude: func(ctx context.Context, methodName string, rsp proto.Message, err error) error {
    50  //	      return appstatus.GRPCifyAndLog(ctx, err)
    51  //	    },
    52  //	  }
    53  //	}
    54  //
    55  // It recognizes only appstatus-annotated errors and treats any other error as
    56  // internal. This behavior is important to avoid accidentally returning errors
    57  // from Spanner or other client library that also uses grpc/status package to
    58  // communicate status code from *other* services. For example, if a there is a
    59  // typo in Spanner SQL statement, spanner package may return a status-annotated
    60  // errors with code NotFound. In this case, our RPC must respond with internal
    61  // error and not NotFound.
    62  package appstatus