0

Using the following code:

 const assrt = function () {
    try {
      return chaiAssert.apply(null, arguments);
    }
    catch (e) {
      return handleError(e);
    }
  };

 v.assert = new Proxy(assrt, {
    get: function (target, prop) {

      if(typeof prop === 'symbol'){
        // I don't know what to do with symbols, so return
         return Reflect.get(...arguments);
      }

      // but here! we still get properties that don't exist
      if(!chaiAssert[prop]){
        return handleError(
          new Error(`The assertion library used does not have '${prop}' property or method.`)
        );
      }

      return function () {
        try {
          return chaiAssert[prop].apply(null, arguments);
        }
        catch (e) {
          return handleError(e);
        }
      }
    }
  });

the error I get with this code is:

TypeError: Cannot convert a Symbol value to a string

and this occurs on the line:

new Error(`The assertion library used does not have '${prop}' property or method.`));

I have used Proxies before, and I have never seen Symbols being passed to the get method of the Proxy. Does anyone know how to circumvent this problem?

Why are Symbols being passed to the Proxy get function and how do I properly handle that?

2
  • the prop that's being sent to the get function is Symbol(util.inspect.custom), and think there are others that get sent as well. Commented Sep 28, 2017 at 22:06
  • I think, you have to debug the code. It doesn't seem like symbols have to be filtered out by if(typeof prop === 'symbol'){. Commented Oct 11, 2017 at 13:15

1 Answer 1

2

Why are Symbols being passed to the Proxy get function?

We don't know, you didn't show any code that actually uses the proxy. But many symbols are accessed by builtin methods, e.g. when you iterate the proxy it uses the Symbol.iterator method.

and how do I properly handle that?

You cannot concatenate a symbol with a string, you need to be explicit about doing this. You can use either prop.toString() or just switch based on typeof prop.

Sign up to request clarification or add additional context in comments.

7 Comments

can you demonstrate what you mean by switching on typeof prop
typeof prop == "symbol" ? "Symbol ["+prop.toString()+"]" : prop or whatever you want to do with symbols. Maybe just ignore them completely in your proxy handler and just respond with the default Reflect.get for them.
Yeah I don't know what to do with the symbols but I need to pass back what they are looking for when the symbols get passed...can you show how to use Reflect as you suggest?
looks like this => return Reflect.get(...arguments)
I am so confused why a non-symbol property would get passed to the handler's get method, and the target property would not exist on the target. I will update the OP.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.