go.uber.org/yarpc@v1.72.1/pkg/errors/server.go (about)

     1  // Copyright (c) 2022 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package errors
    22  
    23  import (
    24  	"fmt"
    25  	"strings"
    26  
    27  	"go.uber.org/yarpc/api/transport"
    28  	"go.uber.org/yarpc/yarpcerrors"
    29  )
    30  
    31  // RequestBodyDecodeError builds a YARPC error with code
    32  // yarpcerrors.CodeInvalidArgument that represents a failure to decode
    33  // the request body.
    34  func RequestBodyDecodeError(req *transport.Request, err error) error {
    35  	return newServerEncodingError(req, nil, false /*isResponse*/, false /*isHeader*/, err)
    36  }
    37  
    38  // ResponseBodyEncodeError builds a YARPC error with code
    39  // yarpcerrors.CodeInvalidArgument that represents a failure to encode
    40  // the response body.
    41  func ResponseBodyEncodeError(req *transport.Request, err error) error {
    42  	return newServerEncodingError(req, nil, true /*isResponse*/, false /*isHeader*/, err)
    43  }
    44  
    45  // RequestHeadersDecodeError builds a YARPC error with code
    46  // yarpcerrors.CodeInvalidArgument that represents a failure to
    47  // decode the request headers.
    48  func RequestHeadersDecodeError(req *transport.Request, err error) error {
    49  	return newServerEncodingError(req, nil, false /*isResponse*/, true /*isHeader*/, err)
    50  }
    51  
    52  // ResponseHeadersEncodeError builds a YARPC error with code
    53  // yarpcerrors.CodeInvalidArgument that represents a failure to
    54  // encode the response headers.
    55  func ResponseHeadersEncodeError(req *transport.Request, err error) error {
    56  	return newServerEncodingError(req, nil, true /*isResponse*/, true /*isHeader*/, err)
    57  }
    58  
    59  // ExpectEncodings verifies that the given request has one of the given
    60  // encodings, otherwise it returns a YARPC error with code
    61  // yarpcerrors.CodeInvalidArgument.
    62  func ExpectEncodings(req *transport.Request, want ...transport.Encoding) error {
    63  	got := req.Encoding
    64  	for _, w := range want {
    65  		if w == got {
    66  			return nil
    67  		}
    68  	}
    69  
    70  	return newServerEncodingError(req, want, false /*isResponse*/, false /*isHeader*/, newEncodingMismatchError(want, got))
    71  }
    72  
    73  func newServerEncodingError(req *transport.Request, encodings []transport.Encoding, isResponse bool, isHeader bool, err error) error {
    74  	if len(encodings) == 0 {
    75  		encodings = []transport.Encoding{req.Encoding}
    76  	}
    77  	parts := []string{"failed to"}
    78  	if isResponse {
    79  		switch len(encodings) {
    80  		case 1:
    81  			parts = append(parts, fmt.Sprintf("encode %q response", string(encodings[0])))
    82  		default:
    83  			parts = append(parts, fmt.Sprintf("encode %v response", encodings))
    84  		}
    85  	} else {
    86  		switch len(encodings) {
    87  		case 1:
    88  			parts = append(parts, fmt.Sprintf("decode %q request", string(encodings[0])))
    89  		default:
    90  			parts = append(parts, fmt.Sprintf("decode %v request", encodings))
    91  		}
    92  	}
    93  	if isHeader {
    94  		parts = append(parts, "headers")
    95  	} else {
    96  		parts = append(parts, "body")
    97  	}
    98  	parts = append(parts,
    99  		fmt.Sprintf("for procedure %q of service %q from caller %q: %v",
   100  			req.Procedure, req.Service, req.Caller, err))
   101  	return yarpcerrors.Newf(yarpcerrors.CodeInvalidArgument, strings.Join(parts, " "))
   102  }
   103  
   104  func newEncodingMismatchError(want []transport.Encoding, got transport.Encoding) error {
   105  	switch len(want) {
   106  	case 1:
   107  		return fmt.Errorf("expected encoding %q but got %q", want[0], got)
   108  	default:
   109  		return fmt.Errorf("expected one of encodings %v but got %q", want, got)
   110  	}
   111  }