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

Receive the original payload when using the 'request' or 'database' error flow events #21249

Open
nskartoredjo opened this issue Jan 30, 2024 · 3 comments · May be fixed by #22174
Open

Receive the original payload when using the 'request' or 'database' error flow events #21249

nskartoredjo opened this issue Jan 30, 2024 · 3 comments · May be fixed by #22174

Comments

@nskartoredjo
Copy link

nskartoredjo commented Jan 30, 2024

Describe the Bug

(Reported as a bug, as this used to be a feature supported in previous versions. I can't recall when this change happened)

Given a collection A and a o2m relationship with collection B through the field b_id, if you attempt to provide a value for b_id which has not yet been added to the collection B, you will receive a foreign key violation, which is to be expected.

However, I would like to recover from this position by creating a flow which either uses the request or a database error event. Unfortunately, the original payload (i.e. { "b_id": "something" }) isn't provided, thus making it hard to resolve the error by for instance creating the something ID inside the B collection.

To Reproduce

Create the two collections as described above. Create a flow which uses the request or database error event on collection A, and enable logging. Now try to append an entry to collection A by specifying an invalid b_id. Inside your log you will receive something like this:

{
  "payload": {
    "name": "DirectusError",
    "message": "Invalid foreign key for field \"b_id\" in collection \"A\".",
    "stack": "DirectusError: Invalid foreign key for field \"b_id\" in collection \"A\".\n    at foreignKeyViolation (file:///directus/node_modules/.pnpm/file+api_@[email protected][email protected][email protected][email protected]/node_modules/@directus/api/dist/database/errors/dialects/postgres.js:87:12)\n    at extractError (file:///directus/node_modules/.pnpm/file+api_@[email protected][email protected][email protected][email protected]/node_modules/@directus/api/dist/database/errors/dialects/postgres.js:21:20)\n    at translateDatabaseError (file:///directus/node_modules/.pnpm/file+api_@[email protected][email protected][email protected][email protected]/node_modules/@directus/api/dist/database/errors/translate.js:26:28)\n    at file:///directus/node_modules/.pnpm/file+api_@[email protected][email protected][email protected][email protected]/node_modules/@directus/api/dist/services/items.js:148:29\n    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"
  },
  "event": "database.error",
  "client": "postgres"
}

Ideally, you would receive something like this:

{
  "payload": {
    "name": "DirectusError",
    "message": "Invalid foreign key for field \"b_id\" in collection \"A\".",
    "stack": "...",
    "data": {
      "b_id": "something"
    }
  "event": "database.error",
  "client": "postgres"
}

Directus Version

v10.8.3

Hosting Strategy

Self-Hosted (Docker Image)

@br41nslug
Copy link
Member

Reported as a bug, as this used to be a feature supported in previous versions. I can't recall when this change happened

Looking through the code this was not a feature in Directus (9+) as version 9 was a complete rewrite we don't guarantee it to be feature complete with previous versions. Closing this issue for now as not a bug but feel free to create a feature request to get your wishes implemented.

@br41nslug br41nslug closed this as not planned Won't fix, can't repro, duplicate, stale Jan 30, 2024
@nskartoredjo
Copy link
Author

I've found the code that was responsible for this feature. It is seems to be removed after v10.3 during the introduction of the new error handling package. The old error handler included a field called invalid which provided this information:

function foreignKeyViolation(error: PostgresError) {
	const { table, detail } = error;

	const betweenParens = /\(([^)]+)\)/g;
	const matches = detail.match(betweenParens);

	if (!matches) return error;

	const collection = table;
	const field = matches[0].slice(1, -1);
	const invalid = matches[1]!.slice(1, -1);

	return new InvalidForeignKeyException(field, {
		collection,
		field,
		invalid,
	});
}

Is there a way to get this feature back?

@br41nslug
Copy link
Member

Ah i see where the misunderstanding happened, the complete "original payload" was never in the error but we did have a field for the specific value that was failing indeed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 📋 Backlog
Development

Successfully merging a pull request may close this issue.

3 participants