mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 07:15:36 +00:00
chore(error-handler): display message cause the error (#3996)
This commit is contained in:
parent
1e0501cd96
commit
320005843a
@ -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',
|
||||
|
@ -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,
|
||||
},
|
||||
],
|
||||
|
Loading…
Reference in New Issue
Block a user