mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Improved flows error class handling (#18421)
* improved flows error handling * Create gentle-cars-watch.md * updated changeset
This commit is contained in:
@@ -19,7 +19,6 @@ import getDatabase from './database/index.js';
|
||||
import emitter from './emitter.js';
|
||||
import env from './env.js';
|
||||
import * as exceptions from './exceptions/index.js';
|
||||
import { BaseException } from '@directus/exceptions';
|
||||
import logger from './logger.js';
|
||||
import { getMessenger } from './messenger.js';
|
||||
import { ActivityService } from './services/activity.js';
|
||||
@@ -31,6 +30,7 @@ import { constructFlowTree } from './utils/construct-flow-tree.js';
|
||||
import { getSchema } from './utils/get-schema.js';
|
||||
import { JobQueue } from './utils/job-queue.js';
|
||||
import { mapValuesDeep } from './utils/map-values-deep.js';
|
||||
import { sanitizeError } from './utils/sanitize-error.js';
|
||||
|
||||
let flowManager: FlowManager | undefined;
|
||||
|
||||
@@ -433,10 +433,9 @@ class FlowManager {
|
||||
} catch (error) {
|
||||
let data;
|
||||
|
||||
if (error instanceof BaseException) {
|
||||
data = { message: error.message, code: error.code, extensions: error.extensions, status: error.status };
|
||||
} else if (error instanceof Error) {
|
||||
data = { message: error.message };
|
||||
if (error instanceof Error) {
|
||||
// make sure we dont expose the stack trace
|
||||
data = sanitizeError(error);
|
||||
} else if (typeof error === 'string') {
|
||||
// If the error is a JSON string, parse it and use that as the error data
|
||||
data = isValidJSON(error) ? parseJSON(error) : error;
|
||||
|
||||
29
api/src/utils/sanitize-error.test.ts
Normal file
29
api/src/utils/sanitize-error.test.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { describe, expect, test } from 'vitest';
|
||||
import { sanitizeError } from './sanitize-error.js';
|
||||
import { BaseException } from '@directus/exceptions';
|
||||
|
||||
describe('sanitizeError', () => {
|
||||
test('removes stack trace from Error', () => {
|
||||
const error = new Error('test message', {
|
||||
cause: 'test',
|
||||
});
|
||||
|
||||
const result = sanitizeError(error);
|
||||
|
||||
expect(result.stack).toBe(undefined);
|
||||
expect(result.message).toBe(error.message);
|
||||
expect(result.cause).toBe(error.cause);
|
||||
});
|
||||
|
||||
test('removes stack trace from BaseException', () => {
|
||||
const error = new BaseException('test message', 418, 'TEAPOT', { more: 'info' });
|
||||
|
||||
const result = sanitizeError(error);
|
||||
|
||||
expect(result.stack).toBe(undefined);
|
||||
expect(result.message).toBe(error.message);
|
||||
expect(result.code).toBe(error.code);
|
||||
expect(result.status).toBe(error.status);
|
||||
expect(result.extensions).toStrictEqual(result.extensions);
|
||||
});
|
||||
});
|
||||
8
api/src/utils/sanitize-error.ts
Normal file
8
api/src/utils/sanitize-error.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
export function sanitizeError<T extends Error>(error: T): T {
|
||||
// clear the stack
|
||||
if (error.stack !== undefined) {
|
||||
delete error.stack;
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
Reference in New Issue
Block a user