Repository: googleapis/genai-toolbox
File: internal/server/api.go (Go)

Code before:

	res, err := tool.Invoke(ctx, params, accessToken)

	// Determine what error to return to the users.
	if err != nil {
		errStr := err.Error()
		var statusCode int

		// Upstream API auth error propagation
		switch {
		case strings.Contains(errStr, "Error 401"):
			statusCode = http.StatusUnauthorized
		case strings.Contains(errStr, "Error 403"):
			statusCode = http.StatusForbidden
		}

		if statusCode == http.StatusUnauthorized || statusCode == http.StatusForbidden {
			if tool.RequiresClientAuthorization() {
				// Propagate the original 401/403 error.
				s.logger.DebugContext(ctx, fmt.Sprintf("error invoking tool. Client credentials lack authorization to the source: %v", err))
				_ = render.Render(w, r, newErrResponse(err, statusCode))
				return
			}
			// ADC lacking permission or credentials configuration error.
			internalErr := fmt.Errorf("unexpected auth error occured during Tool invocation")
			s.logger.ErrorContext(ctx, fmt.Sprintf("%s: %v", internalErr, err))
			_ = render.Render(w, r, newErrResponse(internalErr, http.StatusInternalServerError))
			return
		}
		err = fmt.Errorf("error while invoking tool: %w", err)
		s.logger.DebugContext(ctx, err.Error())
		_ = render.Render(w, r, newErrResponse(err, http.StatusBadRequest))
		return
	}

	resMarshal, err := json.Marshal(res)
	if err != nil {
		err = fmt.Errorf("unable to marshal result: %w", err)
		s.logger.DebugContext(ctx, err.Error())
		_ = render.Render(w, r, newErrResponse(err, http.StatusInternalServerError))
		return
	}

	_ = render.Render(w, r, &resultResponse{Result: string(resMarshal)})
}

var _ render.Renderer = &resultResponse{} // Renderer interface for managing response payloads.

// resultResponse is the response sent back when the tool was invocated successfully.
type resultResponse struct {
	Result string `json:"result"` // result of tool invocation

Review comment (suggestion):
If we are using %s, does internalErr need to be fmt.Errorf or fmt.Sprint?

(I think we should use fmt.Errorf with %w can wrap the causing error)

Code after:

	res, err := tool.Invoke(ctx, params, accessToken)

	// Determine what error to return to the users.
	if err != nil {
		errStr := err.Error()
		var statusCode int

		// Upstream API auth error propagation
		switch {
		case strings.Contains(errStr, "Error 401"):
			statusCode = http.StatusUnauthorized
		case strings.Contains(errStr, "Error 403"):
			statusCode = http.StatusForbidden
		}

		if statusCode == http.StatusUnauthorized || statusCode == http.StatusForbidden {
			if tool.RequiresClientAuthorization() {
				// Propagate the original 401/403 error.
				s.logger.DebugContext(ctx, fmt.Sprintf("error invoking tool. Client credentials lack authorization to the source: %v", err))
				_ = render.Render(w, r, newErrResponse(err, statusCode))
				return
			}
			// ADC lacking permission or credentials configuration error.
			internalErr := fmt.Errorf("unexpected auth error occured during Tool invocation: %w", err)
			s.logger.ErrorContext(ctx, internalErr.Error())
			_ = render.Render(w, r, newErrResponse(internalErr, http.StatusInternalServerError))
			return
		}
		err = fmt.Errorf("error while invoking tool: %w", err)
		s.logger.DebugContext(ctx, err.Error())
		_ = render.Render(w, r, newErrResponse(err, http.StatusBadRequest))
		return
	}

	resMarshal, err := json.Marshal(res)
	if err != nil {
		err = fmt.Errorf("unable to marshal result: %w", err)
		s.logger.DebugContext(ctx, err.Error())
		_ = render.Render(w, r, newErrResponse(err, http.StatusInternalServerError))
		return
	}

	_ = render.Render(w, r, &resultResponse{Result: string(resMarshal)})
}

var _ render.Renderer = &resultResponse{} // Renderer interface for managing response payloads.

// resultResponse is the response sent back when the tool was invocated successfully.
type resultResponse struct {
	Result string `json:"result"` // result of tool invocation