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

how to receive mirage's error msg on client #4

Open
siddo420 opened this issue Mar 8, 2016 · 3 comments
Open

how to receive mirage's error msg on client #4

siddo420 opened this issue Mar 8, 2016 · 3 comments

Comments

@siddo420
Copy link

siddo420 commented Mar 8, 2016

On the client, is there any way I could access the error message/object that was returned by validator() method?

primus.id.validator(function validate(spark, fn) {
  if (spark.mirage === 'old') return fn(undefined, 'new');
  if (spark.mirage === 'new') return fn(undefined, 'old');

  fn(new Error('The id is neither new or old'));  //<--- how do I access this error object on client?
});

I am trying to make sure accounts are in a specific state but I need to know where/how validator function failed? I could have done it in authentication middleware but I can still not detect the error message there (or correct me if I am wrong).

I was thinking if we could have an event thrown on client something like 'mirage error' with this error object then that would solve this problem.

@lpinca
Copy link
Member

lpinca commented Mar 8, 2016

You should be able to send data to the client:

primus.id.validator(function validate(spark, fn) {
  if (spark.mirage === 'old') return fn(undefined, 'new');
  if (spark.mirage === 'new') return fn(undefined, 'old');

  var err = new Error('The id is neither new or old');

  spark.emit('mirage error', err.message);
  fn(err);
});

With the auth hook the error message is automatically sent to the client.

@siddo420
Copy link
Author

siddo420 commented Mar 8, 2016

So, 'spark' is active in validator. Does spark.write properly work here as well?

I was under the impression that communication with client is only possible inside/after 'connection' event (which I believe is only emitted once .validator returns with success).

Auth errors:

I got confused by

"Unfortunately, the amount of detail you get in your client when authorization fails depends on the transformer in use. Most real-time frameworks supported by Primus don't expose the status code, headers or response body." at https://github.com/primus/primus#client. I'm using Engine.io (any issues?).

However, I also did some testing and the actual error code/msg server sent is printed from somewhere inside the primus client lib and I did not receive it in 'error' or 'unexpected-response' events. How do I access through 'primus' client object on client.

    debug('xhr data %s', this.data);
    xhr.send(this.data);  // <-- this line prints the status code etc
  } catch (e) {

From 'error' event on client, I get.

Error: xhr poll error

then, there is stack trace.

FYI: the status code is 503, 401 etc.

@lpinca
Copy link
Member

lpinca commented Mar 8, 2016

The documentation is correct, if you use Engine.IO and the auth hook, in your error listener you'll get a generic xhr poll error. That error object actually includes a description property, but Engine.IO uses the status code as description which is not very useful. If that line is changed to self.onError(xhr.responseText || xhr.status); then you'll get the actual JSON string returned by primus authorization middleware.

So it doesn't work out of the box.

spark.write works inside the validator. mirage uses an async listener for the connection event which prevents the next listeners from being called until this one completes its operations. If mirage validation or generation fails, the connection is automatically closed. Messages sent by the client during this validation/generation process are buffered and not emitted until validation completes successfully.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants