Improved flows error class handling (#18421)

* improved flows error handling

* Create gentle-cars-watch.md

* updated changeset
This commit is contained in:
Brainslug
2023-05-03 15:40:45 +02:00
committed by GitHub
parent 0457bf3085
commit c74b5da7b9
4 changed files with 46 additions and 5 deletions

View File

@@ -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;

View 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);
});
});

View 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;
}