akveo / nebular

:boom: Customizable Angular UI Library based on Eva Design System :new_moon_with_face::sparkles:Dark Mode
https://akveo.github.io/nebular
MIT License
8.05k stars 1.51k forks source link

How to display the error message from the server. #442

Closed Hexor closed 6 years ago

Hexor commented 6 years ago

Issue type

I'm submitting a ... (check one with "x")

Issue description

Current behavior: I want to know how to display the error message from the server. For example, if the password was not right for login, what exact json response should server response so that nebular could display the error.

Currently my serve will return the api response like this:

{
    "errors": "Password not right",
    "messages": "",
    "data": {
        "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9kaWRpZGkudHRcL2FwaVwvYXV0aFwvcmVnaXN0ZXIiLCJpYXQiOjE1MjcxNDU1NjksImV4cCI6MTUyNzE0OTE2OSwibmJmIjoxNTI3MTQ1NTY5LCJqdGkiOiJnOVNJVldvOEppVFRvM3FFIiwic3ViIjo3LCJwcnYiOiI4N2UwYWYxZWY5ZmQxNTgxMmZkZWM5NzE1M2ExNGUwYjA0NzU0NmFhIn0._IxOIl9MyKn7bVxIDG9B6LrMbKVpxPHnb0EjAwglIkg",
        "token_type": "bearer",
        "expired_after": 1527149169
    }
}

but nebular will not show "Password not right" message as I wished.

Thank you.

Expected behavior:

Steps to reproduce:

Related code:

I had followed the docs(Custom UI Components) to change auth ui. How can I get the error message which i received from the server?

login(): void {
    this.errors = this.messages = [];
    this.submitted = true;

    this.service.authenticate(this.provider, this.user).subscribe((result: NbAuthResult) => {
      this.submitted = false;

      if (result.isSuccess()) {
        this.messages = result.getMessages();
      } else {
        this.errors = result.getErrors();
      }

      const redirect = result.getRedirect();
      if (redirect) {
        setTimeout(() => {
          return this.router.navigateByUrl(redirect);
        }, this.redirectDelay);
      }
    });
  }

Other information:

npm, node, OS, Browser

<!--
Node, npm: `node --version` and `npm --version`
OS: Windows (7/8/10). Linux (incl. distribution). macOS (El Capitan? Sierra?)
Browser: Chrome/Safari/Firefox/etc?
-->

Angular, Nebular

<!--
Check your `package-lock.json` or locate a `package.json` in the `node_modules` folder.
-->
nnixaa commented 6 years ago

Hi @Hexor, I presume you use NbEmailPassAuthProvider, if so, please take a look at the available options here https://akveo.github.io/nebular/#/docs/auth/nbemailpassauthprovider. By default this provider waits for an array of errors by the data.errors path in your response object, so something like this:

{
  data: { errors: ['Error Message']}
}

So you can either change your API to conform to this format or adjust the provider configuration, for example, change getter function:

errors: {
   getter: (module: string, res: HttpErrorResponse) => {
    return [res.errors];
  },
 },

Hope this helps.

Hexor commented 6 years ago

Thank you. I forgot to return the http status code. Once I return a error code like 404, everything works fine.