Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9/N] Add integration test for interceptor <> transport integration #2344

Open
wants to merge 8 commits into
base: dev-tracing-fix
Choose a base branch
from
35 changes: 35 additions & 0 deletions api/transport/response.go
Original file line number Diff line number Diff line change
@@ -61,6 +61,41 @@ type ResponseWriter interface {
SetApplicationError()
}

// ExtendedResponseWriter extends the ResponseWriter interface to allow setting
// and retrieving additional metadata related to application errors, and to track
// the size of the response.
//
// Functions on ExtendedResponseWriter are not thread-safe.
type ExtendedResponseWriter interface {
ResponseWriter

// SetApplicationError specifies that this response contains an application error.
// If called, this MUST be called before any invocation of Write().
// This signals that the response is an application-level error, rather than
// a system or transport error.
SetApplicationError()

// SetApplicationErrorMeta allows setting additional metadata related to the
// application error. The metadata can contain fields such as error code, name,
// or details that provide more context about the error.
SetApplicationErrorMeta(applicationErrorMeta *ApplicationErrorMeta)

// IsApplicationError returns a boolean indicating whether the response
// contains an application error. This helps consumers of the response to know
// if the response was flagged as an application error.
IsApplicationError() bool

// ApplicationErrorMeta returns the application error metadata that was set
// by the SetApplicationErrorMeta method. If no metadata was set, this returns nil.
// This can provide further details about the nature of the application error.
ApplicationErrorMeta() *ApplicationErrorMeta

// ResponseSize returns the number of bytes written to the response. This method
// allows tracking of the size of the response, which can be useful for monitoring,
// logging, or debugging purposes.
ResponseSize() int
}

// ApplicationErrorMeta contains additional information to describe the
// application error, using an error name, code and details string.
//
66 changes: 66 additions & 0 deletions internal/interceptor/inbound.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) 2024 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

// Package interceptor defines the interceptor interfaces that are used within each transport.
// The package is currently put under internal because we don't allow customized interceptors at this moment.
// Interceptor interfaces are the alias types of middleware interfaces to share the common utility functions.
package interceptor

import (
"go.uber.org/yarpc/api/middleware"
)

type (
// UnaryInbound defines a transport interceptor for `UnaryHandler`s.
//
// UnaryInbound interceptor MAY do zero or more of the following: change the
// context, change the request, call the ResponseWriter, modify the response
// body by wrapping the ResponseWriter, handle the returned error, call the
// given handler zero or more times.
//
// UnaryInbound interceptor MUST be thread-safe.
//
// UnaryInbound interceptor is re-used across requests and MAY be called multiple times
// for the same request.
UnaryInbound = middleware.UnaryInbound

// OnewayInbound defines a transport interceptor for `OnewayHandler`s.
//
// OnewayInbound interceptor MAY do zero or more of the following: change the
// context, change the request, handle the returned error, call the given
// handler zero or more times.
//
// OnewayInbound interceptor MUST be thread-safe.
//
// OnewayInbound interceptor is re-used across requests and MAY be called
// multiple times for the same request.
OnewayInbound = middleware.OnewayInbound

// StreamInbound defines a transport interceptor for `StreamHandler`s.
//
// StreamInbound interceptor MAY do zero or more of the following: change the
// stream, handle the returned error, call the given handler zero or more times.
//
// StreamInbound interceptor MUST be thread-safe.
//
// StreamInbound interceptor is re-used across requests and MAY be called
// multiple times for the same request.
StreamInbound = middleware.StreamInbound
)
Loading