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

GraphQL error: Cannot set properties of undefined (setting 'code') #17817

Closed
3 tasks done
nodeworks opened this issue Mar 16, 2023 · 6 comments · Fixed by #18143
Closed
3 tasks done

GraphQL error: Cannot set properties of undefined (setting 'code') #17817

nodeworks opened this issue Mar 16, 2023 · 6 comments · Fixed by #18143

Comments

@nodeworks
Copy link
Contributor

Checklist

Describe the Bug

There is an issue with the GraphQL service that when there's an error, and Directus tries to format that error, it fails because a property on the error doesn't exist. It's trying to set "code" on the "extensions" property of the error but "extensions" doesn't exist which gives the error below.

Screenshot 2023-03-16 at 10 54 44 AM
Screenshot 2023-03-16 at 10 54 54 AM

To Reproduce

I'm using SQL Server, try making a graphql request that errors out.

Hosting Strategy

Self-Hosted (Docker Image)

@paescuj
Copy link
Member

paescuj commented Mar 20, 2023

Would you be able to provide an exact reproduction?

@nodeworks
Copy link
Contributor Author

I can’t as the data is sensitive. I can do a private screenshare though if that would help.

@paescuj
Copy link
Member

paescuj commented Mar 20, 2023

I can’t as the data is sensitive. I can do a private screenshare though if that would help.

If possible, simply some steps like the following example would be enough:

  1. Create collection "A"
  2. Create input field "B"
  3. ...
  4. Perform GraphQL mutation:
    mutation {
      update_a_item(id: 15, data: { b: "Example" }) {
        id
        b
      }
    }

@nodeworks
Copy link
Contributor Author

I think it might be specific to MS SQL. Anything from the graphql side that triggers an error (could be any error) that hits the formatError function causes the problem.

File: /api/src/services/graphql/index.ts
Function: formatError()

@nodeworks
Copy link
Contributor Author

It seems as though the "extensions" property on the error is set on the BaseException class:

packages/shared/src/exceptions/base.ts

However, when it's trying to extract the error and it can't find the error code it just straight up returns the error object:

Screenshot 2023-03-20 at 3 11 06 PM

Whereas it should be wrapping it in a new exception that extends the BaseException.

@nodeworks
Copy link
Contributor Author

These are currently the error codes for MSSQL for the Directus dialects:

enum MSSQLErrorCodes {
	FOREIGN_KEY_VIOLATION = 547,
	NOT_NULL_VIOLATION = 515,
	NUMERIC_VALUE_OUT_OF_RANGE = 220,
	UNIQUE_VIOLATION = 2601, // or 2627
	VALUE_LIMIT_VIOLATION = 2628,
}

The error I hit had a code of 544. So we can either:

  1. Wrap the generic error in a new exception that extends the BaseException when the error code isn't found / used.
  2. Populate the MSSQLErrorCodes enum with more available codes.
  3. Utilize the PR I made where we just check for the "extensions" property first before trying to access it.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants