0

I am using the web SDK to interact with the Appwrite GraphQL API for user login. However, I am encountering an issue where the API returns a 200 response and the following response., even when I enter an incorrect email or password.

{
    "errors": [
        {
            "message": "Invalid credentials. Please check the email and password.",
            "extensions": {
                "category": "appwrite"
            },
            "locations": [
                {
                    "line": 5,
                    "column": 9
                }
            ],
            "path": [
                "accountCreateEmailSession"
            ]
        }
    ],
    "data": {
        "accountCreateEmailSession": null
    }
}

My code:

const login = async (email, password) => {
    try {
      const res = await graphql.mutation({
        query: `mutation (
          $email: String!,
          $password: String!,
      ) {
        accountCreateEmailSession(
          email: $email,
          password: $password,
      ) {
          _id
        }
      }`,
        variables: {
          email: email,
          password: password,
        },
      });
      console.log(res);
    } catch (error) {
      console.log(error);
      throw error;
    }
  };

It should log the error message from the catch block not from the try block when a user enters a wrong email or password.

Anyone please help me with this.

r121
  • 2,478
  • 8
  • 25
  • 44
  • 1
    Why do you assume graphql should throw an error? – Jan Pfeifer Jan 18 '23 at 09:15
  • 1
    Please post code, error messages, markup, data structures, and other textual information **as text**, not just as a *picture* of text. Why: http://meta.stackoverflow.com/q/285551/157247 – T.J. Crowder Jan 18 '23 at 09:15
  • What does this question have to do with [tag:reactjs]? – T.J. Crowder Jan 18 '23 at 09:18
  • For the avoidance of doubt: If `graphql.mutation` returns a promise and that promise is rejected, the `catch` block above **wlll** be triggered. So if it's not being triggered, either `graphql.mutation` is not returning a promise, or (this seems more likely) that promise isn't being rejected. The code shown doesn't seem to be using GraphQL.js. What JavaScript graphql library are you using? – T.J. Crowder Jan 18 '23 at 09:23
  • As @T.J.Crowder said. Expected return value is a Promise, but no exception is being thrown, nor promise is being rejected. So you cant catch it in try-catch block. There is no indication that API call should fail that way. API call was sucessfull, so all you need is to check res object and handle potential errors. – Jan Pfeifer Jan 18 '23 at 10:09
  • So, I should add a check inside the try catch, if there is an error then throw it: ``` if (res.errors) { throw res.errors[0].message; } ``` – r121 Jan 18 '23 at 10:28
  • @r121 you dont need try-catch at all. Just add some if-else logic. – Jan Pfeifer Jan 18 '23 at 11:34

1 Answers1

0

As docs say, in event of an error it will be inside the response body. However the HTTP status will be 200 OK at all times if HTTP request itself doesn't fail. You can consider implementing your own error handling logic.

Mert Canatan
  • 198
  • 1
  • 2
  • 11