chore(error-handler): display message cause the error (#3996)

This commit is contained in:
ChengLei Shao 2024-04-09 19:52:08 +08:00 committed by GitHub
parent 1e0501cd96
commit 320005843a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 2 deletions

View File

@ -1,5 +1,6 @@
import { Database } from '@nocobase/database';
import { MockServer, createMockServer } from '@nocobase/test';
import { createMockServer, MockServer } from '@nocobase/test';
describe('create with exception', () => {
let app: MockServer;
beforeEach(async () => {
@ -13,6 +14,27 @@ describe('create with exception', () => {
await app.destroy();
});
it('should render error with cause property', async () => {
app.use(
(ctx, next) => {
ctx.throw(
400,
new Error('wrapped error', {
cause: new Error('original error'),
}),
);
},
{ after: 'dataSource' },
);
const response = await app.agent().resource('users').create({
values: {},
});
expect(response.statusCode).toEqual(400);
expect(response.body.errors[0].message).contains('original error');
});
it('should handle not null error', async () => {
const collection = app.collection({
name: 'users',

View File

@ -10,10 +10,17 @@ export class ErrorHandler {
defaultHandler(err, ctx) {
ctx.status = err.statusCode || err.status || 500;
let message = err.message;
// if error has a cause, append it to the message
if (err.cause) {
message += `: ${err.cause.message}`;
}
ctx.body = {
errors: [
{
message: err.message,
message,
code: err.code,
},
],