go.uber.org/yarpc@v1.72.1/api/transport/response.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 transport
    22  
    23  import (
    24  	"io"
    25  
    26  	"go.uber.org/yarpc/yarpcerrors"
    27  )
    28  
    29  // Response is the low level response representation.
    30  type Response struct {
    31  	Headers Headers
    32  	Body    io.ReadCloser
    33  	// Response payload size before any compression applied by the protocol
    34  	// When using the HTTP transport, this value is set from the HTTP header
    35  	// content-length. It should be noted that this value is set manually and
    36  	// will not be updated automatically if the body is being modified
    37  	BodySize int
    38  
    39  	ApplicationError bool
    40  	// ApplicationErrorMeta adds information about the application error.
    41  	// This field will only be set if `ApplicationError` is true.
    42  	ApplicationErrorMeta *ApplicationErrorMeta
    43  }
    44  
    45  // ResponseWriter allows Handlers to write responses in a streaming fashion.
    46  //
    47  // Functions on ResponseWriter are not thread-safe.
    48  type ResponseWriter interface {
    49  	io.Writer
    50  
    51  	// AddHeaders adds the given headers to the response. If called, this MUST
    52  	// be called before any invocation of Write().
    53  	//
    54  	// This MUST NOT panic if Headers is nil.
    55  	AddHeaders(Headers)
    56  	// TODO(abg): Ability to set individual headers instead?
    57  
    58  	// SetApplicationError specifies that this response contains an
    59  	// application error. If called, this MUST be called before any invocation
    60  	// of Write().
    61  	SetApplicationError()
    62  }
    63  
    64  // ApplicationErrorMeta contains additional information to describe the
    65  // application error, using an error name, code and details string.
    66  //
    67  // Fields are optional for backwards-compatibility and may not be present in all
    68  // responses.
    69  type ApplicationErrorMeta struct {
    70  	Code    *yarpcerrors.Code
    71  	Name    string
    72  	Details string
    73  }
    74  
    75  // ApplicationErrorMetaSetter enables setting the name of an
    76  // application error, surfacing it in metrics.
    77  //
    78  // Conditionally upcast a ResponseWriter to access the
    79  // functionality.
    80  type ApplicationErrorMetaSetter interface {
    81  	// SetApplicationErrorMeta specifies the name of the application error, if any.
    82  	// If called, this MUST be called before any invocation of Write().
    83  	SetApplicationErrorMeta(*ApplicationErrorMeta)
    84  }