Giter VIP home page Giter VIP logo

Comments (2)

jcready avatar jcready commented on July 21, 2024 1

The first problem is that you shouldn't reassign the def[Whatever] variables since we're already returning references to their initial values (which is why you're seeing it only ever returning the first response). The second problem is that you're not throwing an error if the second attempt fails for the same isAuthError(response) logic. Here's an attempt to fix your interceptor:

export default (): RpcInterceptor => ({
  interceptUnary(
    next: NextUnaryFn,
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    method: MethodInfo<any, any>,
    input: object,
    options: RpcOptions,
  ): UnaryCall {
    let defHeader = new Deferred<RpcMetadata>();
    let defMessage = new Deferred<object>();
    let defStatus = new Deferred<RpcStatus>();
    let defTrailer = new Deferred<RpcMetadata>();

    void (async () => {
      try {
        const first_try = await next(method, input, options);

        // If the response doesn't have an auth error then resolve and exit.
        if (!isAuthError(first_try.response)) {
          defHeader.resolve(first_try.headers);
          defMessage.resolve(first_try.response);
          defStatus.resolve(first_try.status);
          defTrailer.resolve(first_try.trailers);
          return;
        }

        // Otherwise try again with a reset auth status
        console.warn(`[${method.name}] auth error, unauthenticating & retrying...`);

        const second_try = await next(
          method,
          {
            ...input,
            authStatus: {},
          },
          options,
        );

        // If the second attempt's response also has an auth error then throw a custom RpcError.
        if (isAuthError(second_try.response)) {
          const error = new RpcError(
            'The request does not have valid authentication credentials for the operation',
            'UNAUTHENTICATED',
            {...second_try.headers, ...second_try.trailers}
          );
          error.methodName = method.name;
          error.serviceName = method.service.typeName;
          throw error;
        }

        defHeader.resolve(second_try.headers);
        defMessage.resolve(second_try.response);
        defStatus.resolve(second_try.status);
        defTrailer.resolve(second_try.trailers);
      } catch (err) {
        defHeader.rejectPending(err);
        defMessage.rejectPending(err);
        defStatus.rejectPending(err);
        defTrailer.rejectPending(err);
      }
    })();

    return new UnaryCall(
      method,
      options.meta ?? {},
      input,
      defHeader.promise,
      defMessage.promise,
      defStatus.promise,
      defTrailer.promise,
    );
  },
});

from protobuf-ts.

memoalv avatar memoalv commented on July 21, 2024

Worked like a charm. Thank you very much for the pointers. I couldn't wrap my head around the def[Whatever] variables.

from protobuf-ts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.